简体   繁体   English

Java httpclient登录网站和下载文件 - 会话问题!

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

I log on to a website using the POST method (httpclient from apache). 我使用POST方法登录网站(来自apache的httpclient)。 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. 我让HttpClient执行HttpPost,让连接管理器释放它然后我想发布一条GET消息,打开一个php-URL文件来下载pdf。 But all I get is the html file of a "session expired" page (println: File: index_GT_neu.html?fehlermeldung=fehler_sessioncheck ) 但我得到的是“会话过期”页面的html文件(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. 我在想,一旦我使用HttpClient实例在网站上登录,我就可以打开另一个仅在登录后才可用的URL。 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();

By default, DefaultHttpClient does not have a cookie store. 默认情况下, DefaultHttpClient没有cookie存储。 A cookie store is needed in order to store cookies that are populated initially or that are obtained while interacting with the HTTP client. 需要cookie存储来存储最初填充的cookie或在与HTTP客户端交互时获得的cookie。 As soon as digging into this topic you will start to think about the scope/sharing of cookies. 一旦深入研究这个主题,您就会开始考虑cookie的范围/共享。

You can enable the cookie store with one additional line of code: 您可以使用一行额外的代码启用Cookie存储:

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

I know, this might be a bit late, still HTH. 我知道,这可能有点晚了,还是HTH。

I am not familiar with this library but try creating context before calling the post and reuse the same context for the get: 我不熟悉这个库,但在调用post之前尝试创建上下文,并为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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM