【发布时间】:2014-12-01 09:04:18
【问题描述】:
如果我在 chrome 浏览器中简单地粘贴 urlString 值,我可以下载 csv 文件。
但是,当我尝试使用相同的 urlString 在下面的代码的帮助下下载文件时,我得到 response.getStatus() as 400 错误
WebResource webResource = client.resource(urlString);
WebResource.Builder wb=webResource.accept("application/json,application/pdf,text/plain,image/jpeg,application/xml,application/vnd.ms-excel");
ClientResponse response =wb.get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("HTTP error code : "
+ response.getStatus());
}
InputStream input = response.getEntity(InputStream.class);
byte[] byteArray = org.apache.commons.io.IOUtils.toByteArray(input);
FileOutputStream fos = new FileOutputStream(new File(fileToSave));
fos.write(byteArray);
fos.flush();
fos.close();
不过,我的接受参数中不需要超过text/plain,只是为了扩大接受范围,我添加了更多。
花了很多时间试图找到问题,请指教。 有很多类似的问题,但没有一个能解决我的问题。
我正在使用以下球衣版本
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.18.2</version>
【问题讨论】:
-
你能分享你的网址格式吗? URL 应该是 HTML 编码形式。
-
URL 格式是
https://something/something1?something=jsdfjs&sdjfl=343,正是我在浏览器中使用的,如果我使用 URLEncoder.encode() 对其进行编码并转换为 https%3A ...,我开始收到一个新错误@ 987654329@ -
看起来不错,请确保您的参数不应包含任何特殊字符和空格。
-
当然。我正在测试的很简单,只包含字母和数字
-
你能找到一个我们可以测试的特定公共 url,它会产生同样的问题(即在浏览器中成功而在客户端 api 中失败)。
标签: java web-services rest jersey-client