Java send GET request and POST request sample _java

Source: Internet
Author: User
Tags readline stringbuffer

Java sends get and POST requests to the server

Copy Code code as follows:

Package com.hongyuan.test;

Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.PrintWriter;
Import java.net.HttpURLConnection;
Import Java.net.URL;

public class HttpClient {
Send a GET request
public static string get (String path) throws exception{
HttpURLConnection Httpconn=null;
BufferedReader In=null;
try {
URL url=new url (path);
Httpconn= (HttpURLConnection) url.openconnection ();

Read response
if (Httpconn.getresponsecode () ==HTTPURLCONNECTION.HTTP_OK) {
StringBuffer content=new StringBuffer ();
String tempstr= "";
In=new BufferedReader (New InputStreamReader (Httpconn.getinputstream ()));
while ((Tempstr=in.readline ())!=null) {
Content.append (TEMPSTR);
}
return content.tostring ();
}else{
throw new Exception ("There is a problem with the request!");
}
catch (IOException e) {
E.printstacktrace ();
}finally{
In.close ();
Httpconn.disconnect ();
}
return null;
}
Send a GET request, Parameter form key1=value1&key2=value2 ...
public static string post (string path,string params) throws exception{
HttpURLConnection Httpconn=null;
BufferedReader In=null;
PrintWriter Out=null;
try {
URL url=new url (path);
Httpconn= (HttpURLConnection) url.openconnection ();
Httpconn.setrequestmethod ("POST");
Httpconn.setdoinput (TRUE);
Httpconn.setdooutput (TRUE);

Send POST request parameters
Out=new PrintWriter (Httpconn.getoutputstream ());
OUT.PRINTLN (params);
Out.flush ();

Read response
if (Httpconn.getresponsecode () ==HTTPURLCONNECTION.HTTP_OK) {
StringBuffer content=new StringBuffer ();
String tempstr= "";
In=new BufferedReader (New InputStreamReader (Httpconn.getinputstream ()));
while ((Tempstr=in.readline ())!=null) {
Content.append (TEMPSTR);
}
return content.tostring ();
}else{
throw new Exception ("There is a problem with the request!");
}
catch (IOException e) {
E.printstacktrace ();
}finally{
In.close ();
Out.close ();
Httpconn.disconnect ();
}
return null;
}

public static void Main (string[] args) throws Exception {
String resmessage=httpclient.get ("Http://localhost:3000/hello?hello=hello get");
String resmessage=httpclient.post ("Http://localhost:3000/hello", "Hello=hello post");
System.out.println (Resmessage);
}

}

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.