Java httpclient to Log in to a Site and Upload a File - Session Problems!

I sign up to the website using the POST method (httpclient from apache). I allow HttpClient to execute HttpPost, let the connection manager issue it, and then I want to post a GET message that opens the php url file to download pdf. But all I get is an html file of the page expired (the println: File: index_GT_neu.html?fehlermeldung=fehler_sessioncheck)

I thought that as soon as I used an instance of HttpClient to log in to the site, I could open another URL, accessible only after logging in. But, apparently, I was mistaken. Can anyone 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();
+3
source share
2 answers

DefaultHttpClient cookie. cookie, HTTP, cookie. , / cookie.

cookie :

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

, , HTH.

+1

, 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);
0

Source: https://habr.com/ru/post/1785639/


All Articles