JAVA send post and GET request __java

Source: Internet
Author: User
Tags finally block flush readline stringbuffer
public static string doget (string url) throws exception{
String result= "";
try {
SYSTEM.OUT.PRINTLN ("Send GET Request:" +url);
URL restserviceurl = new URL (URL);


HttpURLConnection httpconnection = (httpurlconnection) restserviceurl.openconnection ();
Httpconnection.setrequestmethod ("get");
Httpconnection.setrequestproperty ("Accept", "Application/json");


if (Httpconnection.getresponsecode ()!= 200) {
throw new RuntimeException ("HTTP get Request Failed with Error code:"
+ Httpconnection.getresponsecode ());
}


BufferedReader responsebuffer = new BufferedReader (New InputStreamReader (
(Httpconnection.getinputstream ()));


String output;



while (output = Responsebuffer.readline ())!= null) {
result = = output;
}


Httpconnection.disconnect ();


catch (Malformedurlexception e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}

return result;
}


public static string doPost (String URL, map<string, object> params) throws exception{
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realurl = new URL (URL);
Opening and linking to 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) ");
Send POST request must be set as follows 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
StringBuffer sb = new StringBuffer ();
For (String Key:params.keySet ()) {
Sb.append ("&" +key+ "=" +params.get (key));
}
Sb.deletecharat (0);
Out.print (SB);
Buffer of flush output stream
Out.flush ();
Defines the response of a bufferedreader input stream to read a 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 turn off the output stream, the input stream
finally{
try{
if (out!=null) {
Out.close ();
}
if (in!=null) {
In.close ();
}
}
catch (IOException ex) {
Ex.printstacktrace ();
}
}
return result;


}


Related Article

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.