简体   繁体   中英

Getting HTTP 400 error when trying to download file using Jersey Client

I am able to download the csv file fine if I simpley paste the urlString value in chrome browser.

But, when I am trying to download the file using same urlString with help of below code I get response.getStatus() as 400 error

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

Though, I don't need more than text/plain in my accept param, just to widen the accept, I added more.

Spent lot of time trying to find the issue, please advise. There are many similar questions, but none solves my problem.

I am using following jersey version

    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>1.18.2</version>

Your code is fine in principle.

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

where it is incorporated in a JUnit test. (See code below). If you now put your url into getCSVAsFile(url) it would be possible to tell you what's going on whith a specific url. Probably the

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

line is the culprit. I'd comment it out for a start because it tells the webservice you are calling to send you a specific Representation/MediaType. If there is no match you might end up with an error.

JUnit-Test

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM