/**
* HttpClient 模拟发送http请求
* @author zxl
* @date 2018年6月18日上午9:28:41
* @描述:
*
*/
public class HttpClientUtil {
/**
* GET请求
* @param url
* @param params
* @return String
*/
public static String doGet(String url,List<NameValuePair> params){
CloseableHttpClient client=HttpClients.createDefault();
try {
String param=EntityUtils.toString(new UrlEncodedFormEntity(params,Consts.UTF_8));
HttpGet httpGet=new HttpGet(url+"?"+param);
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
HttpResponse response=client.execute(httpGet);
System.out.println("statusCode:"+response.getStatusLine().getStatusCode());
if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
HttpEntity httpEntity=response.getEntity();
String res=EntityUtils.toString(httpEntity, "utf-8");
return res;
}
} catch (ParseException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
/**
* POST请求
* @param url
* @param jsonObject
* @return JSONObject
*/
public static JSONObject doPost(String url,JSONObject jsonObject) {
CloseableHttpClient client=HttpClients.createDefault();
HttpPost httpPost=new HttpPost(url);
System.out.println("jsonObject:"+jsonObject.toString());
StringEntity entity=new StringEntity(jsonObject.toString(), "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
//设置User-Agent,否则请求码403
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
httpPost.setEntity(entity);
HttpResponse httpResponse = null;
try {
httpResponse = client.execute(httpPost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int resultCode=httpResponse.getStatusLine().getStatusCode();
System.out.println("resultCode:"+resultCode);
if(resultCode==200) {
HttpEntity entity2=httpResponse.getEntity();
try {
String result=EntityUtils.toString(entity2, "utf-8");
return JSONObject.parseObject(result);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
}
* HttpClient 模拟发送http请求
* @author zxl
* @date 2018年6月18日上午9:28:41
* @描述:
*
*/
public class HttpClientUtil {
/**
* GET请求
* @param url
* @param params
* @return String
*/
public static String doGet(String url,List<NameValuePair> params){
CloseableHttpClient client=HttpClients.createDefault();
try {
String param=EntityUtils.toString(new UrlEncodedFormEntity(params,Consts.UTF_8));
HttpGet httpGet=new HttpGet(url+"?"+param);
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
HttpResponse response=client.execute(httpGet);
System.out.println("statusCode:"+response.getStatusLine().getStatusCode());
if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
HttpEntity httpEntity=response.getEntity();
String res=EntityUtils.toString(httpEntity, "utf-8");
return res;
}
} catch (ParseException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
/**
* POST请求
* @param url
* @param jsonObject
* @return JSONObject
*/
public static JSONObject doPost(String url,JSONObject jsonObject) {
CloseableHttpClient client=HttpClients.createDefault();
HttpPost httpPost=new HttpPost(url);
System.out.println("jsonObject:"+jsonObject.toString());
StringEntity entity=new StringEntity(jsonObject.toString(), "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
//设置User-Agent,否则请求码403
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
httpPost.setEntity(entity);
HttpResponse httpResponse = null;
try {
httpResponse = client.execute(httpPost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int resultCode=httpResponse.getStatusLine().getStatusCode();
System.out.println("resultCode:"+resultCode);
if(resultCode==200) {
HttpEntity entity2=httpResponse.getEntity();
try {
String result=EntityUtils.toString(entity2, "utf-8");
return JSONObject.parseObject(result);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
}