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.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 Httpclientpostformap {
public static void Main (string[] args) {
map<string, object> map =New hashmap<string, object> ();
Map.put ("Username","Jiangcui");
Map.put ("Password","chenzx0918");
String URL ="Https://passport.cnblogs.com/user/signin?ReturnUrl=https%3A%2F%2Fwww.cnblogs.com%2F";
String result = Httpclientpostformap.DoPost (URL, map);
System.OUT.PRINTLN (result);
}
static method, the class name can be called directly
public static string DoPost (string url, map<string, object> paramsmap) {
Closeablehttpclient closeablehttpclient = httpclients.Createdefault ();
Configure connection time-outs
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);
Assemble POST request parameters
list<namevaluepair> list =New Arraylist<namevaluepair> ();
For (String Key:paramsMap.keySet ()) {
List.add (New Basicnamevaluepair (Key, String.ValueOf (Paramsmap.get (key)));
}
try {
Encode parameters into the appropriate format, such as encoding key-value pairs as param1=value1¶m2=value2
Urlencodedformentity urlencodedformentity =New Urlencodedformentity (list,"Utf-8");
Httppost.setentity (urlencodedformentity);
Perform a POST request
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 map type)