Java sending httpclient request and receiving request result

I

1. Write an httprequestutils tool class, including post request and get request

2. Write business code and send HTTP request

3. MVC profile setting controller scan directory

4. Receive HTTP request

Receive post request

Receive get request

II

get

post

III

I don't need to say more about the importance of HTTP protocol. Compared with the urlconnection of traditional JDK, It increases ease of use and flexibility (the specific differences will be discussed later). It not only makes it easy for the client to send HTTP requests, but also facilitates developers to test the interface (based on HTTP protocol), which not only improves the development efficiency, but also facilitates the improvement of code robustness. Therefore, mastering httpclient is a very important compulsory content. After mastering httpclient, I believe I will have a deeper understanding of HTTP protocol.

1、 Introduction

Httpclient is a sub project under Apache Jakarta common, which is used to provide an efficient, up-to-date and feature rich client programming toolkit supporting HTTP protocol, and it supports the latest versions and suggestions of HTTP protocol. Httpclient has been used in many projects, such as the other two famous open source projects cactus and htmlunit on Apache Jakarta.

2、 Characteristics

1. Based on standard and pure java language. Http1.0 is implemented 0 and http1 one

2. All HTTP methods (get, post, put, delete, head, options, and trace) are implemented in an extensible object-oriented structure.

3. Support HTTPS protocol.

4. Establish transparent connection through HTTP proxy.

5. Use the connect method to establish the HTTPS connection of the tunnel through the HTTP proxy.

6. Basic, digest, ntlmv1, NTLMv2, ntlm2 session, snpnego / Kerberos authentication scheme.

7. Plug in user-defined authentication scheme.

8. Portable and reliable socket factory makes it easier to use third-party solutions.

9. The connection manager supports multi-threaded applications. It supports setting the maximum number of connections, setting the maximum number of connections per host, and discovering and closing expired connections.

10. Automatically process cookies in set cookies.

11. Plug in custom cookie policy.

12. The output stream of request can avoid directly buffering the contents of the stream to the socket server.

13. The input stream of response can effectively read the corresponding content directly from the socket server.

14. In http1 0 and http1 1 uses keepalive to maintain a persistent connection.

15. Directly obtain the response code and headers sent by the server.

16. Ability to set connection timeout.

17. Experimental support http1 1 response caching。

18. The source code is based on the Apache license and can be obtained free of charge.

3、 Method of use

Using httpclient to send a request and receive a response is very simple. Generally, the following steps are required.

1. Create an httpclient object.

2. Create an instance of the request method and specify the request URL. If you need to send a get request, create an httpget object; If you need to send a post request, create an httppost object.

3. If you need to send request parameters, you can call the setparams (hetpparms) method common to httpget and httppost to add request parameters; For httppost objects, you can also call setentity (httpentity) method to set request parameters.

4. Call execute (httpurirequest request) of httpclient object to send a request. This method returns an httpresponse.

5. Call getallheaders(), getheaders (string name) and other methods of httpresponse to obtain the response header of the server; Call the getentity () method of httpresponse to get the httpentity object, which wraps the response content of the server. The program can obtain the response content of the server through this object.

6. Release the connection. The connection must be released whether or not the execution method is successful

4、 Instance

The above simple example of Java sending httpclient request and receiving request results is all the content shared by Xiaobian. I hope it can give you a reference and support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>