package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import org.;
import org.;
import .*;
import ;
import .*;
/**
* http请求工具
*/
public class HttpUtil {
// private static final Logger logger = ();
/**
* 发送post请求携带json数据
* @param URL
* @param json
* @param headerMap
* @return
*/
public static String sendPostJson(String URL, JSONObject json, HashMap<String,String> headerMap) {
CloseableHttpClient client = ();
HttpPost post = new HttpPost(URL);
Iterator<<String, String>> iterator = ().iterator();
while (()) {
<String, String> next = ();
((),());
}
String result;
try {
StringEntity s = new StringEntity((), "utf-8");
(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
(s);
// 发送请求
HttpResponse httpResponse = (post);
(().toString());
// 获取响应输入流
InputStream inStream = ().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
inStream, "utf-8"));
StringBuilder strber = new StringBuilder();
String line;
while ((line = ()) != null)
(line + "\n");
();
result = ();
if (().getStatusCode() == HttpStatus.SC_OK) {
("请求服务器成功,做相应处理");
} else {
("请求服务端失败");
}
} 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 = ();
HttpPost post = new HttpPost(url);
// 设置请求头
// ("Content-Type", "application/x-www-form-urlencoded");
List<NameValuePair> content = new ArrayList<NameValuePair>();
//将content生成entity
Iterator iterator = ().iterator();
while (()) {
<String, String> elem = (<String, String>) ();
(new BasicNameValuePair((), ()));
}
CloseableHttpResponse response = null;
try {
//循环增加header
Iterator headerIterator = ().iterator();
while (()) {
<String, String> elem = (<String, String>) ();
((), ());
}
if (() > 0) {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(content, "UTF-8");
(entity);
}
//发送请求并接收返回数据
response = (post);
if (response != null && ().getStatusCode() == 200) {
//获取response的body部分
HttpEntity entity = ();
//读取reponse的body部分并转化成字符串
result = (entity);
}
return result;
} catch (UnsupportedEncodingException e) {
();
} catch (ClientProtocolException e) {
();
} catch (IOException e) {
();
} finally {
try {
();
if (response != null) {
();
}
} catch (IOException e) {
();
}
}
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 = ();
String resultString = "";
CloseableHttpResponse response = null;
try {
// 创建uri
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : ()) {
(key, (key));
}
}
URI uri = ();
// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);
// 执行请求
response = (httpGet);
// 判断返回状态是否为200
if (().getStatusCode() == 200) {
resultString = ((), "UTF-8");
}
} catch (Exception e) {
();
} finally {
try {
if (response != null) {
();
}
();
} catch (IOException e) {
();
}
}
return resultString;
}
}