本文主要是列举出了HttpClient的简单使用方法,一个是post方法,另一个是get方法。
这里所使用的是HttpClient4.5.jar
以及使用了JSON-lib-2.1.jar
有兴趣的朋友可以在CSDN里搜一下,可以找到
首先post方法,这边我希望传递一个实体类给服务端,所以引用了JSON得方式去传递
先将实体类转换成JSON,再将JSON转换成String字符串型,通过HTTP发送:
package com.HTTPtransfer.test.httpClient;
import java.io.IOException;
import java.nio.charset.Charset;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class HttpClientTool {
/**
* post请求传输String参数
* Content-type:application/json
*
* @param url
* url地址
* @param fileReport
* 文件报表对象
* @param responseStr
* 不需要返回结果
* @return
*/
public static String httpPost(String url, FileReport fileReport) {
// POST请求返回结果
CloseableHttpClient httpClient = HttpClients.createDefault();
JSONObject json = JSONObject.fromObject(fileReport);
HttpPost httpPost = new HttpPost(url);
String responseStr = null;
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
httpPost.setConfig(requestConfig);
try {
if (null != json.toString()) {
StringEntity entity = new StringEntity(json.toString(), Charset.forName("UTF-8"));
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
}
CloseableHttpResponse result = httpClient.execute(httpPost);
if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
try {
// 读取服务器返回过来的json字符串数据
responseStr = EntityUtils.toString(result.getEntity(), "utf-8");
System.out.println(responseStr);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
httpPost.releaseConnection();
}
return responseStr;
}
接下来是GET方法,与POST方法不同的是GET方法是将传递的参数放到访问的URL后;
下面这段代码的红色部分就是所要传递的参数:
/**
* 发送get请求
*
* @param url
* 路径
* @return
*/
public static String httpGet(String url, FileReport fileReport) {
// GET请求返回结果
CloseableHttpClient client = HttpClients.createDefault();
url = url + "/?fileCode=" + fileReport.getFileCode();
url = url + "&bizDate=" + fileReport.getBizDate();
HttpGet httpGet = new HttpGet(url);
String responseStr = null;
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
httpGet.setConfig(requestConfig);
try {
CloseableHttpResponse response = client.execute(httpGet);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
responseStr = EntityUtils.toString(entity, "utf-8");
System.out.println(responseStr);
} else {
}
} catch (IOException e) {
e.printStackTrace();
} finally {
httpGet.releaseConnection();
}
return responseStr;
}
}
在介绍完如何通过HttpClient来参数之后,还有一篇关于在Spring框架中接收HttpClient的POST和GET请求的方法