On the internet for a long time did not find the post, get request of the tool class, now collated a share out. The HTTP tools classes are as follows:
PackageCom.qlwb.business.util;ImportJava.io.BufferedReader;ImportJava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.lang.reflect.Field;ImportJava.net.URL;ImportJava.net.URLConnection;ImportJava.util.List;ImportJava.util.Map;ImportJava.util.Timer;ImportJava.util.TimerTask;ImportNet.sf.json.JSONObject;ImportOrg.apache.log4j.Logger;/** * * @ class Number: * @ class Name: HttpRequest * @ Content Summary: HTTP POST GET request * @author: Lu Weiwei * @ created: April 13, 2016 4:03:50 * @ Repair Change: * @ Modified: * @ Modify Description: Simple description of Modified content * @version 1.0.0 * */ Public class httprequest { /** * Request to send a GET method to a specified URL * * @param URL * Send the requested URL * @param param * Request parameters, request parameters should be in the form of name1=value1&name2=value2. * The response result of the remote resource represented by the @return URL */ Public StaticStringSendget(string url, string param) {String result =""; BufferedReader in =NULL;Try{String urlnamestring = URL +"?"+ param; URL Realurl =NewURL (urlnamestring);connection between//open and URLURLConnection connection = Realurl.openconnection ();//Set Common request PropertiesConnection.setrequestproperty ("Accept","*/*"); Connection.setrequestproperty ("Connection","Keep-alive"); Connection.setrequestproperty ("User-agent","mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");//Establish the actual connectionConnection.connect ();//Get all response header fieldsmap<string, list<string>> map = Connection.getheaderfields ();//Traverse all the response header fields for(String Key:map.keySet ()) {System.out.println (key +"--->"+ map.get (key)); }//define BufferedReader input stream to read the response of the URLin =NewBufferedReader (NewInputStreamReader (Connection.getinputstream ())); String Line; while(line = In.readline ())! =NULL) {result + = line; } }Catch(Exception e) {System.out.println ("Send GET request exception!" "+ e); E.printstacktrace (); }//Use finally block to close the input stream finally{Try{if(In! =NULL) {in.close (); } }Catch(Exception E2) {E2.printstacktrace (); } }returnResult }/** * Request to send a POST method to the specified URL * * @param URL * Send the requested URL * @param param * Request parameters, request parameters should be in the form of name1=value1&name2=value2. * Response results for remote resources represented by the @return * / Public StaticStringSendpost(string url, string param) {PrintWriter out =NULL; BufferedReader in =NULL; String result =""; Logger.getlogger (httprequest.class). info ("Traffic recharge starts:"+url+"&"+param);Try{URL Realurl =NewURL (URL);connection between//open and URLURLConnection conn = Realurl.openconnection ();//Set Common request PropertiesConn.setrequestproperty ("Accept","*/*"); Conn.setrequestproperty ("Connection","Keep-alive"); Conn.setrequestproperty ("User-agent","mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");//Send a POST request must have the following two lines setConn.setdooutput (true); Conn.setdoinput (true);//Get the output stream corresponding to the URLConnection objectout =NewPrintWriter (Conn.getoutputstream ());//Send request ParametersOut.print (param);//Flush output stream bufferingOut.flush ();//define BufferedReader input stream to read the response of the URLin =NewBufferedReader (NewInputStreamReader (Conn.getinputstream ())); String Line; while(line = In.readline ())! =NULL) {result + = line; } logger.getlogger (Httprequest.class). info ("Traffic recharge Ends:"+result); }Catch(Exception e) {System.out.println ("Send POST request exception!" "+ e); E.printstacktrace (); }//Use 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 StaticStringFormatparam(Object o) {StringBuffer sbf=NewStringBuffer (); Class cls = O.getclass (); field[] fields = Cls.getdeclaredfields ();Try{ for(Field f:fields) {f.setaccessible (true);if(!"Serialversionuid". Equals (F.getname ())) {Sbf.append (F.getname () +"="+ (F.get (o) = =NULL?"": F.get (o)) +"&"); }//if (F.get (o)! = null && f.get (o)! = "") {// }} }Catch(IllegalArgumentException e) {//TODO auto-generated catch blockE.printstacktrace (); }Catch(Illegalaccessexception e) {//TODO auto-generated catch blockE.printstacktrace (); }returnSbf.tostring (). SUBSTRING (0, sbf.tostring (). Length ()-1); } Public Static void Main(string[] args) {//Thirdrechargereqparams p=new thirdrechargereqparams ();//P.setmobile ("18706402245");//P.setordermeal ("ten");//P.setmsgid ("123456");//P.setrange ("0");//System.out.println (p.tostring ());//System.out.println (Creditmallhandle.thirdflowrecharge (P));//Timer timer = new timer ();//Timer.schedule (New Requesttask (), 1000,6*1000);//Timer.cancel ();Jsonobject bodyobj = Jsonobject.fromobject (Httprequest.sendpost ("Http://api.wxflow.com:9092/Public/Api/?service=User.buyFlowOrderInfo","username=qlyd&userpwd=27e3b7118777a1114fb0c9cc0052d31a614c55da&mobile=18706402245&ordermeal= 20&ordertime=1&msgid=23213&extend=&range=0 ")); System.out.println (Bodyobj.getjsonobject ("Data"). GetString ("Code")); }}
HTTP request get, Post tool class