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)