Httpclient+jsoup Summary

Source: Internet
Author: User

Jsoup.parse parsing an HTML string, such as Jsoup.parse ("jsoup.connect resolves URL web address, such as Jsoup.connect (http://www.baidu.com). Get ()

You can use HttpClient to get the page, and then use Jsoup.parse to parse the page

String text = getmethod.getresponsebodyasstring ();
Document doc = jsoup.parse (text);

Law One: Httpclient+jsoup

1String Dataurl = "http://hi.mop.com/?";2HttpClient HttpClient =NewHttpClient ();3String cookie = "_ml=371386500452711504675;";4GetMethod GetMethod =NewGetMethod (dataurl);5 //Use the previous cookie as a pass for each visit to the URL you need to authorize6Getmethod.setrequestheader ("Cookie", cookies);7Getmethod.setrequestheader ("Referer", "http://passport.mop.com/");8Getmethod.setrequestheader ("User-agent", "mozilla/4.0" (compatible; MSIE 7.0; Windows NT 6.0) ");9 intStatuscodes =Httpclient.executemethod (GetMethod);Ten System.out.println (statuscodes); OneSYSTEM.OUT.PRINTLN ("Emulation login succeeded");  AString text = getmethod.getresponsebodyasstring ();

Law II: Jsoup connction + jsoup Analysis

Connection conn = jsoup.connect (String URL);            Conn.data ("Txtbill", key); // Set keyword query fields NULL  = conn.timeout (100000). Post (); // Set Request type is post or get type, time-out 100000    milliseconds Results = Doc.select (TagName); // processing return Data              for (Element result:results) {}

 PackageStep1;ImportJava.io.BufferedReader;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;Importjavax.lang.model.element.Element;ImportOrg.apache.commons.httpclient.Cookie;Importorg.apache.commons.httpclient.HttpClient;ImportOrg.apache.commons.httpclient.NameValuePair;ImportOrg.apache.commons.httpclient.cookie.CookiePolicy;ImportOrg.apache.commons.httpclient.methods.GetMethod;ImportOrg.apache.commons.httpclient.methods.PostMethod;ImportOrg.jsoup.Jsoup;Importorg.jsoup.nodes.Document;Importorg.jsoup.select.Elements;/*** * @ClassName: Httplogin * @Description: Java combines jsoup with httpclient *@authorZeze * @date November 10, 2015 PM 5:07:33 **/ Public classHttplogin { Public Static voidMain (string[] args) {//URLString Dataurl = "http://hi.mop.com/?"; HttpClient HttpClient=NewHttpClient (); Try {            //access to cookies after landingString cookie = "_ml=371386500452711504675;"; GetMethod GetMethod=NewGetMethod (Dataurl); //Use the previous cookie as a pass for each visit to the URL you need to authorizeGetmethod.setrequestheader ("Cookie", cookies); Getmethod.setrequestheader ("Referer", "http://passport.mop.com/"); Getmethod.setrequestheader ("User-agent", "mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0) "); intStatuscodes =Httpclient.executemethod (GetMethod); System.out.println ("Status code:" +statuscodes); //if the header information returned does not have a specified length or length greater than 1M the exception is thrown getresponsebodyasstring () to Getresponsebodyasstream ( )//String Text = getmethod.getresponsebodyasstring ();InputStream InputStream =Getmethod.getresponsebodyasstream (); BufferedReader BR=NewBufferedReader (NewInputStreamReader (InputStream)); StringBuffer StringBuffer=NewStringBuffer (); String Str= "";  while(str = br.readline ())! =NULL) {stringbuffer.append (str); }                        //Element Result=text;Document doc =Jsoup.parse (stringbuffer.tostring ()); Elements name=doc.select ("[Class=tc c068 fs14 Yahei mt5 Username]");        System.out.println (Name.text ()); }        Catch(Exception e) {e.printstacktrace (); }    }}

Httpclient+jsoup Summary

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.