Java HTTP request

Source: Internet
Author: User

String s1= "1231dsdgasd of whispering large";

Clob C = new Serialclob (S1.tochararray ());//string Turn Clob
Blob B = new Serialblob (s1.getbytes ("GBK"));//string to Blob
You can also do this by not passing the character set name, using the system's
Blob B = New Serialblob (S1.getbytes ());

String clobstring = c.getsubstring (1, (int) c.length ());//clob to String
String blobstring = new String (B.getbytes (1, (int) b.length ()), "GBK");//blob to String
If the character set name is not passed in front, there is no need to pass in here to avoid errors
String blobstring = new String (B.getbytes (1, (int) b.length ()));

System.out.println (clobstring);
System.out.println (blobstring);p ackage infohold.util;

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;

public class Httprequestutil {
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;
}

/**
* Initiate HTTP request for 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 ();
Freeing resources
Inputstream.close ();
InputStream = null;
Httpurlconn.disconnect ();

} catch (Exception e) {
System.out.println (E.getstacktrace ());
}
return buffer.tostring ();
}

/**
* Send HTTP request to get returned input stream
*
* @param requesturl
* Request Address
* @return InputStream
*/
public static 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 ();
Get the input stream returned
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 parameters, request parameters should be in 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 result = "";
BufferedReader in = null;
try {
String urlnamestring = URL + "?" + param;
URL realurl = new URL (urlnamestring);
Opening and linking between URLs
URLConnection connection = Realurl.openconnection ();
To set common request properties
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 connection
Connection.connect ();
Get 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));
}
Defines the response of the BufferedReader input stream to read the URL
in = new BufferedReader (New InputStreamReader (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 the finally block to close the input stream
finally {
try {
if (in = null) {
In.close ();
}
} catch (Exception E2) {
E2.printstacktrace ();
}
}
return result;
}

/**
* Request to send the 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.
* @param isproxy
* Whether to use proxy mode
* @return The response result of the remote resource represented
*/
public static string Sendpost (string url, string param, Boolean isproxy) {
OutputStreamWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realurl = new URL (URL);
HttpURLConnection conn = null;
if (IsProxy) {//Use 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 ();
}
Opening and linking between URLs

To send a POST request, you must set the following two lines
Conn.setdooutput (TRUE);
Conn.setdoinput (TRUE);
Conn.setrequestmethod ("POST"); Post method

To set common request properties

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
out = new OutputStreamWriter (Conn.getoutputstream (), "UTF-8");
Send Request parameters
Out.write (param);
Buffer for flush output stream
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;
}
} 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 ();
}
}
return result;
}

public static void Main (string[] args) {
Demo: Delegate 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);
}

}

Java HTTP request

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.