Title, use Java to simulate get and post requests. Get can be used to crawl Web pages, using post can be used to achieve the
login of some web site brute force.
but only practice, the practical significance is not big.
Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.io.Reader;Importjava.net.HttpURLConnection;Importjava.net.ProtocolException;ImportJava.net.URL;ImportJava.util.Map;/*** HTTP Tool *@authorRobinzhang **/ Public classHttputil {/*** Request Type: GET*/ Public Final StaticString get = "Get"; /*** Request Type: POST*/ Public Final StaticString post = "POST"; /*** Analog HTTP GET request *@paramURLSTR * Request Path *@paramParammap * Request Parameters *@return * @throwsException*/ Public StaticString Get (String urlstr, map<string, string> Parammap)throwsexception{urlstr= Urlstr + "?" +getparamstring (PARAMMAP); HttpURLConnection Conn=NULL; Try{ //Create a URL objectURL url =NewURL (URLSTR); //Get URL Connectionconn =(HttpURLConnection) url.openconnection (); //to set common request propertiessethttpurlconnection (conn, GET); //establish an actual connectionConn.connect (); //get the content of the response returnreadresponsecontent (Conn.getinputstream ()); }finally{ if(NULL!=conn) Conn.disconnect (); } } /*** Analog HTTP POST request *@paramURLSTR * Request Path *@paramParammap * Request Parameters *@return * @throwsException*/ Public StaticString post (String urlstr, map<string, string> Parammap)throwsexception{HttpURLConnection Conn=NULL; PrintWriter writer=NULL; Try{ //Create a URL objectURL url =NewURL (URLSTR); //GET Request ParametersString param =getparamstring (PARAMMAP); //Get URL Connectionconn =(HttpURLConnection) url.openconnection (); //set Common request Propertiessethttpurlconnection (conn, POST); //establish an actual connectionConn.connect (); //writes the request parameter to the request character streamwriter =NewPrintWriter (Conn.getoutputstream ()); Writer.print (param); Writer.flush (); //read the contents of the response returnreadresponsecontent (Conn.getinputstream ()); }finally{ if(NULL!=conn) Conn.disconnect (); if(NULL!=writer) writer.close (); } } /*** Read the response byte stream and convert it to a string *@paramin * byte stream to read *@return * @throwsIOException*/ Private StaticString readresponsecontent (InputStream in)throwsioexception{Reader Reader=NULL; StringBuilder content=NewStringBuilder (); Try{Reader=NewInputStreamReader (in); Char[] buffer =New Char[1024]; intHead = 0; while(Head=reader.read (buffer)) >0) {content.append (NewString (buffer, 0, head)); } returncontent.tostring (); }finally{ if(NULL!=In ) In.close (); if(NULL!=reader) reader.close (); } } /*** Set HTTP connection properties *@paramConn * HTTP connection *@return * @throwsprotocolexception *@throwsException*/ Private Static voidSethttpurlconnection (HttpURLConnection conn, String Requestmethod)throwsprotocolexception{Conn.setrequestmethod (Requestmethod); Conn.setrequestproperty ("Accept", "*/*"); Conn.setrequestproperty ("Accept-language", "ZH-CN"); Conn.setrequestproperty ("User-agent", "mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; trident/5.0) "); Conn.setrequestproperty ("Proxy-connection", "keep-alive"); if(NULL!=requestmethod &&post.equals (Requestmethod)) {Conn.setdooutput (true); Conn.setdoinput (true); } } /*** Convert parameter to path string *@paramparams * Parameters *@return */ Private StaticString getparamstring (map<string, string>Parammap) { if(NULL==parammap | |Parammap.isempty ()) { return""; } StringBuilder Builder=NewStringBuilder (); for(String key:paramMap.keySet ()) {Builder.append ("&"). Append (Key). Append ("="). Append (Parammap.get (key)); } returnBuilder.deletecharat (0). toString (); } Public Static voidMain (string[] args) {Try{System.out.println (Get ("Http://127.0.0.1/crazy_java.pdf",NULL) ); } Catch(Exception e) {e.printstacktrace (); } }}
Java impersonation of Get and post requests for HTTP