Import Java.io.BufferedReader;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import java.net.HttpURLConnection;
Import Java.net.URL;
Import Java.util.Map;
Import Java.util.Map.Entry;
Import Net.sf.json.JSONObject;
Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;
/**
* HTTP Request Tool Class
*/
public class Httputil {
private static log log = Logfactory.getlog (Formutil.class);
/**
* Send request, return request result
*/
public static string http (string URL, map<string, string> params) {
URL u = null;
HttpURLConnection con = null;
StringBuffer sb = new StringBuffer ();
if (params! = null) {
Log.info ("SMS interface parameter send start");
For (entry<string, string> e:params.entryset ()) {
Sb.append (E.getkey ());
Sb.append ("=");
Sb.append (E.getvalue ());
Sb.append ("&");
Log.info (E.getkey () + "=============" + e.getvalue ());
}
Log.info ("SMS Interface Parameter send End");
Sb.deletecharat (Sb.length ()-1);
}
Send Request
try {
u = new url (URL);
Con = (httpurlconnection) u.openconnection ();
Con.setrequestmethod ("POST");
Con.setdooutput (TRUE);
Con.setdoinput (TRUE);
Con.setusecaches (FALSE);
Con.setrequestproperty ("Content-type", "application/x-www-form-urlencoded;charset=gb2312");
OutputStreamWriter OSW = new OutputStreamWriter (Con.getoutputstream (), "GB2312");
Osw.write (Sb.tostring ());
Osw.flush ();
Osw.close ();
} catch (Exception e) {
E.printstacktrace ();
} finally {
if (con! = null) {
Con.disconnect ();
}
}
Read back Content
StringBuffer buffer = new StringBuffer ();
try {
BufferedReader br = new BufferedReader (New InputStreamReader (Con.getinputstream (), "GB2312"));
String temp;
while (temp = br.readline ()) = null) {
Buffer.append (temp);
}
} catch (Exception e) {
E.printstacktrace ();
}
return buffer.tostring ();
}
}
Another SMS Send class