简体   繁体   English

使用JAVA APACHE HTTPClient下载HTTP帖子生成的文件

[英]Downloading a file generated by a HTTP post using JAVA APACHE HTTPClient

I have a need to log into a webpage, download a file (that appears after a post) and then log out again. 我需要登录到网页,下载文件(帖子后显示),然后再次注销。

Logging in and out is fine, but I am having some troubles when I try to get to the file. 登录和注销都可以,但是在尝试访问文件时遇到了一些麻烦。

In a web browser I fill in the form once I press OK it does to a webpage saying 'Successful' and a file download box appears. 在Web浏览器中,我按OK后填写表格,它对显示“成功”的网页进行显示,并出现文件下载框。

When I do the same thing via HTTP client (copying the parameters used in FF by firebug) all I get the the response.entity is the webpage, but I cannot see the file anywhere. 当我通过HTTP客户端(通过firebug复制FF中在FF中使用的参数)执行相同的操作时,都得到了response.entity是网页,但是我在任何地方都看不到该文件。 Can someone please shed some light on my problem? 有人可以说明我的问题吗?

Here is my code (I realize it will not at the moment save the response, but if it was working, I would expect some long binary response) 这是我的代码(我意识到它暂时不会保存响应,但是如果它能正常工作,我会期望一些长时间的二进制响应)

// get data
httpost = new HttpPost("https://webpage.com.au/folder/getfile.asp?param=2");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("p1", "0"));
nvps.add(new BasicNameValuePair("p2", "03"));
nvps.add(new BasicNameValuePair("p3", "1"));
nvps.add(new BasicNameValuePair("p4", "6"));
nvps.add(new BasicNameValuePair("p5", "1"));
nvps.add(new BasicNameValuePair("p6", "2"));
nvps.add(new BasicNameValuePair("p7", "1"));
nvps.add(new BasicNameValuePair("p8", "0"));
nvps.add(new BasicNameValuePair("p9", "G"));

httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));   
response = httpclient.execute(httpost);
entity = response.getEntity();
printResponse(entity);

System.out.println("Get file post: " + response.getStatusLine());
EntityUtils.consume(entity);

System.out.println("Post file get cookies:");
printCookies(httpclient);

and the response: 和响应:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="language" content="en-us"/>
<meta name="robots" content="noindex, nofollow"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="-1"/>
<link rel="stylesheet" href="/css/style.css" type="text/css"/>
<script language="javascript">
    var wasSubmitted = false;

    function SingleSubmit () 
    { 
        var OK = !wasSubmitted; 
        wasSubmitted = true; 
        return OK;
    }
</script>
</head>
<body bgcolor="#FFFFFF" text="black" topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">
<br>
<center><br><p class="center"></p><p class="center"></p><p class="center">getting file<br>
  <script language=Javascript>
      document.location = "TransSumExcel.asp";
  </script>
</center>
</body>
</html>

From the response text it appears that the browser is directed to load 'TransSumExcel.asp' by the javascript call 从响应文本来看,似乎通过javascript调用将浏览器定向为加载“ TransSumExcel.asp”

 document.location = "TransSumExcel.asp";

So right after the POST request you need to make a GET request for https://webpage.com.au/folder/TransSumExcel.asp 因此,在POST请求之后,您需要对https://webpage.com.au/folder/TransSumExcel.asp进行GET请求

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

相关问题 使用Apache Commons在Java中下载文件时获取HTTP 302 - Getting HTTP 302 when downloading file in Java using Apache Commons 如何使用apache httpclient和web身份验证进行http发布? - How to do http post using apache httpclient with web authentication? 如何使用java httpclient实现HTTP Post大块文件的上传? - How to implement HTTP Post chunked upload of a big file using java httpclient? JAVA-使用httpclient通过http代理(squid)将文件发布到google应用时,在调用execute时卡住了 - JAVA - Using httpclient to post a file to google apps via http proxy (squid) gets stuck when calling execute 如何使用 Java Apache HttpClient 正确发出 POST 请求? - how to properly make a POST request using Java Apache HttpClient? 使用Java中的REST POST调用下载文件 - Downloading a file using a REST POST call in java 使用HttpClient - Java发送带有HTTP POST请求的XML有效负载 - Sending XML payloads with HTTP POST request using HttpClient--Java Apache HTTPClient流式HTTP POST请求? - Apache HTTPClient Streaming HTTP POST Request? 如何使用apache httpclient 4.3.5将文件发布到受保护的URL - How to post a file to a protected URL using apache httpclient 4.3.5 在使用Apache HttpClient进行HTTP GET / POST时,是否可以将参数设置为Http Session? - Is there a way to set parameters to Http Session on making HTTP GET/POST using Apache HttpClient?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM