In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to use apache's HttpClient for http communication". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use apache's HttpClient for http communication".
When we use apache's HttpClient library to consume cloud Restful API, we usually need to make two HTTP calls, the first time to obtain a certain token, such as token to prevent cross-domain request forgery attacks on Cross-site request forgery-CSRF, or such as Wechat API's access token, and the second time for real API consumption.
Usually, after the first request, the server will return some cookie fields to the client. In the second request, if you use the postman testing tool or apache's HttpClient library, the cookie field will be automatically appended to the HTTP header of the second request. For details, please refer to another blog I wrote: OData service parallel performance measurement-how to deal with XSRF token in Java Program and JMeter.
Https://blogs.sap.com/2017/08/28/odata-service-parallele-performance-measurement-how-to-deal-with-xsrf-token-in-java-program-and-jmeter/
This article introduces how apache's HttpClient automatically inserts the cookie issued by the server from the first request when sending the second Http request.
First, enter the single-step debugging of HttpClient: the InternalHttpClient.doExecute method:
The origheaders on line 85, which takes out the http request header fields specified by the programmer in the code, such as basic Authentication,content-type,token and so on:
When did this cookie come in?
It seems that we have to go into httpcore-4.4.3.jar, the implementation of apache HttpClient, to debug.
After observation, once I have finished executing the conn.sendRequestHeader method in line 204, I can observe that the Cookie is automatically set, so the secret is in line 204.
Automatically add Content-Length header fields:
Thus it can be seen that the Content-length is calculated automatically through the method entity.getContentLength (), so we programmers do not have to repeat this calculation action in our application code.
Automatically add the host field:
Automatically join Connection: Keep-Alive
Automatic filling of UserAgent: Apache-HttpClient/4.5.1, which doesn't have to be bothered by programmers.
Finally reached the HTTPRequestInterceptor of RequestAddCookies that I am looking for. You can guess from the literal meaning of this class that it has something to do with the Cookie requested by HTTP.
Create a new Cookie where the hpst,path and secure flags in the CookieOrigin constructor can be seen in the Cookie tab of the Chrome developer tool.
Retrieve the Cookie returned by the server from the previous request from Cookie Store:
Here, add the cookie in Cookie store to the header field of the second request, and the riddle is solved.
Thank you for reading, the above is the content of "how to use apache's HttpClient for http communication". After the study of this article, I believe you have a deeper understanding of how to use apache's HttpClient for http communication, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.