Simple beauty jodd HTTP — a basket of applications

Jodd HTTP is a miniature and simple HTTP client, but it is simple and convenient. Using it, you can easily send requests and read responses. Its goal is to make the daily application very simple, so as to simplify the work of developers.

The best way to learn about jodd HTTP is the sample program.

Simple get method

The above code implements a get request and prints the response result.

Let's look at the response results:

Please understand in combination with the figure below

You can also write:

Or build step by step:

OK, let's look at the request style:

Read response message

After sending the message, the response message is stored in the httpresponse instance. You can call the httpresponse method to obtain the statuscode, statusphase or message header properties.

There are three main methods to read the message itself:

Body () -- usually the basic message content in iso-8859-1 coding format.

Bodytext() -- a message compiled according to the encoding format of the "content type" attribute value of the message header.

Bodybyte() -- basic message returned in the form of byte array, such as downloading a file to be saved.

Note: before using the bodytext() method, you must use the charset() method to specify the encoding format.

Query parameters

In the get method, query parameters can be specified in the URL (correct encoding is required), such as:

Or set it through the query() method, such as:

Setting a parameter can use the query () method, or call the query () method once for each parameter.

Of course, you can also use map as a parameter to transfer parameters.

Note: query parameters (message header or form parameters) can be repeated. Internally, they are stored in an array. Use the removequery method to delete parameters or call the query method multiple times to replace parameters.

Query can also read all internal parameters:

Basic certification

Basic certification is simple:

Post method and form parameters

Similar to query in get method:

As you can see, form is used in the post method to specify form parameters. The use of form is exactly the same as that of query.

Upload file

Again, it's very easy, just add the file to the form parameter. Examples are as follows:

That's all the code.

Monitor file upload progress

When uploading large files, you need to monitor the upload progress. Httpprogresslistener provides this function:

Before uploading the file, httpprogresslistener calculates the callback size --- the number of bytes of the file block to be transferred each time. By default, it is 1% of the size of the entire file. Generally, it will not be less than 512 bytes.

Httpprogresslistener contains an internal member size, which represents the size of the entire request. Note: the size of the entire request is not only the file, but also the actual number of bytes of the entire request to be sent, which is usually larger than the size of a large file (see request message format).

Message header

You can use the header () method to add or obtain the message header parameters. Some general header parameters have been defined, so you can query through contenttype() and other similar methods.

Compress and decompress message

Example of unzip () decompression message

Using the body method

You can manually set the content of the request message -- sometimes, some APIs allow you to specify similar commands in the body:

After setting the body, you can set the parameters without the above form () method.

However, it is more meaningful to use the body () method in httpresponse to receive the message content.

Character and encoding format

By default, the query and form parameters are encoded in UTF-8. Globally, it can be reset through joddhttp, or locally as follows:

Of course, you can set the encoding format of the form in a similar way. Further, the value in the message header "content type" is detected when the form is submitted. If it exists, the value will be used. When receiving a message, the body() method usually returns a message in iso-8859-1 encoding format. To return the specified format, you can use the bodytext() method. At this time, you need to set the value of the encoding format "content type" in advance.

httpconnection

HTTPCONNECTION is an interface that encapsulates HTTP physical communication. When the send () method is called, HTTP will open () a connection (when the connection does not exist). Internally, the httpconnectionprovider creates the connection. See the previous article () The default connection provider is socket based. It usually returns a newly created sockethttpconnection instance, which simply wraps an open socket. You can also use a custom httpconnectionprovider. For example, you can extend the sockethttpconnectionprovider and override the createsocket () method to return sockets from the socket pool or sockets with different expiration times.

Socket

As described above, HTTP communication is completed through ordinary sockets. Therefore, you can change the socket behavior before sending data. The following example shows how HTTP changes the socket behavior.

Sockethttpconnection

Call socket through HTTPCONNECTION:

SockethttpconnectionProvider

Another method is to use sockettpconnectionprovider to create its own provider:

Then use the open method to obtain the socket

To set the default connectionprovider of the custom connectionprovider to apply to all communications, you only need to assign it to joddhttp The httpconnectionprovider does not have to explicitly call the open () method.

Keep alive

By default, all connections are closed. HTTP allows you to set the connection to keep alive mode so that the connection always exists. In the keep alive mode, in the communication session, open an HTTP connection at the first request, and reuse the connection later; If it is not necessary, the socket will not be opened again, so it can be reused by multiple requests.

There are many ways to do this, but the simplest way is as follows:

agent

Httpconnectionprovider supports proxy. You only need to provide proxyinfo instance indicating proxy information (type, address, port, user name, password). It supports HTTP / Socks4 / socket5, with the following differences:

Socks: protocol for sessions traversal across firewall secured socks protocol provides a framework for client / server applications in TCP and UDP domains to use the services provided by network firewalls more conveniently and safely. Conceptually, this protocol is a "shim layer" between the application layer and the transport layer, so it does not provide network layer gateway services such as ICMP information transmission. Both Socks4 and Socks5 belong to socks protocol, but there are differences due to different specific applications supported. Socks4 agent only supports TCP applications, while Socks5 agent can support both TCP and UDP applications. However, the Socks5 proxy also supports various authentication mechanisms, server-side domain name resolution, etc; However, there is no Socks4 agent, so usually the socks agents open to the public are Socks4 agents. Therefore, UDP applications are usually not supported. In other words, Socks5 can do whatever Socks4 can do, while Socks4 may not do everything Socks5 can do.

Input stream parsing

Both httprequest and httpresponse implement the method readfrom (InputStream) to read the input stream. The following example illustrates how the server reads requests.

HttpBrowser

When it is necessary to simulate the "move" scenario by changing the target address, it is not enough to just send a request and receive a response. For example, logging in requires reading the contents of different target addresses in the current session.

Httpbrowser can do this. It sends a request, automatically processes 301 and 302 jumps, reads cookies from the response message, stores them in a new request message, and so on. A simple example is as follows:

HttpTunnel

Jodd HTTP provides a flexible method to build HTTP tunnel (tunnel of simple proxy and destination address), which is implemented by httptunnel.

reference:

【1】 http://jodd.org/doc/http.html

【2】 http://www.it165.net/admin/html/201403/2541.html

【3】 http://www.cnblogs.com/skynet/archive/2010/12/11/1903347.html

【4】 http://www.ccproxy.com/socks4-yu-socks5-de-qu-bie-jie-shao.htm

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
分享
二维码
< <上一篇
下一篇>>