This article illustrates the Java implementation of HTTP POST, get, proxy access request detailed code snippet, share for everyone to refer to, the specific content as follows
Package com.snowfigure.kits.net;
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 encode) {
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 HttpRequest (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 static inputs
Tream 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, request parameter should be NA
The form of me1=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 ();
Sets the common request attribute 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 (key)); }//define BufferedReader input stream to read URL response in = new BufferedReader (New InputStreamReader Connection.getinpu
Tstream ()));
String Line;
while (line = In.readline ())!= null) {result + = line; The 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 ();
} return result; /** * Request * Send the Post method to the specified URL * * @param URL * Send the requested URL * @param param * Request parameter, request parameter should be name
The form of 1=value1&name2=value2. * @param IsProxy * Whether to use proxy mode * @return The response result of the remote resource on behalf/public static string Sendpost (string url, string p
Aram,boolean isproxy) {outputstreamwriter out = null;
BufferedReader in = null; Stringresult = "";
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, NE
W 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 objectout = new OutputStreamWriter (Conn.getoutputstream (), "UTF-8");
Send request parameter out.write (param);
Flush output Stream Buffer Out.flush ();
Defines the response of the BufferedReader input stream to read the URL in = new BufferedReader (New InputStreamReader (Conn.getinputstream ()));
String Line;
while (line = In.readline ())!= null) {result + = line; The catch (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);
}
}
Hopefully this article will help you learn about Java programming.