SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Member
    Join Date
    May 2009
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sending custom http headers through HttpClient

    I want to use Apache's HttpClient to send http request header. Can anyone take a look into my code and see if it is correct? (I am using msn's web site for testing purpose)

    String url = "http://www.msn.com";
    HttpClient client = new HttpClient();
    GetMethod get = new GetMethod(url);

    String etranAuthStr2 = "PFhNTD48VUlEPlNYWUFOR0VUPC9VSUQ+PEVUUkFOQVVUSD48WFNQUk9MRT5BbGw8L1hTUFJPTEU+PFhTUFNlY3VyaXR5SUQ+MzMzPC9YU1BTZWN1cml0eUlEPjxYU1BTZWN1cml0eUlEPjk5NzwvWFNQU2VjdXJpdHlJRD48WFNQU2VjdXJpdHlJRD5HQjwvWFNQU2VjdXJpdHlJRD48WFNQU2VjdXJpdHlJRD5YMDM8L1hTUFNlY3VyaXR5SUQ+PFhTUFNlY3VyaXR5SUQ+WDAxPC9YU1BTZWN1cml0eUlEPjxYU1BSZWNpcGllbnQ+NDlBQ0NULVNZQU5HRVRSQU48L1hTUFJlY2lwaWVudD48L0VUUkFOQVVUSD48L1hNTD4=";

    get.setRequestHeader("CT_REMOTE_USER", "SXYANGET");
    get.setRequestHeader("ETRANAUTH", etranAuthStr2);

    client.executeMethod(get);

    response.getWriter().print(get.getResponseBodyAsString().toString());

    I use a debugging tool called "Fiddler" to see the request headers being sent, and looks like the headers are not sending over. did I miss anything? anyone has any idea?

  2. #2
    SitePoint Wizard rozner's Avatar
    Join Date
    Oct 2002
    Location
    Paris
    Posts
    1,058
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The way you have it should work fine... Can you maybe run a servlet locally and print all the headers to verify?

  3. #3
    SiteP0int Weazle hooknc's Avatar
    Join Date
    Dec 2004
    Location
    Socialist Republic of Boulder
    Posts
    937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also use a product called HttpTea. It acts as a local proxy and will print out all the information in the request and the response. Quite the nice product.
    baby steps... baby steps...

  4. #4
    SitePoint Member
    Join Date
    May 2009
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your replies.

    Sorry I thought it didn't work, but actually I was wrong, the headers were sent.

    I have another problem. the url I am sending my request to is the login page of another application (written in asp.net). I used "www.msn.com" in my example just for testing purpose. When I sent the request to the login page, it redirected to the home page, but the images and styles were lost.

    Anyone has thoughts? Thank you in advance for your help.

  5. #5
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Images and styles? Login redirects to home page? msn.com?

    I'm lost.

    You're creating a request, it has no images or styles, so...which images and styles are missing?

    What login? msn.com login? This seems pretty specific, why are you using msn as a test?

  6. #6
    SitePoint Member
    Join Date
    May 2009
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I used msn as a test for no specific reason. it was just used to test if the header information is passed to the server. it can be any site. the problem is solved and I have already confirmed that the header was sent successfully. so just forget about msn.

    I need to automatic log in by passing my header information. now it bypass the log in page successfully and goes to the home page. The content of the home page is showing, my only problem is that the look and feel (images and styles) on that page is lost (such as button images, etc.).

  7. #7
    SitePoint Member
    Join Date
    May 2009
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I am doing is to have a link in my java in house application leads to a vendor application, which is written in asp.net.

    As long as the user logs in to my java application and click on the link, the user does not need to login again.

    So I need to send the user information through the http header to the vendor login page (vendor specific requirement).

    I am able to log in now but the look and feel of the page (redirected from the login page) is not right (no images and styles).

    Is it correct to use the following to display the content of the response? am I missing anything?

    response.getWriter().print(get.getResponseBodyAsString().toString());

  8. #8
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Okay...your app is acting as a sort of a proxy to the vendor app, but, most likely, the vendor app uses relative paths for the images and styles, hence, when displayed by your localhost, the paths are invalid == no images or styles.

    So, there's your problem...solution...can you open another window and let the user go to the vendor site rather than trying to play man-in-the-middle?

  9. #9
    SitePoint Member
    Join Date
    May 2009
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your reply.

    I think you are right, but if what you mean is to let user logs in by himself, then it is not an option.

    I must provide single sign on functionality for the user. is there any other options?

  10. #10
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Drop the HttpClient stuff, set the same headers in the request that comes with the doPost method and redirect the user to the vendor app.

  11. #11
    SitePoint Member
    Join Date
    May 2009
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

    I believe the custom headers are not sent if I set the custom headers in the middle of a request. I tried it before.

  12. #12
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    I just looked at setting headers, you would actually do it on the response, which would then be redirected.

    Did you try it and check with Fiddler, which you now know doesn't work?

  13. #13
    SitePoint Member
    Join Date
    May 2009
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had something like the following in my servlet:

    response.setStatus(301);
    response.setHeader("CT_REMOTE_USER", "SXYANGET");
    response.setHeader("ETRANAUTH", "etranAuthStr2");
    response.setHeader( "Location", "http://DNXBETRAN01-V/NBIUATeTRAN/UserLogin.aspx" );
    response.setHeader( "Connection", "close" );

    the custom headers were not passed over in the http header.

  14. #14
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    setHeader is for setting existing headers, you'll need to use addHeader to get what you want.

  15. #15
    SitePoint Member
    Join Date
    May 2009
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I changed it and it still didn't work.

    Thanks any way.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •