Apache HttpComponents Client 4.0快速入门/升级-2.POST方法访问网页(转)

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();
}
}
}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/inkfish/archive/2009/11/02/4757754.aspx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值