最近在使用httpclient,我也是初学,把一些用法和心得和大家分享下,共同学习,如果使用httpclient 需要依赖httpclient_*.jar 官方下载地址 下载下来将lib下的jar导入工程。 以下代码使用httpclient-4.5.1。 1.使用httpclient 发送Get和Post请求 Get 请求 CloseableHttpClient httpclient = HttpClients.createDefault(); //get 请求 CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpGet httpGet = new HttpGet("http://www.baidu.com"); System.out.println("Executing get request " + httpGet.getURI()); CloseableHttpResponse response = httpclient.execute(httpGet); try { System.out.println(response.getStatusLine()); //返回请求的响应码 httpGet.abort(); } finally { response.close(); } } finally { httpclient.close(); } //Post 请求 CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpPost httpPost = new HttpPost("http://www.baidu.com"); List<NameValuePair> pairList = new ArrayList<NameValuePair>(); pairList.add(new BasicNameValuePair("参数名","参数值")); System.out.println("Executing get request " + httpPost.getURI()); CloseableHttpResponse response = httpclient.execute(httpPost); try { System.out.println(response.getStatusLine()); //返回请求的响应码 httpPost.abort(); } finally { response.close(); } } finally { httpclient.close(); }
使用HttpClient4发送 http请求
原创feifei1719 博主文章分类:工具 ©著作权
文章标签 java Java httpClient 文章分类 Java 后端开发
下一篇:使用dom4j 读写xml文件
![](https://ucenter.51cto.com/images/noavatar_middle.gif)
-
HttpClient4之模拟http请求
Java code?12345678910111213141516171819202122232425262728293031323334353637383940414243444546...
HttpClient4之模拟http请求 Http apache java 实例化 -
HttpClient4登陆有验证码的网站
其实就这个问题,本来是很简单的,我自己花了近两个下午才搞定,现在记录一下。也希望能
java apache 验证码 -
HttpClient4 Post/Put XML到一个服务器上
1、参数名方式POST XML数据import org.apache.http.*; import org.apache.http.clientlient.Default...
XML Apache Servlet Java JavaEE -
客户端HttpClient4处理 Servlet Gzip后的内容
1、服务器端servlet代码:protected void doPost(HttpServletRequest request, HttpServletResponse respo
httpClient http协议 apache 拦截器 客户端