Java implementation HTTP Interface

Source: Internet
Author: User

Package com.lvxh.service;

Import java.net.HttpURLConnection;
Import Java.net.URL;
Import Java.nio.charset.StandardCharsets;

Import Org.apache.commons.io.IOUtils;
Import Org.apache.commons.lang3.StringUtils;
Import Org.apache.http.HttpResponse;
Import org.apache.http.client.HttpClient;
Import org.apache.http.client.entity.UrlEncodedFormEntity;
Import Org.apache.http.client.methods.HttpPost;
Import org.apache.http.impl.client.HttpClients;
Import Org.apache.http.util.EntityUtils;

Import com.lvxh.bean.NameValuePairList;

/**
* HTTP Tool class
*
* @author Lvxh
* @since V100R001C02
* @version V100R001C02
*/
Public abstract class Httputil {


/**
* Usage String url= "http://192.168.2.181:8080/sic/app/login.action"; Map
* <string,string>map = new hashmap<> (); Map.put ("Domaincode", "SA60");
* Map.put ("LoginName", "root"); Map.put ("Password", "Rootroot");
* SYSTEM.OUT.PRINTLN (Sendsms (Url,map));
*
* @param URL
* String Request Address
*
* @param list
* List of key-value pairs
*
* @return String Returns the result set
*/
public static string post (string url, namevaluepairlist list) {
HttpClient HttpClient = Httpclients.createdefault ();
HttpPost HttpPost = new HttpPost (URL);
String strresult = "";
try {
Httppost.addheader ("Content-type", "application/x-www-form-urlencoded");
Httppost.setentity (New Urlencodedformentity (List.getlist (), "UTF-8"));

HttpResponse response = Httpclient.execute (HttpPost);
if (Response.getstatusline (). Getstatuscode () = = 200) {
strresult = entityutils.tostring (Response.getentity (), standardcharsets.utf_8);
} else {
strresult = string.valueof (Response.getstatusline (). Getstatuscode ());
}
} catch (Exception e) {
E.printstacktrace ();
}
return strresult;
}

/**
*. Send HTTP requests in JSON (Application/json) and return the corresponding messages in real time
*
* @param URL
* Request URL
* @param data
* Request data
* @return Request the appropriate
*/
public static string sendjsonpostimmediate (string URL, string data) {
Return sendpost (URL, data.replaceall ("\\\\u003d", "="), "Application/json", 1000);
}

/**
*. Send the HTTP request in JSON (Application/json) and return the corresponding message
*
* @param URL
* Request URL
* @param data
* Request data
* @return Request the appropriate
*/
public static string Sendjsonpost (string url, string data,int timeout) {
Return sendpost (URL, data.replaceall ("\\\\u003d", "="), "Application/json", timeout);
}

/**
*. Send the HTTP request as a form (application/x-www-form-urlencoded) and return the corresponding message
*
* @param URL
* Request URL
* @param data
* Request data
* @return Request the appropriate
*/
public static string Sendformpost (string url, string data,int timeout) {
Return sendpost (URL, data.replaceall ("\\\\u003d", "="), "application/x-www-form-urlencoded", timeout);
}


/**
*. send an HTTP request and return the appropriate message
*
* @param URL
* Request URL
* @param data
* Request data
* @param contentType
* Request Type
* @param timeout
* Request Timeout setting
* @return Request the appropriate
*/
private static string Sendpost (string URL, string data, string contentType, int timeout) {
StringBuffer SBF = new StringBuffer ();
HttpURLConnection connection = null;
try {
Connection = (httpurlconnection) new URL (URL). OpenConnection ();
Connection.setdoinput (TRUE);
Connection.setdooutput (TRUE);
Connection.setrequestmethod ("POST");
Connection.setconnecttimeout (timeout);
Connection.setreadtimeout (timeout);
Connection.setusecaches (FALSE);
Connection.setinstancefollowredirects (TRUE);
Connection.setrequestproperty ("Content-type", ContentType);
Connection.connect ();

Ioutils.write (data, Connection.getoutputstream (), standardcharsets.utf_8);

For (String Line:IOUtils.readLines (Connection.getinputstream (), standardcharsets.utf_8)) {
Sbf.append (line);
}
} catch (Exception e) {
}
finally{
if (connection! = null) {
Connection.disconnect ();
}
}
return sbf.tostring ();
}

/**
* Usage String url= "http://192.168.2.181:8080/sic/app/login.action"; Map
* <string,string>map = new hashmap<> (); Map.put ("Domaincode", "SA60");
* Map.put ("LoginName", "root"); Map.put ("Password", "Rootroot");
* SYSTEM.OUT.PRINTLN (Sendsms (Url,map));
*
* @param
* The type of the desired object
*
* @param URL
* String Request Address
*
* @param list
* List of key-value pairs
*
* @param classoft
* Classoft Conversion type
*
* @return Object Returns the result set
*/
public static <T> T getpostobj (String URL, namevaluepairlist list, class<t> classoft) {
String jsonstring = post (URL, list);
if (Stringutils.isempty (jsonstring)) {
return null;
}
Return Jsonutil.getobject (jsonstring, Classoft);
}
}

Java implementation HTTP Interface

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.