HttpClient Post Method (parameter is JSON type)

Source: Internet
Author: User

Package httpclient;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;


Import org.apache.http.*;
Import org.apache.http.client.ClientProtocolException;
Import Org.apache.http.client.config.RequestConfig;
Import org.apache.http.client.entity.UrlEncodedFormEntity;
Import Org.apache.http.client.methods.CloseableHttpResponse;
Import Org.apache.http.client.methods.HttpGet;
Import Org.apache.http.client.methods.HttpPost;
Import Org.apache.http.entity.ContentType;
Import org.apache.http.entity.StringEntity;
Import org.apache.http.impl.client.CloseableHttpClient;
Import org.apache.http.impl.client.HttpClients;
Import Org.apache.http.message.BasicNameValuePair;
Import Org.apache.http.util.EntityUtils;

Import Org.json.JSONObject;
/**
* Created by Jiangcui on 2018/5/21.
*/
public class Httpclientpostforjson {
public static void Main (string[] args) {
Jsonobject Jsonobject =New Jsonobject ();
Jsonobject.append ("Username","Jiangcui");
Jsonobject.append ("Password","pdmi1234");

String URL ="Https://passport.cnblogs.com/user/signin?ReturnUrl=https%3A%2F%2Fwww.cnblogs.com%2F";
String result = Httpclientpostforjson.Dopostforjson (Url,jsonobject);
System.OUT.PRINTLN (result);
}

Static methods that can be called directly
public static string Dopostforjson (string url, Jsonobject jsonparams) {
Closeablehttpclient closeablehttpclient = httpclients.Createdefault ();

Configuring the time-out period
Requestconfig requestconfig = Requestconfig.Custom ()
. Setconnecttimeout (5000)
. Setconnectionrequesttimeout (5000)
. SetSocketTimeout (5000)
. setredirectsenabled (True
. build ();

HttpPost HttpPost =New HttpPost (URL);
Setting the time-out period
Httppost.setconfig (Requestconfig);
Httppost.setheader ("Content-type","Application/json");

Jsonobject jsonobject = null;

try {
stringentity entity =New Stringentity (Jsonparams.tostring (),"Utf-8");
Entity.setcontentencoding ("Utf-8");
Entity.setcontenttype ("Application/json");
Httppost.setentity (entity);

Perform post
Closeablehttpresponse closeablehttpresponse = Closeablehttpclient.execute (HttpPost);
String strrequest ="";
if (Null! = Closeablehttpresponse &&!"". Equals (Closeablehttpresponse)) {
System.Out.println (Closeablehttpresponse.getstatusline (). Getstatuscode ());
if (Closeablehttpresponse.getstatusline (). Getstatuscode () = = Httpstatus.SC_OK) {
Httpentity httpentity = closeablehttpresponse.getentity ();
Strrequest = Entityutils.ToString (httpentity);
}else {
Strrequest ="Error Response" + closeablehttpresponse.getstatusline (). Getstatuscode ();
}
}
return strrequest;

}catch (Clientprotocolexception e) {
E.printstacktrace ();
Return"Protocol Exception";
}catch (ParseException e) {
E.printstacktrace ();
return "Parse exception";
} catch (IOException e) {
E.printstacktrace ();
return "Transmission exception";
} finally {
try {
if (closeablehttpclient! = null) {
Closeablehttpclient.close ();
}
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}

HttpClient Post Method (parameter is JSON type)

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.