Apache HttpComponents Client 4.0快速入门/升级-2.POST方法访问网页
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方法访问网页的更多相关文章
- python3.5+django2.0快速入门(二)
昨天写了python3.5+django2.0快速入门(一)今天将讲解配置数据库,创建模型,还有admin的后台管理. 配置数据库 我们打开mysite/mysite/settings.py这个文件. ...
- ExtJs 6.0+快速入门,ext-bootstrap.js文件的分析,各版本API下载
ExtJS6.0+快速入门+API下载地址 ExtAPI 下载地址如下,包含各个版本 http://docs.sencha.com/misc/guides/offline_docs.html 1.使用 ...
- python3.5+django2.0快速入门(一)
因为这篇教程需要用到anaconda的一些操作,如果还不懂anaconda的操作的同学可以看下这篇文章python 入门学习之anaconda篇. 创建python3+的开发环境 直接在终端输入:co ...
- TensorFlow 2.0 快速入门指南 | iBooker·ApacheCN
原文:TensorFlow 2.0 Quick Start Guide 协议:CC BY-NC-SA 4.0 自豪地采用谷歌翻译 不要担心自己的形象,只关心如何实现目标.--<原则>,生活 ...
- JAVA中使用Apache HttpComponents Client的进行GET/POST请求使用案例
一.简述需求 平时我们需要在JAVA中进行GET.POST.PUT.DELETE等请求时,使用第三方jar包会比较简单.常用的工具包有: 1.https://github.com/kevinsawic ...
- Thinkphp5.0快速入门笔记(3)
学习来源与说明 https://www.kancloud.cn/thinkphp/thinkphp5_quickstart 测试与部署均在windows10下进行学习. 快速入门第三节 获取当前的请求 ...
- Thinkphp5.0快速入门笔记(2)
学习来源与说明 https://www.kancloud.cn/thinkphp/thinkphp5_quickstart 测试与部署均在windows10下进行学习. 示例建立新的模块和控制器 在a ...
- rancher2.0快速入门
注意:本入门指南的目的是让您快速的运行一个Rancher2.0环境,它不适用于生产.有关更全面的说明,请查阅Rancher安装. 本教程将指导您完成: 安装Rancher v2.0 : 创建第一个集群 ...
- Thinkphp5.0快速入门笔记(1)
学习来源与说明 https://www.kancloud.cn/thinkphp/thinkphp5_quickstart 测试与部署均在windows10下进行学习. Composer安装和更新 C ...
随机推荐
- GSON 简介 示例
Gson简介 目前解析json最常用的三种工具:org.json(Java常用的解析),fastjson(阿里巴巴出的),Gson(Google出的),解析速度最快的是Gson. Gson的全名为Go ...
- spring依赖注入源码分析和mongodb自带连接本地mongodb服务逻辑分析
spring依赖注入本质是一个Map结构,key是beanId,value是bean对应的Object. autowired是怎么将定义的接口与对应的bean类建立联系? <bean name= ...
- 熟悉java堆内存和栈内存和mysql的insert语句中含有id的处理
java的堆内存和栈内存有什么区别呢? 如果mysql数据库表的id是递增的,如果没有插入id,则id自增,如果插入id,则插入什么就显示什么.
- 获取android手机联系人信息
package com.yarin.android.Examples_04_04; import android.app.Activity; import android.database.Curso ...
- QQ群开放接口
http://qun.qq.com/open.html#click http://my.oschina.net/ij2ee/blog/191692
- SQL2008 存储过程参数相关
使用inputparame时,使用的是 varchar(20),和数据库中的DEPARTNAME完全匹配,可以查出值: USE [test] GO SET ANSI_NULLS OFF GO SE ...
- Python 快捷键
Ctrl + [ .Ctrl + ] 缩进代码Alt+3 Alt+4 注释.取消注释代码行Alt+5 Alt+6 切换缩进方式 空格<=>TabAlt+/ 单词完成,只要文中出现过,就可以 ...
- Mysql 查询性能优化
查询优化,索引优化,库表结构优化需要齐头并进,一个不能落. 为什么查询速度会慢 在阐释编写快速的查询之前,需要清楚一点,真正重要的是响应时间.如果把查询看做是一个任务的话,那么它由一系列子任务构成,每 ...
- 趣味PAT--循环-19. 币值转换(20)
One visible minute on the stage is attributed to ten years of invisible practice off the stage. &quo ...
- memcache的使用
什么是memcache? Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像.视频.文件以及数据库检索的结果等. ...