packagecom.paytest.util;Importjava.io.BufferedReader;Importjava.io.IOException;Importjava.io.InputStreamReader;Importjava.io.PrintWriter;Importjava.io.UnsupportedEncodingException;Importjava.net.URL;Importjava.net.URLConnection;Importjava.net.URLEncoder;Importjava.util.List;Importjava.util.Map; public classHttpRequest {/*** A request to send a GET method to a specified URL *@paramURL * URL to send request *@paramparam * Request parameters, request parameters should be in the form of name1=value1&name2=value2. * @returnThe response result of the remote resource represented by the URL*/ public Staticstring Sendget (string url, string Param) {string result= ""; BufferedReader in=NULL; Try{String urlnamestring= URL + "?" +param; URL Realurl=NewURL (urlnamestring); //opening and linking between URLsURLConnection connection =realurl.openconnection (); //to set common request properties /*** Application/x-javascript text/xml->xml Data * Application/x-javascript->json Object * application/x-www-form-urlencoded-> form data * application/json;charset=utf-8 JSON data * */Connection.setrequestproperty ("content-type", "application/x-www-form-urlencoded"); Connection.setrequestproperty ("accept", "*/*"); Connection.setrequestproperty ("connection", "keep-alive"); Connection.setrequestproperty ("user-agent", "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) "); //establish an actual connectionConnection.connect (); //Get all response header fieldsmap<string, list<string>> map =Connection.getheaderfields (); //iterate through all the response header fields//for (String key:map.keySet ()) {//System.out.println (key + "--->" + map.get (key));// } //defines the response of the BufferedReader input stream to read the URLin =NewBufferedReader (NewInputStreamReader (connection.getinputstream (), "utf-8")); String line; while(line = In.readline ())! =NULL) {result+=line ; } } Catch(Exception e) {System.out.println ("send GET Request exception!" " +e); E.printstacktrace (); Result= "io_exception"; } //Use the finally block to close the input stream finally { Try { if(in! =NULL) {in.close (); } } Catch(Exception e2) {e2.printstacktrace (); } } returnresult; } /*** Request to send the Post method to the specified URL * *@paramURL * URL to send request *@paramparam * Request parameters, request parameters should be in the form of name1=value1&name2=value2. * @returnthe response result of the remote resource represented*/ public Staticstring Sendpost (string url, string Param) {printwriter out=NULL; BufferedReader in=NULL; String result= ""; Try{URL Realurl=NewURL (url); //opening and linking between URLsURLConnection conn =realurl.openconnection (); //to set common request properties /*** Application/x-javascript text/xml->xml Data * Application/x-javascript->json Object * application/x-www-form-urlencoded-> form data (key=value&key=value ... form of Data) * application/json;charset=utf-8 JSON data **/Conn.setrequestproperty ("content-type", "application/x-www-form-urlencoded"); Conn.setrequestproperty ("accept", "*/*"); Conn.setrequestproperty ("connection", "keep-alive"); Conn.setrequestproperty ("user-agent", "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) "); //to send a post request, you must set the following two linesConn.setdooutput (true); Conn.setdoinput (true); //gets the output stream corresponding to the URLConnection objectout =NewPrintWriter (conn.getoutputstream ()); //Send Request ParametersOut.print (param); //buffer for flush output streamOut.flush (); //defines the response of the BufferedReader input stream to read the URLin =NewBufferedReader (NewInputStreamReader (conn.getinputstream (), "utf-8")); String line; while(line = In.readline ())! =NULL) {result+=line ; } } Catch(Exception e) {System.out.println ("send POST Request exception!" " +e); E.printstacktrace (); } //Use the finally block to close the output stream, input stream finally { Try { if(out! =NULL) {out.close (); } if(in! =NULL) {in.close (); } } Catch(ioexception Ex) {ex.printstacktrace (); } } returnresult; } public Static voidMain (string[] Args)throwsunsupportedencodingexception {String S= "fdsa you after years."; //string string = Urlencoder.encode (s, "utf-8");String string = Urlencoder.encode (s, "gbk"); System.out.println (string); String SR= Httprequest.sendget ("https://127.0.0.1/pay/queryOrder", "cpid=weixing&cppwd=654321&phone=13293787666 &spnumber=&msgcont= "+string+" &extend=01234 "); System.out.println (sr); }}
HTTP Request Class