1.get请求
public static String httpGet(String url) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setSocketTimeout(5000)
.setRedirectsEnabled(true)
.build();
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(requestConfig);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
String srtResult = EntityUtils.toString(entity);
httpClient.close();
return srtResult;
}
2.使用BasicNameValuePair模拟表单发送post请求
/**
* List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
* list.add(new BasicNameValuePair("age", "20")); //请求参数(只能为字符串)
* list.add(new BasicNameValuePair("name", "zhangsan")); //请求参数
* @param url
* @param list
* @return
*/
public static String httpPost(String url,List<BasicNameValuePair> list) throws IOException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"UTF-8");
post.setEntity(entity);
CloseableHttpResponse response = httpClient.execute(post);
response.getStatusLine().getStatusCode();
HttpEntity news = response.getEntity();
String str = EntityUtils.toString(news, "utf-8");
httpClient.close();
return str;
}
3.发送xml类型的post请求
/**
*
* @param url
* @param xmlData (xml字符串)
* @return result (String字符串)
* @throws Exception
*/
public String httpPost(String url, String xmlData) throws Exception {
HttpClient client=HttpClients.createDefault();
HttpPost post = new HttpPost(url);
List<BasicNameValuePair> parameters = new ArrayList();
parameters.add(new BasicNameValuePair("xml", xmlData));
post.setEntity(new UrlEncodedFormEntity(parameters,"UTF-8"));
HttpResponse response = client.execute(post);
System.out.println(response.toString());
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "UTF-8");
return result;
}
4.发送JSONObject参数的post请求
/**
* JSONObject jsonParam = new JSONObject();
* jsonParam.put("name", "admin");
* jsonParam.put("pass", "123456");
* @param url
* @param jsonParam
* @return
* @throws IOException
*/
public static String httpPost(String url,JSONObject jsonParam) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
post.setEntity(entity);
CloseableHttpResponse response = httpClient.execute(post);
HttpEntity news = response.getEntity();
String str = EntityUtils.toString(news, "utf-8");
httpClient.close();
return str;
}
5.第二种方式创建http请求
public static String httpGet(String url) throws IOException {
CloseableHttpClient httpClient= HttpClients.createDefault();
HttpGet httpGet=new HttpGet(url);
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
CloseableHttpResponse response=httpClient.execute(httpGet);
HttpEntity entity=response.getEntity();
response.close();
httpClient.close();
return EntityUtils.toString(entity, "utf-8");
}