HttpClient之GET请求
2015-11-26 21:57
603 查看
JavaEE或者Android的开发者对HttpClient一定不陌生,实际上这是一个在项目中经常被用到的框架,框架本身用法很简单,网上有很多这方面的资料,这里只是一个简单的使用总结。
当在实际的项目开发中需要作为客户端发送HTTP请求时,HttpClient是个不错的选择,下面简单介绍两种GET请求。
方法一,直接拼接参数
当在实际的项目开发中需要作为客户端发送HTTP请求时,HttpClient是个不错的选择,下面简单介绍两种GET请求。
方法一,直接拼接参数
package com.zws.httpclient; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.junit.Test; public class ClientGet { static String uri = "http://localhost:8080/resteasy/rest/user/"; static String CHAR_SET = "UTF-8"; @Test public void get0() { String path = "getUserDetail2?userId=23&status=1";//直接拼接参数 CloseableHttpClient client = HttpClients.createDefault(); HttpGet get = new HttpGet(getUri(path)); CloseableHttpResponse resp = null; try { resp = client.execute(get); int code = resp.getStatusLine().getStatusCode();//响应码 System.out.println("resp code:" + code); HttpEntity entity = resp.getEntity(); String msg = EntityUtils.toString(entity, CHAR_SET);//响应信息 System.out.println("receive msg:" + msg); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (resp != null) { try { resp.close(); } catch (IOException e) { e.printStackTrace(); } } } } public String getUri(String path) { return uri + path; } }方法二,利用URIBuilder辅助构造URI
package com.zws.httpclient; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import org.apache.http.HttpEntity; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.junit.Test; public class ClientGet { static String uri = "http://localhost:8080/resteasy/rest/user/"; static String CHAR_SET = "UTF-8"; @Test public void get1() { String path = "/resteasy/rest/user/getUserDetail2"; CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse resp = null; try { URIBuilder url = new URIBuilder(); url.setScheme("http"); url.setHost("localhost:8080"); url.setPath(path); url.setParameter("userId", "23"); url.setParameter("status", "1"); URI uri = url.build(); System.out.println("url:" + uri.toString()); HttpGet get = new HttpGet(uri); resp = client.execute(get); int code = resp.getStatusLine().getStatusCode(); System.out.println("resp code:" + code); HttpEntity entity = resp.getEntity(); String msg = EntityUtils.toString(entity, CHAR_SET); System.out.println("receive msg:" + msg); } catch (URISyntaxException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (resp != null) { try { resp.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
相关文章推荐
- GET和post取值限制区别分析
- 深入C#中get与set的详解
- POST与GET方法的区别简要分析
- VB使用XMLHTTP实现Post与Get的方法
- 分享下GET和POST的真正区别
- ASP.NET的HtmlForm控件学习及Post与Get的区别概述
- asp教程中get post提交表单有5点区别
- 区分ASP.NET中get方法和post方法
- php下通过POST还是GET来传值
- jQuery调用AJAX时Get和post公用的乱码解决方法实例说明
- ajax请求get与post的区别总结
- Jquery AJAX POST与GET之间的区别
- jquery中常用的SET和GET
- jQuery中get和post方法传值测试及注意事项
- ASP.NET MVC Web API HttpClient简介
- ajax请求post和get的区别以及get post的选择
- jQuery中ajax - get() 方法实例详解
- JSP、Servlet中get请求和post请求的区别总结
- 深入理解curl类,可用于模拟get,post和curl下载
- jq的get传参数在utf-8中乱码问题的解决php版