Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7592869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:00:54+00:00 2026-05-30T21:00:54+00:00

I am having a problem getting the Apache HttpClient to connect to a service

  • 0

I am having a problem getting the Apache HttpClient to connect to a service external to my virtualised development environment.
To access the internet (e.g. api.twitter.com) I need to call a local URL (e.g. api.twitter.com.dev.mycompany.net), which then forwards the request to real host.

The problem is, that to whatever request I send, I get a 404 Not Found response.

I have tried debugging it using wget, and it appears the problem is, that the destination server identifies the desired resource by using both the request URL and the hostname in the Host header. Since the hostname does not match, it is unable to locate the resource.

I have (unsuccessfully) tried to override the Host header by setting the http.virtual-host parameter on the client like this:

HttpClient client = new DefaultHttpClient();
if (envType.isWithProxy()) {
    client.getParams().setParameter(ClientPNames.VIRTUAL_HOST, "api.twitter.com");
}

Technical details:

  1. Client is used as an executor in RESTeasy to call the REST API. So “manually” setting the virtual host (as described here) is not an option.

  2. Everything is done via HTTPS/SSL – not that I think it makes a difference.

Edit 1: Using a HttpHost instead of a String does not have the desired effect either:

HttpClient client = new DefaultHttpClient();
if (envType.isWithProxy()) {
    HttpHost realHost = new HttpHost("api.twitter.com", port, scheme);
    client.getParams().setParameter(ClientPNames.VIRTUAL_HOST, realHost);
}

Edit 2: Further investigation has revealed, that the parameter needs to be set on the request object. The following is the code v. 4.2-aplha1 of HttpClient setting the virtual host:

HttpRequest orig = request;
RequestWrapper origWrapper = wrapRequest(orig);
origWrapper.setParams(params);
HttpRoute origRoute = determineRoute(target, origWrapper, context);

virtualHost = (HttpHost) orig.getParams().getParameter(
            ClientPNames.VIRTUAL_HOST);

paramsare the parameters passed from the client. But the value for ‘virtualHost’ is read from the request parameters.

So this changes the nature of the question to: How do I set the VIRTUAL_HOST property on the requests?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:00:56+00:00Added an answer on May 30, 2026 at 9:00 pm

    ClientPNames.VIRTUAL_HOST is the right parameter for overriding physical host name in HTTP requests. I would just recommend setting this parameter on the request object instead of the client object. If that does not produce the desired effect please post the complete wire / context log of the session (see logging guide for instructions) either here or to the HttpClient user list.


    Follow-up

    OK. Let’s take a larger sledge hammer. One can override content of the Host header using an interceptor.

    DefaultHttpClient client = new DefaultHttpClient();
    client.addRequestInterceptor(new HttpRequestInterceptor() {
    
        public void process(
                final HttpRequest request, 
                final HttpContext context) throws HttpException, IOException {
            request.setHeader(HTTP.TARGET_HOST, "www.whatever.com");
        }
    });
    

    One can make the interceptor clever enough to override the header selectively, only for specific hosts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem getting access to a database which lives on a remote
When using the Apache HttpComponents HttpClient library (4.0.2) I'm having a problem where the
I am having problem getting my tooltip to work when using the jQuery load()
Having a problem getting a TreeView control to display node images. The code below
I am having a problem getting a list of fields from a query defined
I am having a problem getting my project to link with the PhysX libraries
I'm having a problem getting a simple ASP.NET webpage to display. The page contains
I'm having a problem getting a change event to register with the following code:
I'm having a problem getting XCode to deal with a particular file structure that
I am having a problem getting WinDbg to use the PDB files for my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.