HttpClient dynamically replaces a target when sending a request Ip__java

Source: Internet
Author: User
Problem Description:
HTTP requests are made using httpclient, and a target host is requested at random for each request.

When you send an HTTP request, you typically configure the data source and set the clientpnames.default_host so that the target machine HOST and port are configured clientpnames.default_host at the time of the request. However, you cannot request clientpnames.default_host each time, because a httpclient corresponds to a clientpnames.default_host, you cannot modify the target HOST for a request.

For example:
HttpGet httpget = new HttpGet (URL);
When a request is made, HttpGet executes execute as a parameter, at which point the URL is a relative path, and httpclient will splice the Defaulthost+uri to the full request address for the request.

Look at HttpClient source code can find that the URI is a clue, in the Execute method will determine whether the URI set is an absolute or relative path, if the relative path httpclient will defaulthost+uri stitching as the full request address. If scheme is set in the URI and the URI is configured with the host, it will use the complete absolute path of the URI as the request address. The logic of judgment is as follows

public final HttpResponse Execute (httpurirequest request, HttpContext Context) throws IOException, clientprotocolexception {if (request = = null) {throw new Illegalargumentex
    Ception ("Request must not being null.");
return execute (Request), request, Determinetarget; private static Httphost Determinetarget (Httpurirequest request) throws Clientprotocolexception {//A null target m
    Ay be acceptable if there is a default target.
    Otherwise, the null target is detected in the director.

    Httphost target = null;
    URI RequestUri = Request.geturi ();
        if (Requesturi.isabsolute ()) {target = Uriutils.extracthost (RequestUri); if (target = = null) {throw new Clientprotocolexception ("URI does not specify a valid hos
        T name: "+ RequestUri";
} return target; }
After analysis, we found a way to solve the problem, set the URI at the request, note that the build URI is configured to configure the scheme parameter to take effect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.