`
收藏列表
标题 标签 来源
commons-httpclient3.1 get提交返回文本不正确 commons-httpclient3.1
package com.ssy.httpclient;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class CommonsHttpClient3_get百度 {
	/*
	 * 第二方案(在上面的例子中页面http://www.imobile.com.cn/simcard.php需要一个参数是simcard,
	 * 这个参数值为手机号码段,即手机号码的前七位,服务器会返回提交的手机号码对应的省份、城市以及其他详细信息。
	 * GET的提交方法只需要在URL后加入参数信息, 而POST则需要通过NameValuePair类来设置参数名称和它所对应的值。)
	 * 正确地:http://haoma.imobile.com.cn/index.php?mob=1553540
	 */
	public static String simple2() {
		HttpClient client = new HttpClient();
		client.getHostConfiguration().setHost("www.imobile.com.cn", 80, "http");
		HttpMethod method = getPostMethod(); // 使用 POST 方式提交数据
		String response = "";
		try {
			client.executeMethod(method); // 打印服务器返回的状态
			response = new String(method.getResponseBodyAsString().getBytes(
					"8859_1"));
			System.out.println(response + "---response");
		} catch (IOException e) {
			e.printStackTrace();
		}

		// 打印返回的信息
		method.releaseConnection();
		return response;
	}

	private static HttpMethod getGetMethod() {
		return new GetMethod("/simcard.php?simcard=1330227");
	}

	private static HttpMethod getPostMethod() {
		PostMethod post = new PostMethod("/simcard.php");
		NameValuePair simcard = new NameValuePair("simcard", "1330227");
		post.setRequestBody(new NameValuePair[] { simcard });
		return post;
	}

	// 第一方案
	public static String simple1() {
		HttpClient client = new HttpClient();
		String message = "";
		// 设置代理服务器地址和端口
		// client.getHostConfiguration().setProxy("proxy_host_addr",proxy_port);
		// 使用 GET 方法 ,如果服务器需要通过 HTTPS 连接,那只需要将下面 URL 中的 http 换成 https
		HttpMethod method = new GetMethod("http://java.sun.com");

		// 使用POST方法
		// HttpMethod method = new PostMethod("http://java.sun.com");
		try {
			client.executeMethod(method);
			message = method.getResponseBodyAsString();
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 打印服务器返回的状态
		System.out.println(method.getStatusLine() + "\r\n-重定向跳转--message:"
				+ message);

		// 打印返回的信息
		// 释放连接
		method.releaseConnection();
		return message;
	}
	public static String getHttpClientRespon(String url) {
		HttpClient client = new HttpClient();
		HttpMethod httpmethod = new GetMethod(url);
		String host="m.baidu.com";
//		httpmethod.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml");
		httpmethod.setRequestHeader("Connection", "keep-alive");
		httpmethod.setRequestHeader("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");
		httpmethod.setRequestHeader("Accept-Language", "zh-CN,zh;q=0.8");
		httpmethod.setRequestHeader("Host",host);
		httpmethod.setRequestHeader("Referer", "referer");
		httpmethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//		httpmethod.setRequestHeader("Accept-Encoding", "gzip,deflate,sdch");
		
//		client.getState().addCookie(new Cookie(host, "BAIDUID","0E60C61795C19C866B7243C044460EEA:FG=1"));
//		client.getState().addCookie(new Cookie(host, "NBID","0E60C61795C19C860427D881575D077F:FG=1"));
		client.getState().addCookie(new Cookie(host, "locale","zh"));
		client.getState().addCookie(new Cookie(host, "MAPCLICK_TIMECONTROL","2"));
		client.getState().addCookie(new Cookie(host, "BDUSS","3BGbU9sUTdFNHlUTkVOOFY3ajZEMG9lazg0S1J5TC01TWk3aUJ6b3FFZVBPdGxTQVFBQUFBJCQAAAAAAAAAAAEAAAAm5jgUZnNsXzExMTkAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAI-tsVKPrbFSS"));
		client.getState().addCookie(new Cookie(host, "BDRCVFR[feWj1Vr5u3D]","I67x6TjHwwYf0"));
		client.getState().addCookie(new Cookie(host, "H_PS_PSSID","4299_1439_4634_4414_4261_4588"));
		
		String message = "";
		try {
			client.executeMethod(httpmethod);
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		try {
//			 1,读流
			StringBuffer html=new StringBuffer();
			 //======压缩流
			InputStream inputstream=httpmethod.getResponseBodyAsStream();
//			int tt=readInt(inputstream);   529205248   50128字节
			 GZIPInputStream gis = new GZIPInputStream(inputstream);
			 String tmp=null;
			 byte[] bytes = new byte[1024];
			 while((gis.read(bytes))!=-1){
				 html.append(new String(bytes));
			 }
			 message=html.toString();
			 
			 
//			2,直接返回文本
//			message=new String(httpmethod.getResponseBodyAsString().getBytes("iso8859-1"),"gbk");
			
			System.out.println(message);
		} catch (Exception e) {
			e.printStackTrace();
		}
		httpmethod.releaseConnection();
		return message;
	}
	public static int readInt(InputStream in) throws IOException {
        int ch1 = in.read();
        int ch2 = in.read();
        int ch3 = in.read();
        int ch4 = in.read();
        if ((ch1 | ch2 | ch3 | ch4) < 0)
            throw new EOFException();
        return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
}
	public static void main(String[] args) {
		 String url ="http://m.baidu.com/s?uid=YiS1iYii2ag1aHi7_avVa_ue28_au2ai_uvpig8KviqGuHi3_a288Yup2ijjuviIQSSjC&tn=native&native_api=1&abi=armeabi-v7a&psize=2&usertype=0&from=1092a&ver=16782103&network=WF&operator=&country=CN&pkname=com.baidu.appsearch&gms=false&cen=cuid_cut_cua_uid&platform_version_id=15&firstdoc=&pu=cua%40_PvjhYa6vhIDJEjPkJAiC_hX2NgUI2iKAWF4B%2Cosname%40baiduappsearch%2Cctv%401%2Ccfrom%401092a%2Ccuid%40YiS1iYii2ag1aHi7_avVa_ue28_au2ai_uvpig8Kvi6muviJ0a2gi_ue-8_cuBipguv9i9FyB%2Ccut%400ks9RgaU2ikBI2N0_hvUhguiLjN3pxjKfOFGC%2Ccsrc%40app_box_txt&word=%E5%8D%A1&st=10a001&language=zh&apn=&&native_api=1&pn=0&f=search";
		 getHttpClientRespon(url);

	}
}

		 
		 
		 
		 
		 
		 
		 
		 
		 
		 

		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
Global site tag (gtag.js) - Google Analytics