Java HTTP Request

Source: Internet
Author: User

ImportJava.io.*;Importjava.net.HttpURLConnection;Importjava.net.MalformedURLException;ImportJava.net.URL;Importjava.net.URLConnection;ImportJava.net.URLEncoder;ImportJava.util.zip.GZIPInputStream; Public classTesthttp { Public Static voidMain (string[] args)throwsIOException {Try{URL URL=NewURL ("http://zhidao.baidu.com/"); URLConnection Conn=url.openconnection (); HttpURLConnection HttpURLConnection=(httpurlconnection) conn; Httpurlconnection.setrequestproperty ("Host", "zhidao.baidu.com"); Httpurlconnection.setrequestproperty ("Connection", "keep-alive"); Httpurlconnection.setrequestproperty ("Cache-control", "max-age=0"); Httpurlconnection.setrequestproperty ("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); Httpurlconnection.setrequestproperty ("User-agent", "mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/35.0.1916.153 safari/537.36 SE 2.X METASR 1.0 "); Httpurlconnection.setrequestproperty ("Referer", "https://www.baidu.com/link?url=BHPFr4z5_jfNStMOzkornuMJSmwj_ 2doif9q8ivqw9rw8xpahovxdfjkgkpsspiyghynort7ziken5eo1q207a&wd=&eqid=a08f92f0000022f80000000355eece3b "); //The key is the following sentence this sentence means that the browser supports the decompression method if you send this header, then the server may be returned in some way compressed information//httpurlconnection.setrequestproperty ("accept-encoding", "gzip");Httpurlconnection.setrequestproperty ("Accept-language", "zh-cn,zh;q=0.8"); Httpurlconnection.setrequestproperty ("Cookie", "baiduid=93f317fbb6b90290b30a5c8521adf385:fg=1; pstm=1441426636; bidupsid=f3bb3c015ab71995f023187b9424234b; bdshare_firstime=1441714095462; BDRCVFR[ZLN_T-KFICF]=PDG3IQUBLHTTVQMULR8MVQV; h_ps_pssid=1431_13477_12868_17104_16935_17004_15648_11513_13932_16969_17051; ik_93f317fbb6b90290b30a5c8521adf385=4; ik_cid_74=4; hm_lvt_6859ce5aaf00fb00387e6434e4fcc925=1441714095,1441714137,1441714225,1441715740; hm_lpvt_6859ce5aaf00fb00387e6434e4fcc925=1441717326 "); Conn.setdooutput (true); Conn.setdoinput (true); String result= ""; BufferedReader in=NULL; PrintWriter out=NULL; out=NewPrintWriter (Conn.getoutputstream ()); //Send Request Parameters//out.print (param); //buffer for flush output streamOut.flush (); //GBK and UTF-8 are two kinds of coding methods used in transmission process.in =NewBufferedReader (NewInputStreamReader (Conn.getinputstream (), "GBK"));            String Line;  while(line = In.readline ())! =NULL) {result+=Line ;                    } System.out.println (Result); }         Catch(malformedurlexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }         }

The key points are two: do not send Httpurlconnection.setrequestproperty ("accept-encoding", "gzip") when first sending; This header, otherwise the received data is compressed in gzip mode.

The second time to use the InputStream note the coding method

Java HTTP Request

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.