Http-java analog POST request little chestnuts

Source: Internet
Author: User
Tags finally block getmessage

1 ImportJava.io.BufferedReader;2 Importjava.io.IOException;3 ImportJava.io.InputStreamReader;4 ImportJava.io.PrintWriter;5 ImportJava.net.URL;6 Importjava.net.URLConnection;7 8 ImportJavax.net.ssl.HostnameVerifier;9 Importjavax.net.ssl.HttpsURLConnection;Ten Importjavax.net.ssl.SSLSession; One  A ImportOrg.apache.log4j.Logger; -  - ImportCom.xwtech.parser.PostRequestHtmlParser; the  - /** - * POST Request Class--Get HTML response -  */ +  Public classPostrequestextendsThread { -     PrivateString url = "Https://b2b.10086.cn/b2b/main/listVendorNoticeResult.html?noticeBean.noticeType="; +     PrivateString params; A     PrivateLogger Logger; at      -      PublicPostrequest (intNoticetype,intperpagesize) { -Logger = Logger.getlogger (postrequest.class); -          This. url = This. URL +Noticetype; //Stitching URL request with no parameters  -params = "page.currentpage=" + currentpage + "&page.perpagesize=" +perpagesize -+ "&noticebean.sourcech=&noticebean.source=" in+ "&noticebean.title=&noticebean.startdate=&noticebean.enddate="; -     } to  +      Public voidrun () { -PrintWriter out =NULL; theBufferedReader in =NULL; *URLConnection conn =NULL; $String result = "";Panax Notoginseng         Try { -conn =Newurl (url). OpenConnection (); theConn.setusecaches (false); +Conn.setrequestproperty ("Accept", "*/*"); AConn.setrequestproperty ("Connection", "keep-alive"); theConn.setrequestproperty ("User-agent", +"mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/54.0.2840.71 safari/537.36 "); -Conn.setrequestproperty ("X-requested-with", "XMLHttpRequest"); $             //to send a POST request, you must set the following two lines $Conn.setdooutput (true); -Conn.setdoinput (true); -  the             //gets the output stream corresponding to the URLConnection object -out =NewPrintWriter (Conn.getoutputstream ());Wuyi             //Send Request Parameters the Out.print (params); -             //buffer for flush output stream Wu Out.flush (); -in =NewBufferedReader (NewInputStreamReader (Conn.getinputstream ())); About String Line; $              while(line = In.readline ())! =NULL) { -Result + =Line ; -             } -}Catch(Exception e) { ALogger.error (CurrentThread (). GetName () + "thread POST request problem!\n" + e.getmessage () + "\ n"); +}finally{//Use the finally block to close the output stream, input stream the             Try { -                 if(Out! =NULL) { $ out.close (); the                 } the                 if(In! =NULL) { the in.close (); the                 } -}Catch(IOException ex) { inLogger.error (CurrentThread (). GetName () + "thread post request Data flow problem!\n" + ex.getmessage () + "\ n"); the             } the } About //Get results result, you can do the next step directly here, or put it in the global field and get it by other means ... the     } the}

Information such as the corresponding parameters and URLs can be viewed using browser network components such as Chrome,firefox.

Java-get Request: http://www.cnblogs.com/husky/p/6377577.html

Http-java analog POST request little chestnuts

Related Article

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.