Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import java.io.UnsupportedEncodingException;
Import java.net.HttpURLConnection;
Import java.net.InetSocketAddress;
Import Java.net.Proxy;
Import Java.net.URL;
Import java.net.URLConnection;
Import java.util.List;
Import Java.util.Map; /** * HTTP Request Tool class * @author snowfigure * @since 2014-8-24 13:30:56 * @version v1.0.1/public class Httprequestut
Il {static Boolean proxyset = false;
static String ProxyHost = "127.0.0.1";
static int proxyport = 8087; /** * Code * @param source * @return/public static string UrlEncode (String source,string Enc
ODE) {String result = source;
try {result = Java.net.URLEncoder.encode (Source,encode);
catch (Unsupportedencodingexception e) {e.printstacktrace (); Return "0";
return result;
public static string URLENCODEGBK (string source) {string result = Source;
try {result = Java.net.URLEncoder.encode (source, "GBK");
catch (Unsupportedencodingexception e) {e.printstacktrace ();
return "0";
return result; /** * Initiates HTTP request fetch return result * @param req_url request Address * @return/public static String Httpreques
T (String req_url) {StringBuffer buffer = new StringBuffer ();
try {URL url = new URL (req_url);
HttpURLConnection httpurlconn = (httpurlconnection) url.openconnection ();
Httpurlconn.setdooutput (FALSE);
Httpurlconn.setdoinput (TRUE);
Httpurlconn.setusecaches (FALSE);
Httpurlconn.setrequestmethod ("get");
Httpurlconn.connect (); Converts the returned input flow to a string InputStream InputStream = Httpurlconn.getinputstream ();
InputStreamReader InputStreamReader = new InputStreamReader (InputStream, "utf-8");
BufferedReader BufferedReader = new BufferedReader (InputStreamReader);
String str = NULL;
while (str = Bufferedreader.readline ())!= null) {buffer.append (str);
} bufferedreader.close ();
Inputstreamreader.close ();
Releasing resources inputstream.close ();
InputStream = null;
Httpurlconn.disconnect ();
catch (Exception e) {System.out.println (E.getstacktrace ());
return buffer.tostring (); /** * Send HTTP request to get return input stream * @param requesturl request Address * @return InputStream * * Public stat
IC InputStream httprequestio (String requesturl) {InputStream inputstream = null; try {URL url = new URL (requeSturl);
HttpURLConnection httpurlconn = (httpurlconnection) url.openconnection ();
Httpurlconn.setdoinput (TRUE);
Httpurlconn.setrequestmethod ("get");
Httpurlconn.connect ();
Gets the returned input stream inputstream = Httpurlconn.getinputstream ();
catch (Exception e) {e.printstacktrace ();
return inputstream; /** * Request to send a GET method to the specified URL * * @param URL * Send the requested URL * @param param *
Request parameter, the request parameter should be the form of name1=value1&name2=value2. * @return The response result of the remote resource represented by the URL/public static string Sendget (string URL, string param) {String results = ""
;
BufferedReader in = null;
try {String urlnamestring = URL + '? ' + param;
URL realurl = new URL (urlnamestring);
The connection between open and URL urlconnection connection = Realurl.openconnection (); Set the generic request genusSexual 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 the actual connection connection.connect ();
Gets all response header fields Map<string, list<string>> Map = Connection.getheaderfields (); Iterate through all the response header fields for (String Key:map.keySet ()) {System.out.println (key +--->) + map.get (k
EY));
}//define BufferedReader input stream to read URL response in = new BufferedReader (New InputStreamReader (
Connection.getinputstream ()));
String Line;
while (line = In.readline ())!= null) {result + = line; Exception e {System.out.println ("Send Get request exception.
"+ e);
E.printstacktrace ();
} Use the finally block to close the input stream finally {try {if (in!= null) {In.close
();
} catch (Exception E2) {e2.printstacktrace ();
} return result; /** * Request * Send the Post method to the specified URL * * @param URL * Send the requested URL * @param param *
Request parameter, the request parameter should be the form of name1=value1&name2=value2. * @param isproxy * Use proxy mode * @return The response result of a remote resource on behalf of/public static String Sendpost (Strin
G URL, String param,boolean isproxy) {outputstreamwriter out = null;
BufferedReader in = null;
String result = "";
try {URL realurl = new URL (URL);
HttpURLConnection conn = null; if (IsProxy) {//using proxy mode @SuppressWarnings ("static-access") Proxy proxy = new Proxy (proxy.type . DIRECT. HTTP, New Inetsocketaddress (ProxyHost, ProxypoRT));
conn = (httpurlconnection) realurl.openconnection (proxy);
}else{conn = (httpurlconnection) realurl.openconnection ();
//Open and URL connection//Send POST request must be set as follows two lines conn.setdooutput (true);
Conn.setdoinput (TRUE); Conn.setrequestmethod ("POST");
Post method/Set Common request attribute Conn.setrequestproperty ("accept", "*/*");
Conn.setrequestproperty ("Connection", "keep-alive"); Conn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1;
SV1) ");
Conn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");
Conn.connect ();
Gets the output stream corresponding to the URLConnection object = new OutputStreamWriter (Conn.getoutputstream (), "UTF-8");
Send request parameter out.write (param);
Flush output Stream Buffer Out.flush (); DefinedBufferedReader input stream to read the response of the URL in = new BufferedReader (New InputStreamReader Conn.getinputstr
EAM ()));
String Line;
while (line = In.readline ())!= null) {result + = line; Exception e {System.out.println ("send POST request exception.
"+e);
E.printstacktrace (); ///Use finally block to turn off output stream, input stream finally{try{if (out!=null) {out.
Close ();
} if (In!=null) {in.close ();
} catch (IOException ex) {ex.printstacktrace ();
} return result;
public static void Main (string[] args) {//demo: proxy access String URL = "http://api.adf.ly/api.php";
String para = "key=youkeyid&youuid=uid&advert_type=int&domain=adf.ly&url=http://somewebsite.com"; String Sr=httprequestutil.sendpost (url,para,true);
System.out.println (SR);
}
}
/** * Send HTTP POST request * * @param xmlinfo * JSON converted to a string * @param URL * please Request URL * @return return info/public static string Dohttppost (String xmlinfo, String URL) {System.out.prin
TLN ("launched data:" + xmlinfo);
byte[] XmlData = Xmlinfo.getbytes ();
InputStream instr = null;
Java.io.ByteArrayOutputStream out = null;
try {URL url = new url (URL);
URLConnection Urlcon = Url.openconnection ();
Urlcon.setdooutput (TRUE);
Urlcon.setdoinput (TRUE);
Urlcon.setusecaches (FALSE);
Urlcon.setrequestproperty ("Content-type", "Application/json");
Urlcon.setrequestproperty ("CharSet", "Utf-8");
Urlcon.setrequestproperty ("Content-length", String.valueof (Xmldata.length));
System.out.println (string.valueof (xmldata.length));
DataOutputStream printout = new DataOutputStream ( Urlcon.getoutputstream ());
Printout.write (XmlData);
Printout.flush ();
Printout.close ();
InStr = Urlcon.getinputstream ();
byte[] bis = Ioutils.tobytearray (instr);
String responsestring = new string (bis, "UTF-8"); if ((responsestring = null) | | ("". Equals (Responsestring.trim ()))
{System.out.println ("return null");
} System.out.println ("Return data is:" + responsestring);
return responsestring;
catch (Exception e) {e.printstacktrace ();
return "0";
Finally {try {out.close ();
Instr.close ();
catch (Exception ex) {return "0"; }
}
}