In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.