springCloud 不同项目互相调用接口

A项目

1.调用接口类

public static String defaultConnection(String method, String path, int timeout, int readTimeout, String data)
            throws Exception {
        URL url=null;
        URLConnection con=null;
        HttpURLConnection urlCon=null;
        String strResponse=null;
        StringBuilder sb=new StringBuilder();
        int intResponseCode = HttpURLConnection.HTTP_OK;
        OutputStream out=null;
        InputStream input=null;

//第1步,建立连接

try {
    url=new URL(path);
    //向某个特定协议对象返回表现http资源连接的引用
    con=url.openConnection();
}catch(Exception e) {
    e.printStackTrace();
    System.out.println("http连接失败:"+e.getMessage());
}

//第2步,验证连接的类型,必须是HttpURLConnection

if(!(con instanceof HttpURLConnection)){
    System.out.println("http连接失败,连接类型错误");
}

//第3步,发送报文

  try {
        //表明程序必须把名称/值对输出到服务器程序资源
        con.setConnectTimeout(timeout == 0 ? 1000 : timeout);
        con.setReadTimeout(readTimeout == 0 ? 1000 : readTimeout);
        con.setRequestProperty("Content-Type","application/json");


        con.setDoOutput(true);
        con.setDoInput(true);
        //表明只能返回有用的信息
        con.setUseCaches(false);
        urlCon=(HttpURLConnection)con;
        //设置HTTP请求方法
        urlCon.setRequestMethod(method);
        //获得输出流对象
        out=urlCon.getOutputStream();
        DataOutputStream dos=new DataOutputStream(out);
        dos.write(data.getBytes("utf-8"));
        dos.flush();
    }catch(Exception e) {
        e.printStackTrace();
        System.out.println("http发送失败:"+e.getMessage());
    }finally{
        out.close();
    }
 //第4步,校验返回状态
try {
    intResponseCode=urlCon.getResponseCode();
}catch(Exception e) {
    e.printStackTrace();
    System.out.println("http返回状态失败:"+e.getMessage());
}
if(intResponseCode!=HttpURLConnection.HTTP_OK){  //如果不为(HTTP_OK)200,说明服务器返回错误
    System.out.println("request "+ urlCon.getURL() +" fail. response: code="+intResponseCode +
            ", message="+urlCon.getResponseMessage());
}
   //第5步,接收报文
    try {
        input=urlCon.getInputStream();
        //将字节流转换为字符流
        BufferedReader br=new BufferedReader(new InputStreamReader(input));
        while((strResponse=br.readLine())!=null){
            sb.append(strResponse);
        }
    }catch(Exception e){
        e.printStackTrace();
        System.out.println("http接收错误:"+e.getMessage());
    }finally{
        input.close();
        //关闭和服务器的连接
        urlCon.disconnect();
    }
    return sb.toString();
}

2. 调用方法

public void testtoken(){

        String uuid = "{\"accessToken\":\"123456\"}";
        System.out.println("uuid=="+uuid);
        String responseEntity = "";
        try {
            responseEntity = defaultConnection("POST", "http://localhost:50666/receiveToken", 10000, 10000, uuid);
        }catch(Exception e){
            e.printStackTrace();
        }
        System.out.println("responseEntity=="+responseEntity);
    }

B项目

@RequestMapping(value = "/receiveToken", method = RequestMethod.POST)
    public void receiveToken(@RequestBody UserParamDto user,HttpServletResponse response) throws Exception {
        System.out.println(user.getAccessToken());
        PrintWriter out = null;
        try {
            out = response.getWriter();
            System.out.println(out);
            out.write("{\"loginName\":\"11223344\"}");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

结果

A
在这里插入图片描述
B
在这里插入图片描述

————————————————
版权声明:本文为CSDN博主「m0_38072862」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_38072862/article/details/82419103

你可能感兴趣的:(java)