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

时间:2013-05-16 12:38:40

标签: java apache post download apache-httpclient-4.x

我需要登录网页,下载文件(在帖子后显示),然后再次注销。

登录和退出很好,但是当我尝试访问该文件时遇到了一些麻烦。

在网络浏览器中,我填写表单,一旦我按下OK,它就会显示一个名为“成功”的网页,并显示一个文件下载框。

当我通过HTTP客户端(通过firebug复制FF中使用的参数)执行相同的操作时,我得到的响应是。网页,但我无法在任何地方看到该文件。有人可以对我的问题有所了解吗?

这是我的代码(我意识到它现在不会保存响应,但如果它正常工作,我会期待一些长二进制响应)

// 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);

和回复:

<!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>

1 个答案:

答案 0 :(得分:0)

从响应文本中可以看出,浏览器被定向为通过javascript调用加载“TransSumExcel.asp”

 document.location = "TransSumExcel.asp";

因此,在POST请求之后,您需要为https://webpage.com.au/folder/TransSumExcel.asp

发出GET请求