Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java httpclient to log into website and download file - Session Problems!

I log on to a website using the POST method (httpclient from apache). I let the HttpClient execute the HttpPost, let the connection manager release it and then I want to post a GET message that opens a php-URL file to download a pdf. But all I get is the html file of a "session expired" page (println: File: index_GT_neu.html?fehlermeldung=fehler_sessioncheck)

I was thinking that once i used the instance of HttpClient to log on at the site, I would be able to open another URL that is only available after log on. But appearently I was wrong. Somebody could give me a hint? Thanks in advance!

This is what my main looks like:

    // prepare post method
    HttpPost post = new HttpPost("http://epaper02.niedersachsen.com/epaper/index_GT_neu.html");

    //prepare get method
    HttpGet httpget = new HttpGet("http://epaper01.niedersachsen.com/epaper/getfile.php?pdf=0114_GTB_HP_01.pdf&zeitung=GT&ekZeitung=&Y=11&M=01&D=14&C=0");

    // add parameters to the post method
    List <NameValuePair> parameters = new ArrayList <NameValuePair>();
    parameters.add(new BasicNameValuePair("username", "test"));
    parameters.add(new BasicNameValuePair("passwort", "test")); 
    UrlEncodedFormEntity sendentity = new UrlEncodedFormEntity(parameters, HTTP.UTF_8);
    post.setEntity(sendentity); 

    // create the client and execute the post method
    HttpClient client = new DefaultHttpClient();
    HttpResponse postResponse = client.execute(post);

    //Output the Response from the POST
    System.out.print(convertInputStreamToString(postResponse.getEntity().getContent()));

    //releasing POST 
    EntityUtils.consume(postResponse.getEntity());

    //Execute get
    HttpContext context = new BasicHttpContext();   
    HttpResponse getResponse = client.execute(httpget, context);
    System.out.println("Statusline: " + getResponse.getStatusLine());

    if (getResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
        throw new IOException(getResponse.getStatusLine().toString());
    HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
    String currentUrl = URLDecoder.decode(currentReq.getURI().toString(), "UTF-8");

    int i = currentUrl.lastIndexOf('/');
    String fileName = null;
    if (i < 0) {
        fileName = currentUrl;
    } else {
        fileName = currentUrl.substring(i + 1);
    }
    System.out.println("File: " + fileName);

    //Create file
    OutputStream os = new FileOutputStream( fileName);
    InputStream is = getResponse.getEntity().getContent();
    byte[] buf = new byte[4096];
    int read;
    while ((read = is.read(buf)) != -1) {
        os.write(buf, 0, read);
    }
    os.close();

    client.getConnectionManager().shutdown();
like image 606
tzippy Avatar asked Sep 02 '26 10:09

tzippy


1 Answers

By default, DefaultHttpClient does not have a cookie store. A cookie store is needed in order to store cookies that are populated initially or that are obtained while interacting with the HTTP client. As soon as digging into this topic you will start to think about the scope/sharing of cookies.

You can enable the cookie store with one additional line of code:

DefaultHttpClient client = new DefaultHttpClient();
client.setCookieStore(new BasicCookieStore());

I know, this might be a bit late, still HTH.

like image 177
mp911de Avatar answered Sep 04 '26 01:09

mp911de



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!