httpclient的get/post请求
2018-02-25 00:00
288 查看
1.使用httpclient执行get请求
2.使用httpclient执行带参数的get请求
3. 使用httpclient执行post请求
4.使用httpclient执行带参数的post请求
@Test public void doGet() throws Exception { //创建一个httpclient对象 CloseableHttpClient httpClient = HttpClients.createDefault(); //创建一个GET对象 HttpGet get = new HttpGet("http://www.sogou.com"); //执行请求 CloseableHttpResponse response = httpClient.execute(get); //取响应的结果 int statusCode = response.getStatusLine().getStatusCode(); System.out.println(statusCode); HttpEntity entity = response.getEntity(); String string = EntityUtils.toString(entity, "utf-8"); System.out.println(string); //关闭httpclient response.close(); httpClient.close(); }
2.使用httpclient执行带参数的get请求
@Test public void doGetWithParam() throws Exception{ //创建一个httpclient对象 CloseableHttpClient httpClient = HttpClients.createDefault(); //创建一个uri对象 URIBuilder uriBuilder = new URIBuilder("http://www.sogou.com/web"); uriBuilder.addParameter("query", "花千骨"); HttpGet get = new HttpGet(uriBuilder.build()); //执行请求 CloseableHttpResponse response = httpClient.execute(get); //取响应的结果 int statusCode = response.getStatusLine().getStatusCode(); System.out.println(statusCode); HttpEntity entity = response.getEntity(); String string = EntityUtils.toString(entity, "utf-8"); System.out.println(string); //关闭httpclient response.close(); httpClient.close(); }
3. 使用httpclient执行post请求
@Test public void doPost() throws Exception { CloseableHttpClient httpClient = HttpClients.createDefault(); //创建一个post对象 HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.html"); //执行post请求 CloseableHttpResponse response = httpClient.execute(post); String string = EntityUtils.toString(response.getEntity()); System.out.println(string); response.close(); httpClient.close(); }
4.使用httpclient执行带参数的post请求
@Test public void doPostWithParam() throws Exception{ CloseableHttpClient httpClient = HttpClients.createDefault(); //创建一个post对象 HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.html"); //创建一个Entity。模拟一个表单 List<NameValuePair> kvList = new ArrayList<>(); kvList.add(new BasicNameValuePair("username", "zhangsan")); kvList.add(new BasicNameValuePair("password", "123")); //包装成一个Entity对象 StringEntity entity = new UrlEncodedFormEntity(kvList, "utf-8"); //设置请求的内容 post.setEntity(entity); //执行post请求 CloseableHttpResponse response = httpClient.execute(post); String string = EntityUtils.toString(response.getEntity()); System.out.println(string); response.close(); httpClient.close(); }
相关文章推荐
- HttpClient4.x进行Get/Post请求并使用ResponseHandler处理响应
- apache httpclient新版本的get/post请求
- HttpClient4.x进行Get/Post请求并使用ResponseHandler处理响应
- HttpClient4.x进行Get/Post请求并使用ResponseHandler处理响应
- org.apache.http.client.HttpClient get/post请求
- AsyncHttpClient 、HttpURLConnection get/post请求、httpClient
- httpclient post请求例子(无参数名与带参数名的例子),多线程并发处理
- Http的Get/Post请求带有特殊字符,400 Bad Request解决方案
- Java用org.apache.http.client的HttpClient发送Post请求
- 用HttpClient发送get,post请求的工具类
- Java用HttpClient3发送Get和Post请求(增强)
- HttpClient发送get post请求和数据解析
- HttpComponent-HttpClient使用-发送Post请求
- java实现的利用HTTP协议原理实现的GET/POST请求的web服务器
- httpClient post/get请求。
- HttpClient的POST和GET请求和Session的保持-yellowcong
- Android—Http连接之GET/POST请求
- 利用HttpClient发送post请求京东接口并将结果用POI导出为 Excel表格
- get和post请求(HttpUrlConnection、HttpClient、AsyncHttpClient)多线程下载,断点续传
- 【Angular】angular-HttpClient 与服务器通讯 Get Post Put Delete Http 拦截器 请求讲解