HttpClient的使用指南
来源:互联网 发布:java中构造方法的使用 编辑:程序博客网 时间:2024/06/15 15:31
使用HttpClient可以在客户端发送Post请求,请求访问服务端的Servlet, 依赖包httpclient和httpcore,http://hc.apache.org/httpcomponents-client-4.4.x/index.html上下载,这里所用的是httpcomponents-client-4.4.1-bin.tar.gz.
public void sendPostMethod(){ DefaultHttpClient httpclient = new DefaultHttpClient(); //参数代表目标地址 HttpPost httppost = new HttpPost("http://172.18.60.2:8080/MyFirstWeb/TestServlet"); // HttpPost参数传递 List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>(); nvps.add(new BasicNameValuePair("user", "chris")); nvps.add(new BasicNameValuePair("name", "benpaoba")); nvps.add(new BasicNameValuePair("age", "25")); try { httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); // 执行请求HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } // 显示结果 BufferedReader reader = new BufferedReader(new InputStreamReader( entity.getContent(), "UTF-8")); String line = null; while ((line = reader.readLine()) != null) { System.out.println(line); } if (entity != null) { entity.consumeContent(); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch blocke.printStackTrace(); } catch (UnsupportedOperationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { httpclient.getConnectionManager().shutdown(); }}
0 0
- HttpClient的使用指南
- HttpClient--HttpClient的Post请求
- paypal ipn的使用指南
- EasyMock2的使用指南(转载)
- Hashmap 的使用指南
- Google Code的使用指南
- 肉鸡的详细使用指南
- Wine的完全使用指南
- erlang的reltool使用指南
- [Test]JMock的使用指南
- virgo的使用指南
- CListCtrl控件的使用指南
- Vim的使用指南
- Eclispe 快捷键的使用指南
- iptables 详细的使用指南
- Androidpn的使用指南
- BaseAdapter的使用指南
- ViewPager的使用指南
- TCP三次握手/四次挥手详解
- [Practical.Vim(2012.9)].Drew.Neil.Tip49 学习摘要
- 24L01
- HDU 2006-2009
- Android中ViewStub组件使用
- HttpClient的使用指南
- 浅谈随机森林在人脸对齐上的应用~
- error: stray ‘\357’ in program
- HDU4115.Eliminate the Conflict(剪刀石头布)——2-sat可行性判断
- 解决Myeclipse/Eclipse使用SVN插件,checkout本地SVN仓库错误
- C语言杂记1
- 简述RFID系统的时隙ALOHA算法的工作过程
- android开发使用greenDAO如何自动生成代码
- RFID系统二进制属性搜索算法是如何解决碰撞的?简述其实现步骤