The Java language uses HttpClient to impersonate a browser login

Source: Internet
Author: User

Use HttpClient to simulate the browser login site, and then you can do things, such as publishing information, etc.

First step : Get the actual post URL (without considering the complexity of the case)

  1, need to use the Httpfox plug-in to Firefox, Httpfox clear a bit, and then start to capture

2, switch back to the page login page, start to enter their own account password login, log in successfully after the cut back to Httpfox stop, see the recent Post method contains the post data data, and this post method URL address,

3. This gives the data parameters (Parameter) and values (value)that need to be post when impersonating the login, and the URL of the actual post

Step two , use httpclient to log in

1, the simple core code is as follows

  

1Closeablehttpclient httpclient =Httpclients.createdefault ();2list<namevaluepair> PostData =NewArraylist<namevaluepair>();3         //there may be multiple parameters here4Postdata.add (NewBasicnamevaluepair ("username", "username"));5Postdata.add (NewBasicnamevaluepair ("Password", "password"));6         //The URL is the actual post address, using Httpfox to get7HttpPost HttpPost =NewHttpPost (URL);9         Try { OneHttppost.setentity (NewUrlencodedformentity (PostData, "GBK")); AResponse =Httpclient.execute (httppost); the}Catch(IOException e) { -}finally { - Closeio (response); -}

The Java language uses HttpClient to impersonate a browser 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.