HTTP interface Invocation

Source: Internet
Author: User

public class Httptest {
String creatorderurl= "Http://xxx.xxx.net/scriptParams/runScripts";

public void Testcrestorder () {
System.out.println ("url==" +creatorderurl);//
HttpRequest (Creatorderurl);
System.out.println ("url2==" +httprequest (Creatorderurl));
}

/**get Request */
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 ();
}
/**post Request
*
*/
/**
* 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;
}
/**
* Send an HTTP POST request
*
* @param xmlinfo
* JSON converted into a string
* @param URL
* Request URL
* @return Return information
*/
public static string Dohttppost (String xmlinfo, String URL) {
System.out.println ("Originating 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";
}
}
}
Method Two:
/**
* 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.
* @return The response result of the remote resource represented
*/
public static string sendpostnew (string url, string param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realurl = new URL (URL);
Opening and linking between URLs
URLConnection conn = Realurl.openconnection ();

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) ");

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

Gets the output stream corresponding to the URLConnection object
out = new PrintWriter (Conn.getoutputstream ());
Send Request parameters
Out.print (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;
}
}

HTTP interface Invocation

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.