欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

http client实践

程序员文章站 2022-03-10 13:27:48
...

1 使用http client

参考网上例子登陆baidu、人人、淘宝

http://robblog.iteye.com/blog/638206

 

官网文档

http://hc.apache.org/httpcomponents-client-ga/examples.html

http://hc.apache.org/httpcomponents-client-ga/tutorial/html/index.html

 

登陆人人代码

 

DefaultHttpClient httpclient = new DefaultHttpClient();
		try {
			HttpGet httpget = new HttpGet("http://www.renren.com");

			HttpResponse response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity();

			System.out.println("Login form get: " + response.getStatusLine());
			EntityUtils.consume(entity);

			System.out.println("Initial set of cookies:");
			List<Cookie> cookies = httpclient.getCookieStore().getCookies();
			if (cookies.isEmpty()) {
				System.out.println("None");
			} else {
				for (int i = 0; i < cookies.size(); i++) {
					System.out.println("- " + cookies.get(i).toString());
				}
			}

			HttpPost httpost = new HttpPost("http://www.renren.com/ajaxLogin/login");
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("_rtk", "6bca5579"));
			nvps.add(new BasicNameValuePair("domain", "renren.com"));
			nvps.add(new BasicNameValuePair("email", "XXXX"));
			nvps.add(new BasicNameValuePair("key_id", "1"));
			nvps.add(new BasicNameValuePair("origURL", "http://www.renren.com/home"));
			nvps.add(new BasicNameValuePair("password", "XXXXXX"));

			httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

			response = httpclient.execute(httpost);
			entity = response.getEntity();

			System.out.println("Login form get: " + response.getStatusLine());
			EntityUtils.consume(entity);

			System.out.println("Post logon cookies:");
			cookies = httpclient.getCookieStore().getCookies();
			if (cookies.isEmpty()) {
				System.out.println("None");
			} else {
				for (int i = 0; i < cookies.size(); i++) {
					System.out.println("- " + cookies.get(i).toString());
				}
			}

		} finally {
			// When HttpClient instance is no longer needed,
			// shut down the connection manager to ensure
			// immediate deallocation of all system resources
			httpclient.getConnectionManager().shutdown();
		}

 有些网站的登录稍微复杂,需要模拟js的行为,向服务端发送进一步的校验信息,如天涯,它对cookie的获取需要通过js多次向服务端发送请求。

对于这类研究,采用http watch工具可以事半功倍.

 

2 使用tcp,通过http watch截获发送包,用socket重发这个包,模拟http请求。

 

相关标签: http