前言
package com.example.httpSimulate;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.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.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.URI;
import java.util.*;
/**
* http请求工具
*/
public class HttpUtil {
// private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
/**
* 发送post请求携带json数据
* @param URL
* @param json
* @param headerMap
* @return
*/
public static String sendPostJson(String URL, JSONObject json, HashMap<String,String> headerMap) {
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(URL);
Iterator<Map.Entry<String, String>> iterator = headerMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> next = iterator.next();
post.addHeader(next.getKey(),next.getValue());
}
String result;
try {
StringEntity s = new StringEntity(json.toString(), "utf-8");
s.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
post.setEntity(s);
// 发送请求
HttpResponse httpResponse = client.execute(post);
System.out.println(httpResponse.getEntity().toString());
// 获取响应输入流
InputStream inStream = httpResponse.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
inStream, "utf-8"));
StringBuilder strber = new StringBuilder();
String line;
while ((line = reader.readLine()) != null)
strber.append(line + "\n");
inStream.close();
result = strber.toString();
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
System.out.println("请求服务器成功,做相应处理");
} else {
System.out.println("请求服务端失败");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return result;
}
/**
* @param url 访问地址
* @param headerMap header 参数;可以通过下面工具类将string类型转换成map
* @param contentMap 需要传输参数参数;对象可以通过json转换成map
* @return 返回网页返回的数据
*/
public static String sendPostForm(String url, Map<String, String> headerMap, Map<String, String> contentMap) {
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
// 设置请求头
// post.addHeader("Content-Type", "application/x-www-form-urlencoded");
List<NameValuePair> content = new ArrayList<NameValuePair>();
//将content生成entity
Iterator iterator = contentMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> elem = (Map.Entry<String, String>) iterator.next();
content.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
}
CloseableHttpResponse response = null;
try {
//循环增加header
Iterator headerIterator = headerMap.entrySet().iterator();
while (headerIterator.hasNext()) {
Map.Entry<String, String> elem = (Map.Entry<String, String>) headerIterator.next();
post.addHeader(elem.getKey(), elem.getValue());
}
if (content.size() > 0) {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(content, "UTF-8");
post.setEntity(entity);
}
//发送请求并接收返回数据
response = httpClient.execute(post);
if (response != null && response.getStatusLine().getStatusCode() == 200) {
//获取response的body部分
HttpEntity entity = response.getEntity();
//读取reponse的body部分并转化成字符串
result = EntityUtils.toString(entity);
}
return result;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 发送get请求
* @param url 请求URL
* @param param 请求参数 key:value url携带参数 或者无参可不填
* @return
*/
public static String doGet(String url, Map<String, String> param) {
// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
String resultString = "";
CloseableHttpResponse response = null;
try {
// 创建uri
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();
// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);
// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
}