码迷,mamicode.com
首页 > 编程语言 > 详细

Java利用HttpClient进行网页登录

时间:2014-11-18 13:28:51      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:java   httpclient   get   post   

http://e.neusoft.edu.cn/nav_login

模拟浏览器登录上面这个网站,登录服务器根据验证码、refer和cookie保护措施,验证码在html文件里。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
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 HttpRequst {
	public static void main(String strings[]) 
	{
	    HttpClient httpclient = new DefaultHttpClient();
	    try
	    {
	    	HttpGet getMethod=new HttpGet("http://e.neusoft.edu.cn/nav_login");
	    	getMethod.setHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
	    	HttpResponse response=httpclient.execute(getMethod);
	    	Header arrHeaders[]=response.getAllHeaders();
	    	EntityUtils.consume(response.getEntity());

	    	getMethod=new HttpGet("http://e.neusoft.edu.cn/RandomCodeAction.action?randomNum=0.9179729035059037");
	    	getMethod.setHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
	    	response=httpclient.execute(getMethod);
	    	EntityUtils.consume(response.getEntity());
	    	
	    	
	    	
	    	String checkcode="";
	    	Scanner scan=new Scanner(System.in);
	    	checkcode=scan.next();
	    
	    	HttpPost postMethod=new HttpPost("http://e.neusoft.edu.cn/LoginAction.action");
	    	postMethod.setHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
	    	postMethod.setHeader("Referer", "http://e.neusoft.edu.cn/nav_login");
	    	List<NameValuePair> params=new ArrayList<NameValuePair>();
	    	params.add(new BasicNameValuePair("account","13354284438"));
	    	params.add(new BasicNameValuePair("password","c11d5abf846db275243d94b8472706d4"));
	    	params.add(new BasicNameValuePair("code",""));
	    	params.add(new BasicNameValuePair("checkcode",checkcode));
	    	params.add(new BasicNameValuePair("Submit","Login"));
	    	//添加参数
	    	postMethod.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
	    	//postMethod.setHeaders(arrHeaders);
	    	response=httpclient.execute(postMethod);
	    	EntityUtils.consume(response.getEntity());
	    	
	    	class PostThread implements  Runnable
	    	{
	    		private HttpClient myclient=null;
	    		private int num=23453234;
	    		public PostThread(HttpClient client)
	    		{
	    			myclient=client;
	    		}
				@Override
				public void run() {
					// TODO Auto-generated method stub
					while(true)
					{
						HttpPost postMethod=new HttpPost("http://e.neusoft.edu.cn/rechargecard.action");
						postMethod.setHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
						postMethod.setHeader("Referer", "http://e.neusoft.edu.cn/nav_Rcard");
						List<NameValuePair> params=new ArrayList<NameValuePair>();
						params.add(new BasicNameValuePair("cardName","046uijalz101480"));
						params.add(new BasicNameValuePair("cardPsw",""+num++));
						params.add(new BasicNameValuePair("Submit","提 交"));
						HttpResponse response;
						try {
							postMethod.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
							response = myclient.execute(postMethod);
							EntityUtils.consume(response.getEntity());
						} catch (ClientProtocolException e) {
						// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
						// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
			    	
				}
	    		
	    	};
	    	
	    	
	    	PostThread mPostThread=new PostThread(httpclient);
	    	mPostThread.run();
	    	
	    }
	    catch (IOException e)
	    {
	    	
	    }
	    finally
	    {
	    	
	    }
	}

}


Java利用HttpClient进行网页登录

标签:java   httpclient   get   post   

原文地址:http://blog.csdn.net/ultimater/article/details/41245517

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!