Example Analysis of java + httpclient + post request
Editor to share with you java + httpclient + post request example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
Public void testMethodpost () {
/ / initialize the http request
CloseableHttpClient httpclient=HttpClientBuilder.create () .build ()
String url = "xxxxxxxxxxxxx"
/ / initialize the http request
HttpPost post=new HttpPost (url)
/ / set the request header
Post.setHeader ("Content-Type", "application/json;charset=UTF-8")
Try {
String paramJson= "{\" userName\ ":\" xxxxxxx\ ",\" passWord\ ":\" JVGK+jDLU7\ "}"
/ / JSONObject jsonObject = JSON.parseObject (paramJson)
/ /
/ / System.out.println (jsonObject)
/ / String param=JSON.toJSONString (jsonObject)
/ / submit parameter send request StringEntity requestEntity = new StringEntity (paramJson, "UTF-8"); post.setEntity (requestEntity); CloseableHttpResponse response=httpclient.execute (post); HttpEntity entity=response.getEntity (); String entityStr = EntityUtils.toString (entity, "UTF-8"); System.out.println (entityStr); / / httpclient.close ();} catch (ClientProtocolException e) {e.printStackTrace () } catch (IOException e) {e.printStackTrace ();}} the pit encountered: 1. When using POST data in the previous stage, the UrlEncodedFormEntity () method is used to get the result that the server returns 5000.The printed request parameters are as follows: key=value&key2=value2 format solution: use StringEntity requestEntity = new StringEntity (paramJson, "UTF-8"); the solution above is all the contents of the article "java + httpclient + post request sample Analysis". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!