Results 1 to 2 of 2
  1. #1
    mpahlenig is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default httpClient API to Download a xls file.

    Hi,

    I'm writing a code to download xls file from a website, I first need to login then get download the file. This does not work and I don't know what I'm missing. I new to httpClient.

    Below is part of View Source that I got from the website with login part of it.

    <h1 class="PageTitle">Login</h1> *<br />
    Already*have a JSE*account? Login here. <br />
    <table id="ctl00_Column1_sitefinity_usercontrols_login_lo gincontrol_ascx1_Login1" cellspacing="0" cellpadding="0" border="0" style="width:100%;border-collapse:collapse;">
    <tr>
    <td>
    <div id="ctl00_Column1_sitefinity_usercontrols_login_lo gincontrol_ascx1_Login1_pnlLogin" onkeypress="javascript:return WebForm_FireDefaultButton(event, 'ctl00_Column1_sitefinity_usercontrols_login_login control_ascx1_Login1_LoginButton')">

    <table >
    <!--<tr>
    <td colspan="2" class="info_box_main_header">
    Sign up or log in
    </td>
    </tr>-->
    <tr>
    <td style="width:30%;"><label for="ctl00_Column1_sitefinity_usercontrols_login_l ogincontrol_ascx1_Login1_UserName" id="ctl00_Column1_sitefinity_usercontrols_login_lo gincontrol_ascx1_Login1_UserNameLabel" class="defaultLabel">User Name:</label></td>
    <td style="width:70%;">
    <input name="ctl00$Column1$sitefinity_usercontrols_login_ logincontrol_ascx1$Login1$UserName" type="text" id="ctl00_Column1_sitefinity_usercontrols_login_lo gincontrol_ascx1_Login1_UserName" class="defaultTextBox" />
    <span id="ctl00_Column1_sitefinity_usercontrols_login_lo gincontrol_ascx1_Login1_UserNameRequired" title="User Name is required." style="color:Red;visibility:hidden;"><img id="ctl00_Column1_sitefinity_usercontrols_login_lo gincontrol_ascx1_Login1_Image8" src="App_Themes/JSE_ContentPage/Images/exclamation-squaresign-glow.png" align="middle" style="border-width:0px;" /></span>
    </td>
    </tr>
    <tr>
    <td style="width:30%;"><label for="ctl00_Column1_sitefinity_usercontrols_login_l ogincontrol_ascx1_Login1_Password" id="ctl00_Column1_sitefinity_usercontrols_login_lo gincontrol_ascx1_Login1_PasswordLabel" class="defaultLabel">Password:</label></td>
    <td style="width:70%;">
    <input name="ctl00$Column1$sitefinity_usercontrols_login_ logincontrol_ascx1$Login1$Password" type="password" id="ctl00_Column1_sitefinity_usercontrols_login_lo gincontrol_ascx1_Login1_Password" class="defaultTextBox" />
    <span id="ctl00_Column1_sitefinity_usercontrols_login_lo gincontrol_ascx1_Login1_PasswordRequired" title="Password is required." style="color:Red;visibility:hidden;"><img id="ctl00_Column1_sitefinity_usercontrols_login_lo gincontrol_ascx1_Login1_Image4" src="App_Themes/JSE_ContentPage/Images/exclamation-squaresign-glow.png" align="middle" style="border-width:0px;" /></span>
    </td>
    </tr>
    <tr>
    <td colspan="2" align="right">
    <span class="defaultCheckBox"><label for="ctl00_Column1_sitefinity_usercontrols_login_l ogincontrol_ascx1_Login1_RememberMe">Remember me next time.</label><input id="ctl00_Column1_sitefinity_usercontrols_login_lo gincontrol_ascx1_Login1_RememberMe" type="checkbox" name="ctl00$Column1$sitefinity_usercontrols_login_ logincontrol_ascx1$Login1$RememberMe" /></span>
    </td>
    </tr>
    <tr>
    <td colspan="2" align="right">
    <a id="ctl00_Column1_sitefinity_usercontrols_login_lo gincontrol_ascx1_Login1_LoginButton" class="loginButton" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$Column1$sitefi nity_usercontrols_login_logincontrol_ascx1$Login1$ LoginButton&quot;, &quot;&quot;, true, &quot;ctl00$Login1&quot;, &quot;&quot;, false, true))">LOGIN &nbsp;<img id="ctl00_Column1_sitefinity_usercontrols_login_lo gincontrol_ascx1_Login1_Image1" src="App_Themes/JSE_Landing/Images/grey_arrowbtn.png" alt="My JSE Login" align="middle" style="border-width:0px;" /></a>

    </td>
    </tr>
    <tr>
    <td colspan="2" align="right" valign="middle">

    </td>
    </tr>
    <tr>
    <td colspan="2" align="center">

    <div id="ctl00_Column1_sitefinity_usercontrols_login_lo gincontrol_ascx1_Login1_ValidationSummary1" style="color:Red;display:none;">

    </div>
    </td>
    </tr>
    </table>

    </div>
    </td>
    </tr>
    </table>
    </td>

    My sample code

    NameValuePair data[] = {
    new NameValuePair("ctl00$Column1$sitefinity_usercontro ls_login_logincontrol_ascx1$Login1$UserName",confi g.getString(Constants.JSE_USERNAME).trim()),
    new NameValuePair("ctl00$Column1$sitefinity_usercontro ls_login_logincontrol_ascx1$Login1$Password", config.getString(Constants.JSE_PASSWORD).trim()),
    new NameValuePair("onkeypress","ctl00$Column1$sitefini ty_usercontrols_login_logincontrol_ascx1$Login1$Lo ginButton")
    };


    authpost = new PostMethod("/login.aspx");
    authpost.setQueryString("ReturnUrl=%2fCAPAD_Restri cted%2fCA_Schedule.xls");
    authpost.setRequestBody(data);
    //authpost.setDoAuthentication(true);
    int statusCode = client.executeMethod(authpost);
    authpost.releaseConnection();
    logger.info("Status code: "+authpost.getStatusLine());

    //for (Header h : authpost.getResponseHeaders())
    //logger.info(h);
    if(statusCode != -1)
    {
    Header headerGET = authpost.getResponseHeader("location");
    if(headerGET != null)
    {

    String newuriGET = headerGET.getValue();
    if(newuriGET == null || newuriGET.equals(""))
    {
    newuriGET = "/";
    }

    redirect = new GetMethod(newuriGET);
    statusCode = client.executeMethod(redirect);
    byte[] responseBody = redirect.getResponseBody();
    FileOutputStream outStream = new FileOutputStream(config.getString(Constants.JSE_OU TPUT_FILE).trim());
    outStream.write(responseBody);
    redirect.releaseConnection();

    }
    }

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    Maybe the site is first sending a 'session cookie', or expecting the cookie to be present on the second request (after logging in). This is possible with the
    Java Code:
    Cookie cookies[] = httpClient.getState().getCookies();
    after the invoke the post operation to the site.
    and then
    Java Code:
        HttpState initialState = new HttpState();
        for (Cookie cookie : cookies) {
          initialState.addCookie(cookie);
       }
       httpClient.setState(initialState);
    though if you use the same httpClient instance for both the method invocations, this should be done automatically for you.

    Java Code:
    HttpClient httpClient = new HttpClient();
    
    PostMethod loginPost = new PostMethod();
    // set name-value pairs
    httpClient.invokeMethod(loginPost);
    loginPost.releaseConnection();
    
    GetMethod getMethod = new GetMethod();
    // set up
    httpClient.invokeMehtod(getMethod);
    
    // read response
    getMethod.releaseConnection()
    It is possible that the application is expecting the 'session cookie' to already be on the initial login post request. that is, Just trying to do the first post to log in won't work without the session cookie on that request. To get around this, before doing the login post operation, do a dummy 'get' operation to some known url in the site, such as read the login page, or some other unprotected home page within the application. This will fetch the cookies (or you can fetch them from the response yourself).

    It's also possible the form post is not even working, such as the javascript on-submit handler is not doing a form post, but some other kind of login process, such as an invoke a handler first using ajax?

    A good utility to help see what is going on is to use a firefox web browser with the "firebug" and "live http headers" plugins installed, so you can see a log of every network message operation, and the contents of every request/response, what cookies, and what javascript things got ran. sometimes its helpful to see that one magic hidden field that isn't being set to the right value.

    And of course, having a bit of patience to try the different combinations of coding to simulate the exact network response that would happen if a human user did the request in firefox browser.

Similar Threads

  1. how to download file from ftp server
    By santhosh_el in forum Networking
    Replies: 0
    Last Post: 11-19-2009, 06:01 AM
  2. Download file from URL?
    By makpandian in forum Networking
    Replies: 0
    Last Post: 03-10-2009, 12:31 PM
  3. Download file from URL?
    By makpandian in forum New To Java
    Replies: 0
    Last Post: 03-10-2009, 12:30 PM
  4. File Download
    By AnGuRuSO in forum New To Java
    Replies: 9
    Last Post: 10-29-2008, 11:00 AM
  5. file download
    By abhiN in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 09:10 AM

Posting Permissions

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