Java HTTP GET and POST requests

Source: Internet
Author: User

1.http Tool Class

Package com.funshion.common.utils;

Import Java.net.URI;
Import Java.net.URL;


Import Org.apache.http.HttpResponse;
Import org.apache.http.client.HttpClient;
Import Org.apache.http.client.methods.HttpGet;
Import Org.apache.http.client.methods.HttpPost;
Import org.apache.http.entity.StringEntity;
Import org.apache.http.impl.client.DefaultHttpClient;
Import Org.apache.http.protocol.HTTP;
Import Org.apache.http.util.EntityUtils;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;

/**
* HTTP Tool class
*
* @author Qiuwy
*
*/
@SuppressWarnings ({"Deprecation", "resource"})
public class Httputil {

public static final int http_code_200 = 200;

private static Logger Logger = Loggerfactory.getlogger (Httputil.class);

/**
* Get
*
* @param URL
* @return
* @throws Exception
*/
public static HTTPRESVO doget (String getUrl) throws Exception {
URL url = new URL (getUrl);
Uri uri = new Uri (Url.getprotocol (), NULL, Url.gethost (), Url.getport (), Url.getpath (), url.getquery (), NULL);
HttpClient HttpClient = new Defaulthttpclient ();
HttpGet httpget = new HttpGet (URI);
HttpResponse response = Httpclient.execute (HttpGet);

int httpcode = Response.getstatusline (). Getstatuscode ();
String result = entityutils.tostring (response.getentity ());

Logger.info ("get.url=" + URI + ", response code=" + Httpcode + ", response entity=" + result);

return new HTTPRESVO (Httpcode, result);
}

/**
* Post
*
* @param URL
* @param params
* @return return HTTP Code
*
*/
public static HTTPRESVO doPost (string url, string params) throws Exception {
URL posturl = new URL (URL);
Uri uri = new Uri (Posturl.getprotocol (), NULL, Posturl.gethost (), Posturl.getport (), Posturl.getpath (), Posturl.getquery (), NULL);

HttpClient HttpClient = new Defaulthttpclient ();
HttpPost HttpPost = new HttpPost (URI);

stringentity entity = new Stringentity (params, HTTP. UTF_8);
Entity.setcontenttype ("Application/json;charset=utf-8");
Httppost.setheader ("Accept", "Application/json");
Httppost.setentity (entity);

HttpResponse response = Httpclient.execute (HttpPost);
int httpcode = Response.getstatusline (). Getstatuscode ();
String result = entityutils.tostring (response.getentity ());

Logger.info ("post.url=" + URI + ", response code=" + Httpcode + ", response entity=" + result);

return new HTTPRESVO (Httpcode, result);

}

/**
* Remote access based on parameter and URL, get return value
*
* @return
*/
Protected Httpresvo Gethttpresult () throws Exception {
if (Stringutils.isempty (This.method)) {
Logger.error ("method is null.");
return null;
}

Handling of Get methods
if (This.method.equalsIgnoreCase (GET)) {
StringBuffer _url = new StringBuffer (This.url);
if (! Stringutils.isempty (This.parameter)) {
_url.append ("?"). Append (This.parameter);
System.out.println (_url.tostring ());
}
Return Httputil.doget (_url.tostring ());
} else {//POST method processing
if (! Stringutils.isempty (This.parameter)) {

Return Httputil.dopost (This.url, This.parameter);
}
Logger.error ("POST method has parameter none.");
}
return null;
}

}

2.http Response return value

Package com.funshion.common.utils;

/**
* HTTP Response return value
*
* @author Qiuwy
*
*/
public class Httpresvo implements Java.io.Serializable {

public static final int STATUS_OK = 200;

Private static final long serialversionuid = -3063948675189138529l;
private int httpcode;
Private String result;

Public Httpresvo () {

}

Public httpresvo (int httpcode, String result) {
This.httpcode = Httpcode;
This.result = result;
}

public int Gethttpcode () {
return httpcode;
}

public void Sethttpcode (int httpcode) {
This.httpcode = Httpcode;
}

Public String GetResult () {
return result;
}

public void Setresult (String result) {
This.result = result;
}

}

Java HTTP GET and POST requests

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.