Use httpclient to download pages, pictures

Source: Internet
Author: User

Code

Importjava.io.IOException;Importjava.io.UnsupportedEncodingException;Importorg.apache.http.client.ClientProtocolException;ImportOrg.apache.http.client.config.CookieSpecs;ImportOrg.apache.http.client.config.RequestConfig;ImportOrg.apache.http.client.methods.CloseableHttpResponse;ImportOrg.apache.http.client.methods.HttpGet;Importorg.apache.http.impl.client.CloseableHttpClient;Importorg.apache.http.impl.client.HttpClients;Importorg.apache.http.util.EntityUtils;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory; Public classMyhttpclientutils {Private StaticLogger Logger = Loggerfactory.getlogger (myhttpclientutils.class); Private Static Final intHttpclient_timeout = 5000;  Public StaticTuple2<boolean,string> Getpagebyurl (String pageurl, string encode)throwsunsupportedencodingexception{Logger.info ("Pageurl=" +pageurl); String Body=NULL; Requestconfig Requestconfig=Requestconfig.custom (). SetSocketTimeout (httpclient_timeout)//Socket Timeout. Setconnecttimeout (Httpclient_timeout)//Connect Timeout. Build (); Closeablehttpclient httpClient=Httpclients.custom (). Setdefaultrequestconfig (Requestconfig). build (); HttpGet HttpGet=NewHttpGet (Pageurl); Try{closeablehttpresponse response=Httpclient.execute (HttpGet); String StatusCode=string.valueof (Response.getstatusline (). Getstatuscode ()); Logger.info ("Getstatuscode=" +response.getstatusline (). Getstatuscode ()); if(Response.getstatusline (). Getstatuscode ()! = 200) {Logger.info ("Return code exception:" +response.getstatusline (). Getstatuscode ()); return NewTuple2<boolean, String> (false,NULL); } Body=entityutils.tostring (Response.getentity (), encode);//System.out.println ("body=" + body);}Catch(IOException e) {System.out.println ("----------Connection timeout--------");//return NE            }            return NewTuple2<boolean, String> (true, body); }             Public StaticTuple2<boolean,byte[]> Getpicbytearray (String picurl)throwsclientprotocolexception, IOException {logger.info ("Download Url=" +Picurl); Requestconfig GlobalConfig=Requestconfig.custom (). Setcookiespec (Cookiespecs.standard). Setconnectionrequesttimeout (HTTPCLIENT_TIMEOUT)        . Setconnecttimeout (Httpclient_timeout). build (); Closeablehttpclient httpClient=Httpclients.custom (). Setdefaultrequestconfig (GlobalConfig). build (); HttpGet HttpGet=NewHttpGet (Picurl); Closeablehttpresponse Response=Httpclient.execute (HttpGet); Logger.info ("Returned status code:" +response.getstatusline (). Getstatuscode ()); if(Response.getstatusline (). Getstatuscode () = = 200) {            byte[] bytes =Entityutils.tobytearray (Response.getentity ()); return NewTuple2<> (true, bytes); }Else{            return NewTuple2<> (false,NULL); }    }}
 PackageTestgetpic;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportOrg.apache.http.Header;ImportOrg.apache.http.client.config.CookieSpecs;ImportOrg.apache.http.client.config.RequestConfig;ImportOrg.apache.http.client.methods.CloseableHttpResponse;ImportOrg.apache.http.client.methods.HttpGet;Importorg.apache.http.impl.client.CloseableHttpClient;Importorg.apache.http.impl.client.HttpClients;Importorg.apache.http.util.EntityUtils; Public classTestPic2 { Public Static voidMain (string[] args)throwsioexception{requestconfig GlobalConfig= Requestconfig.custom (). Setcookiespec (Cookiespecs.standard). Setconnectionrequesttimeout (6000). Setconnecttimeout (6000). build (); Closeablehttpclient httpClient=Httpclients.custom (). Setdefaultrequestconfig (GlobalConfig). build (); HttpGet HttpGet=NewHttpGet ("Http://xxxx.com/abc.jpg"); Closeablehttpresponse Response=Httpclient.execute (HttpGet); Header[] Headerarray=response.getallheaders ();  for(Header h:headerarray) {System.out.println (H.getname ());            System.out.println (H.getvalue ()); System.out.println ("======"); } System.out.println ("---------------");        System.out.println (Response.getstatusline (). Getstatuscode ()); if(Response.getstatusline (). Getstatuscode () = = 200) {            //For (Header h:response.getallheaders ()) {//System.out.println (h.getelements (). length);//For (HeaderElement he:h.getelements ()) {//System.out.println ("pc=" + He.getparametercount ());//                }//System.out.println (H.getname ());//System.out.println (H.getvalue ());//            }                        byte[] B =Entityutils.tobytearray (Response.getentity ()); FileOutputStream Fos=NewFileOutputStream ("Test2.jpg");            Fos.write (b);        Fos.close (); }            }    }

Use httpclient to download pages, pictures

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.