大家好,欢迎来到IT知识分享网。
一,介绍
创建一个HttpRequest类,该类负责向服务器发起HTTP GET, POST请求。该类的对象使用单例模式来创建,因为对于多个客户端而言,需要发送大量的HTTP请求,而每一个请求都需要调用HttpRequest类的方法向服务器建立连接,因此每一个请求都创建一个HttpRequest对象的话,对资源消耗很大。
二,发送请求
1,无参数的GET请求
public String doGet(String uri){
HttpGet httpGet = new HttpGet(uri);
return sendHttpGet(httpGet);
}
2,带多个参数的GET请求
public String doGet(String uri, List<NameValuePair> parameters){
HttpGet httpGet = new HttpGet(uri);
String param = null;
try{
param = EntityUtils.toString(new UrlEncodedFormEntity(parameters));
//build get uri with params
httpGet.setURI(new URIBuilder(httpGet.getURI().toString() + "?" + param).build());
}catch(Exception e){
e.printStackTrace();
}
return sendHttpGet(httpGet);
}
GET请求的多个参数以NameValuePair类型存储在List列表中。通过EntityUtils类将NameValuePair解析成String变量param,然后再使用URIBuilder类重新构造一个带参数的HTTP GET 请求。
三,发送POST请求
1,无参数的POST请求
public String doPost(String uri){
HttpPost httpPost = new HttpPost(uri);
return sendHttpPost(httpPost);
}
2,带一个参数的POST请求
public String doGet(String uri, String paramName, String paramValue){
HttpGet httpGet = new HttpGet(uri);
//build get uri with params
URIBuilder uriBuilder = new URIBuilder(httpGet.getURI()).setParameter(paramName, paramValue);
try{
httpGet.setURI(uriBuilder.build());
}catch(URISyntaxException e){
e.printStackTrace();
}
return sendHttpGet(httpGet);
}
使用URIBuilder的 setParameter方法为POST URI添加请求参数。当需要带多个参数的POST请求时,可借助Lists<NameValuePair>,参考上面“带多个参数的GET请求”的实现方式。
3,使用POST请求发送XML内容实体。
public String doPost(String uri, String reqXml){
HttpPost httpPost = new HttpPost(uri);
httpPost.addHeader("Content-Type", "application/xml");
StringEntity entity = null;
try{
entity = new StringEntity(reqXml, "UTF-8");
}catch(Exception e){
e.printStackTrace();
}
httpPost.setEntity(entity);//http post with xml data
return sendHttpPost(httpPost);
}
XML内容实体以 String reqXml参数表示。首先为请求添加Header表示内容实体的类型-“application/xml”,然后使用StringEntity类包装内容实体。最后调用HttpPost的setEntity()封装内容实体,并发送。
三,与服务器建立HTTP连接
1,建立GET连接
private String sendHttpGet(HttpGet httpGet){
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
String responseContent = null;
try{
httpClient = HttpClients.createDefault();
// httpGet.setConfig(config);
response = httpClient.execute(httpGet);
entity = response.getEntity();
responseContent = EntityUtils.toString(entity, "UTF-8");
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(response != null)
response.close();
if(httpClient != null)
httpClient.close();
}catch(IOException e){
e.printStackTrace();
}
}
return responseContent;
}
HttpEntity entity 是服务器发回的响应实体,由EntityUtils类的toString()方法 转换字符串后,返回给调用者。
建立POST连接和PUT连接,参考整个完整代码:
package http;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.client.methods.HttpPut;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpRequest {
private static HttpRequest httpRequest = null;
private HttpRequest(){}
public static HttpRequest getInstance(){
if(httpRequest == null){
synchronized (HttpRequest.class) {
if(httpRequest == null)
httpRequest = new HttpRequest();
}
}
return httpRequest;
}
public String doGet(String uri){
HttpGet httpGet = new HttpGet(uri);
return sendHttpGet(httpGet);
}
/*
* only one paramter's http get request
*/
public String doGet(String uri, String paramName, String paramValue){
HttpGet httpGet = new HttpGet(uri);
//build get uri with params
URIBuilder uriBuilder = new URIBuilder(httpGet.getURI()).setParameter(paramName, paramValue);
try{
httpGet.setURI(uriBuilder.build());
}catch(URISyntaxException e){
e.printStackTrace();
}
return sendHttpGet(httpGet);
}
/*
* mulitple paramters of http get request
*/
public String doGet(String uri, List<NameValuePair> parameters){
HttpGet httpGet = new HttpGet(uri);
String param = null;
try{
param = EntityUtils.toString(new UrlEncodedFormEntity(parameters));
//build get uri with params
httpGet.setURI(new URIBuilder(httpGet.getURI().toString() + "?" + param).build());
}catch(Exception e){
e.printStackTrace();
}
return sendHttpGet(httpGet);
}
public String doPost(String uri){
HttpPost httpPost = new HttpPost(uri);
return sendHttpPost(httpPost);
}
public String doPost(String uri, String reqXml){
HttpPost httpPost = new HttpPost(uri);
httpPost.addHeader("Content-Type", "application/xml");
StringEntity entity = null;
try{
entity = new StringEntity(reqXml, "UTF-8");
}catch(Exception e){
e.printStackTrace();
}
httpPost.setEntity(entity);//http post with xml data
return sendHttpPost(httpPost);
}
/*
* multiple http put params
*/
public String doPut(String uri, List<NameValuePair> parameters){
HttpPut httpPut = new HttpPut(uri);
String param = null;
try{
param = EntityUtils.toString(new UrlEncodedFormEntity(parameters));
httpPut.setURI(new URIBuilder(httpPut.getURI().toString() + "?" + param).build());
}catch(Exception e){
e.printStackTrace();
}
return sendHttpPut(httpPut);
}
public String doPut(String uri, List<NameValuePair> parameters, String reqXml){
HttpPut httpPut = new HttpPut(uri);
String param = null;
try{
param = EntityUtils.toString(new UrlEncodedFormEntity(parameters));
httpPut.setURI(new URIBuilder(httpPut.getURI().toString() + "?" + param).build());
}catch(Exception e){
e.printStackTrace();
}
StringEntity entity = null;
try{
entity = new StringEntity(reqXml, "UTF-8");
}catch(Exception e){
e.printStackTrace();
}
httpPut.setEntity(entity);
return sendHttpPut(httpPut);
}
private String sendHttpPost(HttpPost httpPost){
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
String responseContent = null;
try{
httpClient = HttpClients.createDefault();
// httpPost.setConfig(config);
response = httpClient.execute(httpPost);
entity = response.getEntity();
responseContent = EntityUtils.toString(entity, "UTF-8");
}catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(response != null)
response.close();
if(httpClient !=null)
httpClient.close();
}catch(IOException e){
e.printStackTrace();
}
}
return responseContent;
}
private String sendHttpGet(HttpGet httpGet){
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
String responseContent = null;
try{
httpClient = HttpClients.createDefault();
// httpGet.setConfig(config);
response = httpClient.execute(httpGet);
entity = response.getEntity();
responseContent = EntityUtils.toString(entity, "UTF-8");
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(response != null)
response.close();
if(httpClient != null)
httpClient.close();
}catch(IOException e){
e.printStackTrace();
}
}
return responseContent;
}
private String sendHttpPut(HttpPut httpPut){
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
String responseContent = null;
try{
httpClient = HttpClients.createDefault();
// httpPut.setConfig(config);
response = httpClient.execute(httpPut);
entity = response.getEntity();
responseContent = EntityUtils.toString(entity, "UTF-8");
}catch(Exception e){
e.printStackTrace();
}
return responseContent;
}
}
参考:http://tzz6.iteye.com/blog/2224757
http://blog.csdn.net/sunny243788557/article/details/8106265
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/25794.html