参考: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);
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
这个是用的比较多的,封装的很好,但是因为封装的好,所以其内部细节往往不被我们重视,我们知其然而不能知其所以然,后续补充