public static String doGet(String url,Map<String, String> params){
CloseableHttpClient httpclient = HttpClients.createDefault();
String resultString = "";
CloseableHttpResponse response = null;
try {
URIBuilder builder = new URIBuilder(url);
if(null!=params){
for (String key:params.keySet()) {
builder.setParameter(key, params.get(key));
}
}
HttpGet get = new HttpGet(builder.build());
response = httpclient.execute(get);
System.out.println(response.getStatusLine());
if(200==response.getStatusLine().getStatusCode()){
HttpEntity entity = response.getEntity();
resultString = EntityUtils.toString(entity, "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(null!=response){
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null!=httpclient){
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return resultString;
}
public static String doPost(String url,Map<String,String> params){
CloseableHttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy( new LaxRedirectStrategy()).build();
String resultString = "";
CloseableHttpResponse response = null;
try {
HttpPost post = new HttpPost(url);
List<NameValuePair> paramaters = new ArrayList<>();
if(null!=params){
for (String key : params.keySet()) {
paramaters.add(new BasicNameValuePair(key,params.get(key)));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity (paramaters);
post.setEntity(formEntity);
}
post.addHeader("user-agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
response = httpclient.execute(post);
System.out.println(response.getStatusLine());
if(200==response.getStatusLine().getStatusCode()){
HttpEntity entity = response.getEntity();
resultString = EntityUtils.toString(entity, "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(null!=response){
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null!=httpclient){
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return resultString;
}
package com.li.modul.joinDemo.util;
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.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.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpClientUtils{
public static String doPostByForm(String url, Map<String,String> headers, Map<String,Object> params) {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
HttpPost httpPost = new HttpPost(url);
if (params != null) {
List<NameValuePair> paramList = new ArrayList<>();
for (String key : params.keySet()) {
paramList.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "UTF-8");
entity.setContentEncoding("UTF-8");
httpPost.setEntity(entity);
}
if (headers != null && headers.size() > 0) {
for (String key : headers.keySet()) {
httpPost.addHeader(key, headers.get(key));
}
}
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
public static String doPostByJson(String url,Map<String,String> headers, String json) {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try{
HttpPost httpPost = new HttpPost(url);
StringEntity requestEntity = new StringEntity(json,"utf-8");
requestEntity.setContentEncoding("UTF-8");
httpPost.setHeader("Content-type", "application/json");
httpPost.setEntity(requestEntity);
if (headers != null && headers.size() > 0) {
for (String key : headers.keySet()) {
httpPost.addHeader(key, headers.get(key));
}
}
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
catch(Exception e)
{
e.printStackTrace();
}
finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
}