.BasicNameva luePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
public class Test2 {
? ? public static void main(String[] args) throws Exception {
? ? ? ? DefaultHttpClient httpclient = new DefaultHttpClient();? ? ? //实例化一个HttpClient
? ? ? ? HttpResponse response = null;
? ? ? ? HttpEntity entity = null;
? ? ? ? httpclient.getParams().setParameter(
? ? ? ? ? ? ? ? ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);? //设置cookie的兼容性
? ? ? ? HttpPost httpost = new HttpPost("http://127.0.0.1:8080/pub/jsp/getInfo");? ? ? ? ? //引号中的参数是:servlet的地址
? ? ? ? List nvps = new ArrayList ();? ? ? ? ? ? ? ? ? ?
? ? ? ? nvps.add(new BasicNameva luePair("jqm", "fb1f7cbdaf2bf0a9cb5d43736492640e0c4c0cd0232da9de"));?
? ? ? ? //? BasicNameva luePair("name", "value"), name是post方法里的属性, value是传入的参数值
? ? ? ? nvps.add(new BasicNameva luePair("sqm", "1bb5b5b45915c8"));
? ? ? ? httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));? ? ? ? ? ? //将参数传入post方法中
? ? ? ? response = httpclient.execute(httpost);? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //执行
? ? ? ? entity = response.getEntity();? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //返回服务器响应
? ? ? ? try{
? ? ? ? ? ? System.out.println("----------------------------------------");
? ? ? ? ? ? System.out.println(response.getStatusLine());? ? ? ? ? ? ? ? ? ? ? ? ? //服务器返回状态
? ? ? ? ? ? Header[] headers = response.getAllHeaders();? ? ? ? ? ? ? ? ? ? //返回的HTTP头信息
? ? ? ? ? ? for (int i=0; i
? ? ? ? ? ? System.out.println(headers[i]);
? ? ? ? ? ? }
? ? ? ? ? ? System.out.println("----------------------------------------");
? ? ? ? ? ? String responseString = null;
? ? ? ? ? ? if (response.getEntity() != null) {
? ? ? ? ? ? responseString = EntityUtils.toString(response.getEntity());? ? ? / /返回服务器响应的HTML代码
? ? ? ? ? ? System.out.println(responseString);? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //打印出服务器响应的HTML代码
? ? ? ? ? ? }
? ? ? ? } finally {
? ? ? ? ? ? if (entity != null)? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? entity.consumeContent();? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // release connection gracefully
? ? ? ? }
? ? ? ? System.out.println("Login form get: " + response.getStatusLine());
? ? ? ? if (entity != null) {
? ? ? ? entity.consumeContent();
? ? ? ? }
? ? ?
? ? }
}
HttpClient4.0 学习实例 - 页面获取
HttpClient 4.0出来不久,所以网络上面相关的实例教程不多,搜httpclient得到的大部分都是基于原 Commons HttpClient 3.1 (legacy) 包的,官网下载页面:http://hc.apache.org/downloads.cgi,如果大家看了官网说明就明白httpclient4.0是从原包分支出来独立成包的,以后原来那个包中的httpclient不会再升级,所以以后我们是用httpclient新分支,由于4.0与之前的3.1包结构以及接口等都有较大变化,所以网上搜到的实例大部分都是不适合4.0的,当然,我们可以通过那些实例去琢磨4.0的用法,我也是新手,记录下学习过程方便以后检索
本实例我们来获取抓取网页编码,内容等信息
默认情况下,服务器端会根据客户端的请求头信息来返回服务器支持的编码,像google.cn他本身支持utf-8,gb2312等编码,所以如果你在头部中不指定任何头部信息的话他默认会返回gb2312编码,而如果我们在浏览器中直接访问google.cn,通过httplook,或者firefox 的firebug插件查看返回头部信息的话会发现他返回的是UTF-8编码
下面我们还是看实例来解说吧,注释等我也放代码里面解释,放完整代码,方便新手理解
本实例将
使用的httpclient相关包
httpclient-4.0.jar
httpcore-4.0.1.jar
httpmime-4.0.jar
commons-logging-1.0.4.jar等其它相关包
// HttpClientTest.java
package com.baihuo.crawler.test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
class HttpClientTest {
? ? public final static void main(String[] args) t