3

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();
tzippy
  • 6,458
  • 30
  • 82
  • 151
  • 1
    Enable wire logging (http://hc.apache.org/httpcomponents-client-ga/logging.html#Wire_Logging) and check you are sending with GET the same cookies you receive with POST. – fglez May 28 '12 at 15:26
  • How is the server logging users? sessions, cookies ? – Harry Aug 08 '14 at 00:33

2 Answers2

2

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.

mp911de
  • 17,546
  • 2
  • 55
  • 95
0

I am not familiar with this library but try creating context before calling the post and reuse the same context for the get:

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

    HttpResponse getResponse = client.execute(httpget, context);
jny
  • 8,007
  • 3
  • 37
  • 56
  • No, that does not work. But thanks anyway, appreciate your help! – tzippy Jan 15 '11 at 22:51
  • Ok, I looked at this example:http://stackoverflow.com/questions/678630/how-do-i-make-an-http-request-using-cookies-on-android. HttpClient should have session information, check if it contains session cookies after consuming the post. Also try commenting out System.out from response from the post. You could be consuming input stream that is only consumable once - totally guessing here. – jny Jan 16 '11 at 03:33
  • Tried that both. A cookie is created after the POST. But I still get the sessioncheck error. Maybe it has to do with the URL i'm trying to call being a php getfile URL? But this shouldnt lead to a session error should it? – tzippy Jan 18 '11 at 14:18