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 -+ "¬icebean.sourcech=¬icebean.source=" in+ "¬icebean.title=¬icebean.startdate=¬icebean.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