HttpComponents Client 4.0快速入门/升级-2.POST方法访问网页
来源:互联网 发布:胖虎是什么梗 知乎 编辑:程序博客网 时间:2024/05/12 16:47
Apache HttpComponents Client 4.0已经发布多时,httpclient项目从commons子项目挪到了HttpComponents子项目下,httpclient3.1和 httpcilent4.0无法做到代码向后兼容,升级比较麻烦。我在做项目之余找时间研究了一下,写了一套3.1与4.0对比的代码,不求面面俱到,但 求简单易懂。如果代码用到真实项目中,还需要考虑诸如代理、Header、异常处理之类的问题。
Http POST方法得到www.g.cn的源码:
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.commons.httpclient.NameValuePair;
- import org.apache.commons.httpclient.methods.PostMethod;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.ParseException;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.protocol.HTTP;
- import org.apache.http.util.EntityUtils;
- public class PostSample {
- public static void main(String[] args) throws ParseException, IOException {
- String url = "http://www.g.cn/";
- System.out.println(url);
- System.out.println("Visit google using Apache commons-httpclient 3.1:");
- List<NameValuePair> data3 = new ArrayList<NameValuePair>();
- data3.add(new NameValuePair("username", "testuser"));
- data3.add(new NameValuePair("password", "testpassword"));
- System.out.println(post3(url, data3));
- System.out.println("Visit google using Apache HttpComponents Client 4.0:");
- List<BasicNameValuePair> data4 = new ArrayList<BasicNameValuePair>();
- data4.add(new BasicNameValuePair("username", "testuser"));
- data4.add(new BasicNameValuePair("password", "testpassword"));
- System.out.println(post4(url, data4));
- }
- /** 使用Apache commons-httpclient 3.1,POST方法访问网页 */
- public static String post3(String url, List<NameValuePair> data) throws IOException {
- org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
- PostMethod postMethod = new PostMethod(url);
- postMethod.setRequestBody(data.toArray(new NameValuePair[data.size()]));
- try {
- System.out.println("<< Response: " + httpClient.executeMethod(postMethod));
- return postMethod.getResponseBodyAsString();
- } finally {
- postMethod.releaseConnection();
- }
- }
- /** 使用Apache HttpComponents Client 4.0,POST方法访问网页 */
- private static String post4(String url, List<? extends org.apache.http.NameValuePair> data)
- throws ParseException, IOException {
- org.apache.http.client.HttpClient client = new DefaultHttpClient();
- HttpPost httpost = new HttpPost(url);
- httpost.setEntity(new UrlEncodedFormEntity(data, HTTP.UTF_8));
- try {
- HttpResponse response = client.execute(httpost);
- HttpEntity entity = response.getEntity();
- System.out.println("<< Response: " + response.getStatusLine());
- if (entity != null) {
- return EntityUtils.toString(entity);
- }
- return null;
- } finally {
- client.getConnectionManager().shutdown();
- }
- }
- }
当然www.g.cn不必要通过post来访问,一般用于需要提交表单的情形。
- Apache HttpComponents Client 4.0快速入门/升级-2.POST方法访问网页
- HttpComponents Client 4.0快速入门/升级-2.POST方法访问网页
- Apache HttpComponents Client 4.0快速入门/升级-1.GET方法访问网页
- HttpComponents(Apache HttpComponents Client 4.1.3)通过Post方式访问网页或传输参数
- HttpComponents(Apache HttpComponents Client 4.1.3)通过代理访问网页的设置方法
- httpcomponents-client-4.3.5实现http的post请求和get请求方法
- httpcomponents-client-4.3.4 HTTPS地址访问
- Httpclient4.3.5简单get,post实用(httpcomponents-client-4.3.5)
- httpcomponents-client 使用小结
- HttpComponents入门解析
- HttpComponents入门解析
- HttpComponents入门解析
- HttpComponents入门解析
- HttpComponents入门解析
- HttpComponents入门解析
- HttpComponents入门解析
- HttpComponents入门解析
- HttpComponents入门解析
- 《C程序设计》谭浩强
- 博客文章整理
- 为什么Java EE输给了Spring[译]
- HttpClient_4 用法 由HttpClient_3 升级到 HttpClient_4 必看
- Ubuntu下MySQL+DRBD+Heartbeat高性能服务集群配置
- HttpComponents Client 4.0快速入门/升级-2.POST方法访问网页
- 《编程之美》-得到无序数组中最大K个数
- Java新手日常练习
- 天津regional 求RP!!!!!!!
- 分享一道很有意思的算法题目
- WEB4-servlet
- 情以何堪的伤感QQ日志分享:一句话,了断一切
- How to handle the session in HttpClient 4.1
- java设计模式-状态模式(State)