[英]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.