1.使用HttpClient发送get请求(不带参数)
import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import org.apache.http.client.methods.HttpGet;import org.apache.http.HttpResponse;import org.apache.http.HttpEntity;// 创建一个HttpClient实例CloseableHttpClient httpClient = null;try{ httpClient = HttpClients.createDefault(); // 创建一个HttpGet实例 HttpGet httpGet = new HttpGet("http://127.0.0.1:8080/test/demo"); // 发送GET请求 HttpResponse execute = httpClient.execute(httpGet); //获取状态码 int statusCode = execute.getStatusLine().getStatusCode(); System.out.println(statusCode); HttpEntity entity = execute.getEntity(); String resutlt = EntityUtils.toString(entity, "utf-8"); System.out.println(resutlt);}catch (Exception e) { e.printStackTrace();}finally{ if (httpClient != null) { try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } }}
2.使用HttpClient发送get请求(带参数)
import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.NameValuePair;import org.apache.http.util.EntityUtils;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.Consts;import org.apache.http.client.methods.HttpGet;import org.apache.http.HttpResponse;import org.apache.http.HttpEntity;import java.util.ArrayList;import java.util.List;// 创建一个HttpClient实例CloseableHttpClient httpClient = null;try{ httpClient = HttpClients.createDefault(); // 装填参数 Listparams = new ArrayList (); params.add(new BasicNameValuePair("id", "1")); //字符串参数 // 转换为键值对 String str = EntityUtils.toString(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); // 创建一个HttpGet实例 HttpGet httpGet = new HttpGet("http://127.0.0.1:8080/test/demo?"+str); // 发送GET请求 HttpResponse execute = httpClient.execute(httpGet); //获取状态码 int statusCode = execute.getStatusLine().getStatusCode(); System.out.println(statusCode); HttpEntity entity = execute.getEntity(); String resutlt = EntityUtils.toString(entity, "utf-8"); System.out.println(resutlt);}catch (Exception e) { e.printStackTrace();} finally { if (httpClient != null) { try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } }}
3.使用HttpClient发送post请求
import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.NameValuePair;import org.apache.http.util.EntityUtils;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.Consts;import org.apache.http.client.methods.HttpPost;import org.apache.http.HttpResponse;import org.apache.http.HttpEntity;import java.util.ArrayList;import java.util.List;// 创建HttpClient实例CloseableHttpClient httpClient = null;// 创建HttpPost实例HttpPost httpPost = new HttpPost("http://127.0.0.1/test/demo");// 装填参数Listparams = new ArrayList ();params.add(new BasicNameValuePair("id", "1")); //字符串参数 可以循环添加多个try { httpClient = HttpClients.createDefault(); // 设置请求参数 httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8")); // 设置header信息 httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0"); // 执行请求 HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity resEntity = httpResponse.getEntity(); // 按指定编码转换结果实体为String类型 String result = EntityUtils.toString(resEntity, "utf-8"); System.out.println(result);} catch (Exception e) { e.printStackTrace();} finally { if (httpClient != null) { try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } }}
参考:http://blog.csdn.net/xiaoxian8023/article/details/49863967
maven 下载包
<dependency>
<groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency>转载请注明来源:https://my.oschina.net/u/3183495/blog/1574130