java调用http的几种方式

参考:https://blog.csdn.net/qq_16504067/article/details/121114404

1 JDK自带API

java核心jar包rt.jar包下为我们提供了java操作http的类。java.net包下面的抽象类HttpURLConnection为我们提供了发起http请求的途径和方法。其具体实现类同样在rt.jar包中,为sun.net.www.protocol.http.HttpURLConnection。这两个类名相同但是包名不同。

其继承关系为:

sun.net.www.protocol.http.HttpURLConnection<---java.net.HttpURLConnection<---java.net.URLConnection

下面举例说明该类的使用方式。

1.1 HttpURLConnection实现http请求

public class HttpURLConnectionUtil {
    public static String doGet(String httpUrl){
        //链接
        HttpURLConnection connection = null;
        InputStream is = null;
        BufferedReader br = null;
        StringBuffer result = new StringBuffer();
        try {
            //创建连接
            URL url = new URL(httpUrl);
            connection = (HttpURLConnection) url.openConnection();
            //设置请求方式
            connection.setRequestMethod("GET");
            //设置连接超时时间
            connection.setReadTimeout(15000);
            //开始连接
            connection.connect();
            //获取响应数据
            if (connection.getResponseCode() == 200) {
                //获取返回的数据
                is = connection.getInputStream();
                if (null != is) {
                    br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                    String temp = null;
                    while (null != (temp = br.readLine())) {
                        result.append(temp);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //关闭远程连接
            connection.disconnect();
        }
        return result.toString();
    }

    public static String doPost(String httpUrl, @Nullable String param) {
        StringBuffer result = new StringBuffer();
        //连接
        HttpURLConnection connection = null;
        OutputStream os = null;
        InputStream is = null;
        BufferedReader br = null;
        try {
            //创建连接对象
            URL url = new URL(httpUrl);
            //创建连接
            connection = (HttpURLConnection) url.openConnection();
            //设置请求方法
            connection.setRequestMethod("POST");
            //设置连接超时时间
            connection.setConnectTimeout(15000);
            //设置读取超时时间
            connection.setReadTimeout(15000);
            //DoOutput设置是否向httpUrlConnection输出,DoInput设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个
            //设置是否可读取
            connection.setDoOutput(true);
            connection.setDoInput(true);
            //设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
            connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");

            //拼装参数
            if (null != param && param.equals("")) {
                //设置参数
                os = connection.getOutputStream();
                //拼装参数
                os.write(param.getBytes("UTF-8"));
            }
            //设置权限
            //设置请求头等
            //开启连接
            //connection.connect();
            //读取响应
            if (connection.getResponseCode() == 200) {
                is = connection.getInputStream();
                if (null != is) {
                    br = new BufferedReader(new InputStreamReader(is, "GBK"));
                    String temp = null;
                    while (null != (temp = br.readLine())) {
                        result.append(temp);
                        result.append("\r\n");
                    }
                }
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭连接
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //关闭连接
            connection.disconnect();
        }
        return result.toString();
    }

    public static void main(String[] args) {
        String message = doGet("https://v0.yiketianqi.com/api?unescape=1&version=v91&appid=43656176&appsecret=I42og6Lm&ext=&cityid=&city=");//查询天气的接口
        System.out.println(message);
    }
}

执行结果:

{"cityid":"101280601","city":"深圳","cityEn":"shenzhen","country":"中国","countryEn":"China","update_time":"2022-08-09 22:37:53","data":[{"day":"09日(星期二)","date":"2022-08-09","week":"星期二","wea":"大雨转暴雨","wea_img":"yu",...}

1.2 调用过程分析

创建sun.net.www.protocol.http.HttpURLConnection对象

 connection = (HttpURLConnection) url.openConnection(); 

这里使用URL的openConnection()方法创建一个HttpURLConnection对象,其实这里方法名具有误导性,通过查看源码,我们可以发现这里其实并不是打开一个连接,这里只是创建了一个HttpURLConnection对象,该对象携带了一些属性:

 

 开始连接

 connection.connect(); 

这里进行tcp连接的建立(三次握手)

此时,HttpURLConnection对象的属性如下(注意如果https需要看代理对象),连接状态为已建立连接。

 

  connection.getResponseCode() 

这行代码作用是获取响应状态码,除了返回状态码之外,还做了很多其他的工作。其中比较重要的是为HttpURLConnection对象设置inputStream属性,该属性表示一个输入流,携带了响应数据,我们就是从这个属性来获得响应数据的。

 

 下面代码是从输入流中读取响应体。

                if (null != is) {
                    br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                    String temp = null;
                    while (null != (temp = br.readLine())) {
                        result.append(temp);
                    }
                }

 

1.3 扩展:reponse返回文件时的处理

既然我们可以得到响应的一个InputStream,如果这个输入流里面是一个文件,我们同样可以有办法接收和另存为文件到本地。

如何判断一个响应是文件类型还是普通的文本类型呢?我们使用响应头中的字段Content-Type字段和Content-Disposition字段进行判断

    public static String download_pdf(String httpUrl, @Nullable String param) {
        StringBuffer result = new StringBuffer();
        //连接
        HttpURLConnection connection = null;
        OutputStream os = null;
        InputStream is = null;
        BufferedReader br = null;
        try {
            //创建连接对象
            URL url = new URL(httpUrl);
            //创建连接
            connection = (HttpURLConnection) url.openConnection();
            //设置请求方法
            connection.setRequestMethod("POST");
            //设置连接超时时间
            connection.setConnectTimeout(15000);
            //设置读取超时时间
            connection.setReadTimeout(15000);
            //DoOutput设置是否向httpUrlConnection输出,DoInput设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个
            //设置是否可读取
            connection.setDoOutput(true);
            connection.setDoInput(true);
            //设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
            connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");

            //拼装参数
            if (null != param) {
                //设置参数
                os = connection.getOutputStream();
                //拼装参数
                os.write(param.getBytes("UTF-8"));
            }
            //设置权限
            //设置请求头等
            //开启连接
            connection.connect();
            //读取响应
            if (connection.getResponseCode() == 200) {
                String contentType = connection.getHeaderField("Content-Type");
                String contentDisposition = connection.getHeaderField("Content-Disposition");
                String filename = contentDisposition.substring(21);
                is = connection.getInputStream();
                if (null != is) {
                    if("application/pdf".equals(contentType))
                    {
                        inputStreamToFile(is,filename);
                        return "pdf文件,已下载到本地";
                    }
                    br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                    String temp = null;
                    while (null != (temp = br.readLine())) {
                        result.append(temp);
                        result.append("\r\n");
                    }
                }
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭连接
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //关闭连接
            connection.disconnect();
        }
        return result.toString();
    }

    private static void inputStreamToFile(InputStream inputStream,String filename) {
        try {
            //新建文件
            File file = new File("E:\\"+filename);
            if (file.exists()) {
                file.createNewFile();
            }
            OutputStream os = new FileOutputStream(file);
            int read = 0;
            byte[] bytes = new byte[1024 * 1024];
            //先读后写
            while ((read = inputStream.read(bytes)) > 0) {
                byte[] wBytes = new byte[read];
                System.arraycopy(bytes, 0, wBytes, 0, read);
                os.write(wBytes);
            }
            os.flush();
            os.close();
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

main方法

    public static void main(String[] args) {
        String message = download_pdf("https://xxx/api/files/achieve_pdf","{\"LCID\":\"F21FTSNCKF2460_MUSenyoC\", \"file\":\"BGI_F21FTSNCKF2460_MUSenyoC_report_cn.pdf\",\"token\":\"xxxxxx\"}");
        System.out.println(message);
    }

 

调试信息:

这里Content-Type:application/pdf,Content-Disposition:attachment; filename=BGI_F21FTSNCKF2460_MUSenyoC_report_cn.pdf

 最后,我们实现了从响应输入流中获得文件,并保存在了本地。

1.4 扩展:request携带文件时的情况

此处,我们讲一下使用http传递图片时的处理方式和原理。

我们知道,http请求包括三部分:请求行、请求头、请求体。本质上http报文是传递的字节流。当我们发送http报文时,HTTP报文会被拆分成多个数据包进行传输,每个数据包再加上tcp头组成tcp报文,数据链路层也会对上层报文进行拆包组成以太网帧,一直到达物理层将以太网帧(一段固定长度的字节)转换为高低电平沿着网线发送出去。

也就是说http报文本质上是字节流。

我们以使用form方式传输图片为例。所谓的form方式和其他方式本质上没有区别,只不过在http头中设置content-type为:multipart/form-data。

而当传输的数据是图片时,content-type又有一些其他的规定,即如下格式:

    POST /api/v4/projects/1416/uploads?access_token=6e4cc9972164966584ae51ca19a91f87607dcdefd6a14db9513fb4ebd6968485 HTTP/1.1
    Host: gitlab.genomics.cn
    Content-Length: 225
    Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
    Content-Disposition: form-data; name="file"; filename="20230906170617.png"
    Content-Type: image/png

            (data)
------WebKitFormBoundary7MA4YWxkTrZu0gW--

也就是说,我们在Content-Type中指定一个boundary,标志其后面将要传输的数据成分比较复杂,可能包含普通键值对字符串,也可能包含图片文件,也可能有其他类型的数据。所有这些都以boundary作为分界线。

上栗中,我们传递的数据只有一种成分(图片),我们传递的键为file,值为图片文件的字节数据。

原理上面讲过了,下面不再废话,上代码

public class HttpURLConnectionUtil {
    public static String doPost(String httpUrl, String name, String filename, @Nullable byte[] param) {
        StringBuffer result = new StringBuffer();
        //连接
        HttpURLConnection connection = null;
        OutputStream os = null;
        InputStream is = null;
        BufferedReader br = null;
        try {
            //创建连接对象
            URL url = new URL(httpUrl);
            //创建连接
            connection = (HttpURLConnection) url.openConnection();
            //设置请求方法
            connection.setRequestMethod("POST");
            //设置连接超时时间
            connection.setConnectTimeout(15000);
            //设置读取超时时间
            connection.setReadTimeout(15000);
            //DoOutput设置是否向httpUrlConnection输出,DoInput设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个
            //设置是否可读取
            connection.setDoOutput(true);
//            connection.setDoInput(true);
            //设置通用的请求属性
            connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=---------------------------1234567890");

            String boundary = "---------------------------1234567890";
            String lineBreak = "\r\n";

            //拼装参数
            if (null != param) {
                //设置参数
                os = connection.getOutputStream();
                StringBuffer sb = new StringBuffer();
//                PrintWriter writer = new PrintWriter(new OutputStreamWriter(os, "UTF-8"), true);
                sb.append("--").append(boundary).append(lineBreak);
                sb.append("Content-Disposition: form-data; name=\""+name+"\"; filename=\""+filename+"\"").append(lineBreak);
                String suffix = filename.substring(filename.lastIndexOf(".")+1, filename.length());
                sb.append("Content-Type: image/"+suffix).append(lineBreak);
                sb.append(lineBreak);
                os.write(sb.toString().getBytes(StandardCharsets.UTF_8));
                //拼装参数
                os.write(param);

                sb = new StringBuffer();
                sb.append(lineBreak);
                sb.append("--").append(boundary).append("--").append(lineBreak);
                os.write(sb.toString().getBytes(StandardCharsets.UTF_8));
                os.flush();
            }
            //设置权限
            //设置请求头等
            //开启连接
            //connection.connect();
            //读取响应
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                is = connection.getInputStream();
                if (null != is) {
                    br = new BufferedReader(new InputStreamReader(is, "GBK"));
                    String temp = null;
                    while (null != (temp = br.readLine())) {
                        result.append(temp);
                        result.append("\r\n");
                    }
                }
            } else {
                is = connection.getInputStream();
                if (null != is) {
                    br = new BufferedReader(new InputStreamReader(is, "GBK"));
                    String temp = null;
                    while (null != (temp = br.readLine())) {
                        result.append(temp);
                        result.append("\r\n");
                    }
                }
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭连接
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //关闭连接
            connection.disconnect();
        }
        return result.toString();
    }
}

调用

byte[] fileBytes = FileUtil.toByteArray(fullPath);// 将文件转为字节数组,略
String filename = FileUtil.filename(fullPath);// 文件名
String retStr = HttpURLConnectionUtil.doPost(url, "file", filename, fileBytes);

1.5 扩展:request同时携带文件和参数示例

1.5.1 接收文件和其他参数接口

    @PostMapping("/testszj")
    public ResponseEntity<?> testszj(@RequestParam("file") MultipartFile file,
                                              @RequestParam("LCID") String LCID,
                                              @RequestParam("elementType") String elementType, @RequestParam("projectType") String projectType) {
        if (file.isEmpty()) {
            return new ResponseEntity<>("请选择一个文件上传", HttpStatus.BAD_REQUEST);
        }

        // 这里只是简单地打印文件信息和其他字段,实际项目中你可能需要保存文件或进行其他处理
        System.out.println("上传的文件名: " + file.getOriginalFilename());
        System.out.println("LCID: " + LCID);
        System.out.println("elementType: " + elementType);
        System.out.println("projectType: " + projectType);

        return new ResponseEntity<>("success", HttpStatus.OK);
    }

1.5.2 发送携带文件和其他参数

HttpFormService.java

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.Map;

@Service
public class HttpFormService {
    private static Logger LOGGER = LoggerFactory.getLogger(HttpFormService.class);
    public String doPost(String httpUrl, Map params, String token) {
        if(null == params) {
            LOGGER.error("参数为空");
            return "参数为空";
        }
        if(null == params.get("file") || "".equals(params.get("file"))) {
            LOGGER.error("文件未传递");
            return "文件未传递";
        }
        String filePath = params.get("file").toString();
        File file = new File(filePath);
        if (!file.exists()) {
            LOGGER.error("文件不存在:{}", filePath);
            return "文件不存在:"+filePath;
        }

        HttpURLConnection conn = null;
        OutputStream os = null;
        FileInputStream fis = null;
        BufferedReader br = null;

        try {
            URL url = new URL(httpUrl);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            if(null != token) {
                conn.setRequestProperty("Authorization", token);
            }
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
            conn.setDoOutput(true);
            os = conn.getOutputStream();
            // Write text fields
            String textParamLine = "";
            for(Object key : params.keySet()) {
                if("file".equals(key)) {
                    continue;
                }
                if(null == params.get(key)) {
                    continue;
                }
                textParamLine = textParamLine + "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n"
                        + "Content-Disposition: form-data; name=\""+key+"\"\r\n\r\n"
                        + params.get(key) + "\r\n"
                        + "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n";
            }
            if(textParamLine.length()>0) {
                os.write(textParamLine.getBytes(StandardCharsets.UTF_8));
            }

            // Write file field
            String fileParamLine = "Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"\r\n"
                    + "Content-Type: " + URLConnection.guessContentTypeFromName(file.getName()) + "\r\n\r\n";
            os.write(fileParamLine.getBytes(StandardCharsets.UTF_8));

            // Write file content
            fis = new FileInputStream(file);
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }

            // Write end boundary
            String endBoundaryLine = "\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--\r\n";
            os.write(endBoundaryLine.getBytes(StandardCharsets.UTF_8));


            br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
            String ret = "";
            String output;
            LOGGER.info("Response from server ....");
            while ((output = br.readLine()) != null) {
                ret = ret + output;
            }
            LOGGER.info(ret);
            return ret;
        } catch (IOException e) {
            LOGGER.error("e", e);
            return "发生了错误";
        } finally {
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            conn.disconnect();
        }
    }
}

1.5.3 调用示例

    @RequestMapping(value = "/upload")
    @ResponseBody
    public CommonResult upload(@RequestBody Map params) {
        String response = httpFormService.doPost("http://localhost:8090/pms/tom/meta/testszj", params, null);

        LOGGER.info("response:{}", JsonUtil.objectToJson(response));
        // 处理响应
        return new CommonResult().success(response);
    }

调用命令

curl -H 'Content-Type: application/json' -X POST -d '{"LCID":"test","elementType":"elementType","projectType":"projectType","file":"D://demo.xlsx"}' 'http://localhost:8080/pms/tom/meta/upload'

1.6 HttpsURLConnection实现https请求

使用HttpsURLConnection,HttpsURLConnection和HttpURLConnection非常类似,只不过需要通过证书文件初始化一个SSLContext对象,然后通过conn.setSSLSocketFactory设置ssl连接

下面例子,通过https发送文件

@Service
public class HttpsFormService {
    private static Logger LOGGER = LoggerFactory.getLogger(HttpsFormService.class);
    public String doPost(String httpsUrl, Map params, String token) {
        if(null == params) {
            LOGGER.error("参数为空");
            return "error:参数为空";
        }
        if(null == params.get("file") || "".equals(params.get("file"))) {
            LOGGER.error("文件未传递");
            return "error:文件未传递";
        }
        String filePath = params.get("file").toString();
        File file = new File(filePath);
        if (!file.exists()) {
            LOGGER.error("文件不存在:{}", filePath);
            return "error:文件不存在:"+filePath;
        }

        HttpsURLConnection conn = null;
        OutputStream os = null;
        FileInputStream fis = null;
        BufferedReader br = null;

        try {
            KeyStore keyStore = loadKeyStoreFromPem("16sconfig");
            TrustManager[] trustManagers = getTrustManagers(keyStore);

            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, trustManagers, null);

            URL url = new URL(httpsUrl);
            conn = (HttpsURLConnection) url.openConnection();
            conn.setSSLSocketFactory(sslContext.getSocketFactory());

            conn.setRequestMethod("POST");
            if(null != token) {
                conn.setRequestProperty("Authorization", token);
            }
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
            conn.setDoOutput(true);
            os = conn.getOutputStream();
            // Write text fields
            String textParamLine = "";
            for(Object key : params.keySet()) {
                if("file".equals(key)) {
                    continue;
                }
                if("token".equals(key)) {
                    continue;
                }
                if(null == params.get(key)) {
                    continue;
                }
                textParamLine = textParamLine + "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n"
                        + "Content-Disposition: form-data; name=\""+key+"\"\r\n\r\n"
                        + params.get(key) + "\r\n"
                        + "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n";
            }
            if(textParamLine.length()>0) {
                os.write(textParamLine.getBytes(StandardCharsets.UTF_8));
            }

            // Write file field
            String fileParamLine = "Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"\r\n"
                    + "Content-Type: " + URLConnection.guessContentTypeFromName(file.getName()) + "\r\n\r\n";
            os.write(fileParamLine.getBytes(StandardCharsets.UTF_8));

            // Write file content
            fis = new FileInputStream(file);
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }

            // Write end boundary
            String endBoundaryLine = "\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--\r\n";
            os.write(endBoundaryLine.getBytes(StandardCharsets.UTF_8));


            br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
            String ret = "";
            String output;
            LOGGER.info("Response from server ....");
            while ((output = br.readLine()) != null) {
                ret = ret + output;
            }
            LOGGER.info(ret);
            return ret;
        } catch (IOException e) {
            LOGGER.error("e", e);
            return "error:发生了错误";
        } catch (NoSuchAlgorithmException e) {
            LOGGER.error("e", e);
            return "error:发生了错误";
        } catch (KeyManagementException e) {
            LOGGER.error("e", e);
            return "error:发生了错误";
        } catch (Exception e) {
            LOGGER.error("e", e);
            return "error:发生了错误";
        } finally {
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            conn.disconnect();
        }
    }

    private TrustManager[] getTrustManagers(KeyStore keyStore) throws Exception {
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(keyStore);
        return tmf.getTrustManagers();
    }

    private KeyStore loadKeyStoreFromPem(String resourceName) throws Exception {
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null, null); // 初始化空的 KeyStore

        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream(resourceName);
        if (inputStream == null) {
            throw new FileNotFoundException("Resource not found: " + resourceName);
        }
        Certificate cert = cf.generateCertificate(inputStream);
        keyStore.setCertificateEntry("server", cert);
        inputStream.close();
        return keyStore;
    }
}

 

2 通过apache的HttpClient

需要注意的是,我们获取响应的方式

 

 我们可以使用3种方式处理响应

例子:

public class HttpClientUtil {
    public static String doGet(String url, String charset) {
        //1.生成HttpClient对象并设置参数
        HttpClient httpClient = new HttpClient();
        //设置Http连接超时为5秒
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
        //2.生成GetMethod对象并设置参数
        GetMethod getMethod = new GetMethod(url);
        //设置get请求超时为5秒
        getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
        //设置请求重试处理,用的是默认的重试处理:请求三次
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
        String response = "";
        //3.执行HTTP GET 请求
        try {
            int statusCode = httpClient.executeMethod(getMethod);
            //4.判断访问的状态码
            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("请求出错:" + getMethod.getStatusLine());
            }
            //5.处理HTTP响应内容
            //HTTP响应头部信息,这里简单打印
            Header[] headers = getMethod.getResponseHeaders();
            for(Header h : headers) {
                System.out.println(h.getName() + "---------------" + h.getValue());
            }
            //读取HTTP响应内容,这里简单打印网页内容
            //读取为字节数组
            byte[] responseBody = getMethod.getResponseBody();
            response = new String(responseBody, charset);
            System.out.println("-----------response:" + response);
            //读取为InputStream,在网页内容数据量大时候推荐使用
            //InputStream response = getMethod.getResponseBodyAsStream();
        } catch (HttpException e) {
            //发生致命的异常,可能是协议不对或者返回的内容有问题
            System.out.println("请检查输入的URL!");
            e.printStackTrace();
        } catch (IOException e) {
            //发生网络异常
            System.out.println("发生网络异常!");
        } finally {
            //6.释放连接
            getMethod.releaseConnection();
        }
        return response;
    }

    /**
     * post请求
     * @param url
     * @param json
     * @return
     */
    public static String doPost(String url, JSONObject json){
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(url);

        postMethod.addRequestHeader("accept", "*/*");
        postMethod.addRequestHeader("connection", "Keep-Alive");
        //设置json格式传送
        postMethod.addRequestHeader("Content-Type", "application/json;charset=UTF-8");
        //必须设置下面这个Header
        postMethod.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
        //添加请求参数
        postMethod.addParameter("commentId", json.getString("commentId"));

        String res = "";
        try {
            int code = httpClient.executeMethod(postMethod);
            if (code == 200){
                res = postMethod.getResponseBodyAsString();
                System.out.println(res);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(doGet("http://192.168.160.7:8088/2.async.js", "UTF-8"));
        System.out.println("-----------分割线------------");
        System.out.println("-----------分割线------------");
        System.out.println("-----------分割线------------");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("commentId", "13026194071");
        System.out.println(doPost("http://192.168.160.7:8088/pms/feedback/queryFeedback?createMan=songzhenjing", jsonObject));
    }
}

2.1 执行过程分析

我们分析一下上面例子的执行过程

 doPost("http://192.168.160.7:8088/pms/feedback/queryFeedback?createMan=xxx", jsonObject) 

调用这个方法中比较核心的一行代码是

 int code = httpClient.executeMethod(postMethod); 

调用HttpClient的executeMethod方法,该方法中创建一个HttpMethodDirector对象,并调用methodDirector.executeMethod(method);

然后,methodDirector.executeMethod(method)中调用了executeWithRetry(method);

然后PostMethod对象method调用method.execute(state, this.conn);

执行的是PostMethod的父类HttpMethodBase的execute方法,在这个父类的execute方法中调用

        writeRequest(state, conn);
        this.requestSent = true;
        readResponse(state, conn);

其中,writeRequest是将请求写到输出流,该输出流会把请求发送出去,经过网卡到达服务器

readResponse是获取服务器的响应,对应一个输入流,把响应写到输入流,然后我们可以在代码里接收输入流从而接收到响应数据。

继续看writeRequest(state, conn);这个函数的简略代码如下

    protected void writeRequest(HttpState state, HttpConnection conn){
        writeRequestLine(state, conn);//写请求行
        writeRequestHeaders(state, conn);//写请求头
        conn.writeLine(); // 写空行
        writeRequestBody(state, conn);//写请求体
    }

我们以writeRequestLine(state, conn);//写请求行,为例说明请求是如何写到输出流的。

conn是HttpConnection类型的对象,注意不要和java自带的HttpURLConnection混淆,他们是不同的,apache中的HttpConnection也不会最终调用jdk中的HttpURLConnection,他是自成一体

HttpConnection对象携带一个this.outputStream,通过调用this.outputStream.write(data, offset, length);把数据写到输出流。

2.2 对请求体的设置

添加form表单参数

上例中,有这么一行代码

 //添加请求参数 postMethod.addParameter("commentId", json.getString("commentId")); 

这里其实对应Content-Type:application/x-www-form-urlencoded时设置的form表单的参数,上例中其实有些不正确,不应该设置 postMethod.addRequestHeader("Content-Type", "application/json;charset=UTF-8"); 。

字节数组、字符串、输入流作为请求体

如下所示,我们使用RequestEntity设置请求体,它有4个实现类,分别表示字节数组作为请求体、输入流作为请求体、请求体传输文件、字符串作为请求体

 

比如,我们常常使用json传递参数,这时候设置请求体的方式为

        //设置json格式传送
        postMethod.addRequestHeader("Content-Type", "application/json;charset=UTF-8");
        //添加请求参数
        RequestEntity requestEntity = new StringRequestEntity("{json格式的参数}");
        postMethod.setRequestEntity(requestEntity);

下面例子中将会马上看到这种使用json作为body体的实例

2.3 扩展:reponse返回文件时的处理

 这个例子中,我们调用一个接口,这个接口接收json格式的参数,并返回一个pdf。我们把这个响应中的pdf以输入流的方式接收,并输出到本地路径下。

    public static boolean downloadpdf(String url, String json, String path){
        LOGGER.info("url:{},json:{},path:{}", url, json, path);
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(url);

        postMethod.addRequestHeader("accept", "*/*");
        postMethod.addRequestHeader("connection", "Keep-Alive");
        //设置json格式传送
        postMethod.addRequestHeader("Content-Type", "application/json;charset=UTF-8");
        //必须设置下面这个Header
        postMethod.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
        //添加请求参数
        RequestEntity requestEntity = new StringRequestEntity(json);
        postMethod.setRequestEntity(requestEntity);

        try {
            LOGGER.info("开始发起请求");
            int code = httpClient.executeMethod(postMethod);
            LOGGER.info("完成发起请求,已返回状态码:{}", code);
            if (code == 200){
                String contentType = postMethod.getResponseHeader("Content-Type").getValue();
                String contentDisposition = postMethod.getResponseHeader("Content-Disposition").getValue();
                LOGGER.info("contentType:{},contentDisposition:{}", contentType, contentDisposition);
                String filename = contentDisposition.substring(21);
                InputStream is = postMethod.getResponseBodyAsStream();
                if (null != is) {
                    if("application/pdf".equals(contentType))
                    {
                        return inputStreamToFile(is,path+filename);
                    }
                }
            }
            else {
                LOGGER.error("http响应码不是200");
            }
        } catch (IOException e) {
            LOGGER.error("e", e);
        }
        return false;
    }

    private static boolean inputStreamToFile(InputStream inputStream,String filename) {
        try {
            //新建文件
            File file = new File(filename);
            if (file.exists()) {
                file.createNewFile();
            }
            OutputStream os = new FileOutputStream(file);
            int read = 0;
            byte[] bytes = new byte[1024 * 1024];
            //先读后写
            while ((read = inputStream.read(bytes)) > 0) {
                byte[] wBytes = new byte[read];
                System.arraycopy(bytes, 0, wBytes, 0, read);
                os.write(wBytes);
            }
            os.flush();
            os.close();
            inputStream.close();
            return true;
        } catch (Exception e) {
            LOGGER.error("e", e);
        }
        return false;
    }

main方法

    public static void main(String[] args) {
        String json = "{\"LCID\":\"F21FTSNCKF2460_MUSenyoC\", \"file\":\"BGI_F21FTSNCKF2460_MUSenyoC_report_cn.pdf\",\"token\":\"xxx\"}";
        String path = "E:\\";
        downloadpdf("https://xxx/api/files/achieve_pdf", json, path);
    }

执行这个main方法后,将会看到,我们成功的实现了请求一个接口,返回一个pdf,并且我们将这个pdf保存到本地。

3 apache的CloseableHttpClient

个人理解和HttpClient类似,后续补充

4 spring的RestTemplate

这个是用的比较多的,封装的很好,但是因为封装的好,所以其内部细节往往不被我们重视,我们知其然而不能知其所以然,后续补充

5 java调用https接口

因为https使用tls协议进行通信,建立连接过程中需要传递证书文件(比如cert.pem)以便服务端验证证书的有效性。而java中使用KeyStore相关的API来进行证书的存储,因此我们需要首先根据证书文件创建一个KeyStore对象。详情见下面代码

因为系统中有其他需要用的RestTemplate,因此这里通过继承的方式定义一个新的RestTemplateHttps

public class RestTemplateHttps extends RestTemplate {
    public RestTemplateHttps(ClientHttpRequestFactory requestFactory) {
        super(requestFactory);
    }
}

RestTemplateConfig配置类

@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate() {
        List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
        interceptors.add(new HeaderRequestInterceptor(XAUTH_TOKEN_HEADER_NAME, inner_token));

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setInterceptors(interceptors);

        return restTemplate;
    }
    @Bean
    public RestTemplateHttps restTemplateForHttps() {
        try {
            KeyStore keyStore = loadKeyStoreFromPem(pemFilePath);
            TrustManager[] trustManagers = getTrustManagers(keyStore);

            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, trustManagers, null);

            SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(
                    sslContext,
                    NoopHostnameVerifier.INSTANCE  // 注意:生产环境中不建议使用NoopHostnameVerifier
            );

            CloseableHttpClient httpClient = HttpClients.custom()
                    .setSSLSocketFactory(sslSocketFactory)
                    .build();

            HttpComponentsClientHttpRequestFactory requestFactory =
                    new HttpComponentsClientHttpRequestFactory(httpClient);

            RestTemplateHttps restTemplate = new RestTemplateHttps(requestFactory);
            List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
            interceptors.add(new HeaderRequestInterceptor(XAUTH_TOKEN_HEADER_NAME, "eyJhbGciOiJIxxxInR5cCI6IkpXVCJ9.xxx.j1KqGFF0_swV3bzKenxxxU-5HsOkLYtk7UM"));
            restTemplate.setInterceptors(interceptors);
            return restTemplate;
        } catch (Exception e) {

        }
        return null;
    }

    public TrustManager[] getTrustManagers(KeyStore keyStore) throws Exception {
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(keyStore);
        return tmf.getTrustManagers();
    }

    private KeyStore loadKeyStoreFromPem(String pemFilePath) throws Exception {
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null, null); // 初始化空的 KeyStore

        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        FileInputStream fis = new FileInputStream(pemFilePath);
        Certificate cert = cf.generateCertificate(fis);
        keyStore.setCertificateEntry("server", cert);
        fis.close();
        return keyStore;
    }
}

接口

    @RequestMapping(value = "/updateProject", method = RequestMethod.POST)
    @ResponseBody
    public Object updateProject(@RequestBody Map params){
        LOGGER.info("params:{}", JsonUtil.objectToJson(params));
        return restTemplateHttps.postForObject("https:xxx.xxx.com/api/microbe/plan/customerProject", params, Object.class);
    }

调用,这里我们通过调用localhost的接口,间接调用另一个系统的https接口:

$ curl -H 'Authorization: xxx' -H 'Content-Type: application/json' -X POST -d '{"customer_email":"xxx"}' 'http://localhost:8090/api/16s/updateProject'

 

posted @ 2022-08-10 00:45  zhenjingcool  阅读(6127)  评论(0编辑  收藏  举报