【问题标题】:Getting HTTP 400 error when trying to download file using Jersey Client尝试使用 Jersey 客户端下载文件时出现 HTTP 400 错误
【发布时间】: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&amp;sdjfl=343,正是我在浏览器中使用的,如果我使用 URLEncoder.encode() 对其进行编码并转换为 https%3A ...,我开始收到一个新错误@ 987654329@
  • 看起来不错,请确保您的参数不应包含任何特殊字符和空格。
  • 当然。我正在测试的很简单,只包含字母和数字
  • 你能找到一个我们可以测试的特定公共 url,它会产生同样的问题(即在浏览器中成功而在客户端 api 中失败)。

标签: java web-services rest jersey-client


【解决方案1】:

您的代码原则上没问题。

https://github.com/WolfgangFahl/Mediawiki-Japi/blob/master/src/test/java/com/bitplan/mediawiki/japi/TestGetCSV.java#L49

它被合并到 JUnit 测试中。 (见下面的代码)。 如果您现在将您的 url 放入 getCSVAsFile(url) 中,则可以告诉您特定 url 发生了什么。应该是

WebResource.Builder wb=webResource.accept("application/json,application/pdf,text/plain,image/jpeg,application/xml,application/vnd.ms-excel");

线是罪魁祸首。我会先将其注释掉,因为它会告诉您正在调用的 Web 服务向您发送特定的 Representation/MediaType。如果没有匹配,您最终可能会出错。

JUnit 测试

package com.bitplan.mediawiki.japi;
import static org.junit.Assert.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.client.apache.ApacheHttpClient;
/**
* test CSV access for test code
* @author wf
*
*/
public class TestGetCSV  {
private ApacheHttpClient client;

/**
* get the a CSV File from the given urlString
* http://stackoverflow.com/questions/27224870/getting-http-400-error-when-trying-to-download-file-using-jersey-client
*
* @param urlString
* @param csvFile
* @throws IOException
*/
public void getCSVAsFile(String urlString, File csvFile) throws IOException {
  client = ApacheHttpClient.create();
  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(csvFile);
  fos.write(byteArray);
  fos.flush();
  fos.close();
}

@Test
public void testGetCSV() throws IOException {
  boolean debug=false;
  File csvFile=new File("/tmp/ExampleWikis.csv");
  getCSVAsFile("http://mediawiki-japi.bitplan.com/mediawiki-japi/index.php/Special:Ask/-5B-5BCategory:ExampleWiki-5D-5D-20-5B-5Bsiteurl::%2B-5D-5D/-3FSiteurl/-3FWikiid/format%3Dcsv/offset%3D0", csvFile);
  String csv=FileUtils.readFileToString(csvFile);
  if (debug)
    System.out.println(csv);
  assertTrue(csv.contains("http://waihekepedia.org/"));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多