HttpClient Post Method (parameter is map 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.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&param2=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)

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.