1、通过JDK网络类Java.net.URLConnection
(1)api
(2)使用:
GET:
1、创建远程连接
1234
URL url = new URL(httpUrl);
connection = (HttpURLConnection) url.openConnection();
12
2、设置连接方式(get、post、put。。。)和通用属性
1
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("Content-Type","application/json;charset=utf-8");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.setRequestMethod("GET"); //不设置也没问题
1234567
3、设置连接超时时间
1
connection.setReadTimeout(15000);
12
4、设置响应读取时间
5、发起请求
12
connection.connect()
1
6、获取请求数据
1
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);
}
}
}
1234567891011
7、关闭连接
1
br.close();
1
POST:
1、创建远程连接
2、设置连接方式(get、post、put。。。)
3、设置连接超时时间
4、设置响应读取时间
5、当向远程服务器传送数据/写数据时,需要设置为true(setDoOutput)
6、当前向远程服务读取数据时,设置为true,该参数可有可无(setDoInput)
7、设置传入参数的格式:(setRequestProperty)
8、设置鉴权信息:Authorization:(setRequestProperty)
9、设置参数
12345678910
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
123456
10、发起请求
11、获取请求数据
12、关闭连接
123
2、common封装好的HttpClient
(1)创建httpClient对象。
1
HttpClient httpClient = new HttpClient();
1
(2)创建以什么方式发送对象
1
PostMethod postMethod = new PostMethod(url);
GetMethod getMethod = new GetMethod(url);
12
(3)设置超时时间
1
//设置Http连接超时为5秒
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
12
(4)设置响应头
1
//设置get请求超时为5秒
getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
//设置请求重试处理,用的是默认的重试处理:请求三次
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
//设置json格式传送
postMethod.addRequestHeader("Content-Type", "application/json;charset=GBK");
//必须设置下面这个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");
12345678910111213
(6)设置参数
1
//post添加请求参数
postMethod.addParameter("commentId", json.getString("commentId"));
12
(6)请求
1
int code = httpClient.executeMethod(postMethod);
// int statusCode = httpClient.executeMethod(getMethod);
12
(7)获取响应
1
//get
byte[] responseBody = getMethod.getResponseBody();
response = new String(responseBody, charset);
System.out.println("-----------response:" + response)
//post
int code = httpClient.executeMethod(postMethod);
if (code == 200){
res = postMethod.getResponseBodyAsString();
System.out.println(res);
}
12345678910111213
3、通过Apache封装好的CloseableHttpClient
(1)创建HttpClient对象。
1
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
1
(2)创建以什么方式发送的对象。
1
HttpGet httpGet = new HttpGet(url);// HttpPost httpPost = new HttpPost(url);
1
(3)设置header
1
httpPost.addHeader("api_gateway_auth_token", tokenString);
httpPost.addHeader("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");
//get同样
123456
(4)设置参数
(一)get
//没有,直接在连接上
(二)post
1234
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding("UTF-8");
//发送json数据需要设置contentType
se.setContentType("application/x-www-form-urlencoded");
//设置请求参数
httpPost.setEntity(se);
1234567
(5)执行。
1
HttpResponse response = httpClient.execute(httpGet)
//HttpResponse response = httpClient.execute(httpPost);
12
(6)获取响应。
1
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//返回json格式
String res = EntityUtils.toString(response.getEntity());
return res;
}
12345