简体   繁体   English

java.io.IOException:服务器返回的HTTP响应代码:URL的400

[英]java.io.IOException: Server returned HTTP response code: 400 for URL

I want to download CSV file from URL. 我想从URL下载CSV文件。 For that i am using HTTP URL Connection. 为此,我正在使用HTTP URL连接。 While running the code, i am getting below error: 运行代码时,出现以下错误:

 java.io.IOException: Server returned HTTP response code: 400 for URL: https://twc.centercode.com/files/report.aspx/PLS Tracker.csv?t=GetElementFullCSV&r=0668821a036046ef9fde587c609e2c8f&e=47437d179bce477f936dcfdc470bf378
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

Please find below code: 请找到以下代码:

        final int BUFFER_SIZE = 4096;  
        String fileURL = "https://twc.centercode.com/files/report.aspx/PLS Tracker.csv?t=GetElementFullCSV&r=0668821a036046ef9fde587c609e2c8f&e=47437d179bce477f936dcfdc470bf378";  
        String fileName = "EFTReportTest.csv";  
        String saveDir = "D:\\";  
        URL url = new URL(fileURL);  
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();  
        int responseCode = httpConn.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) {
        InputStream inputStream = httpConn.getInputStream();
        String saveFilePath = saveDir + "" + fileName;
        FileOutputStream outputStream = new FileOutputStream(saveFilePath);
        int bytesRead = -1;
        byte[] buffer = new byte[BUFFER_SIZE];
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }

        outputStream.close();
        inputStream.close();

        System.out.println("File downloaded");
      }
         else{
            System.out.println("No file to download. Server replied HTTP code: " + responseCode);
        }
        httpConn.disconnect();

Please let me know what i am doing wrong here. 请让我知道我在这里做错了。

HTML code 400 is "Bad request" (See here) HTML代码400为“错误请求” (请参见此处)

Basically, what you're sending to the server is invalid or not formatted in the way it's expecting. 基本上,您发送到服务器的内容无效或未按照预期方式格式化。 This could be due to user agent, security, etc. I don't know enough about the website in question to tell you how they expect it, you might want to check the site for any information about an API or likewise that you could use. 这可能是由于用户代理,安全性等引起的。对于所涉及的网站,我知之甚少,无法告诉您他们对它的期望如何,您可能希望检查该网站以获取有关API的任何信息,或者同样可以使用的信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 服务器返回 java.io.IOException:服务器返回 HTTP 响应代码:URL 为 400 - server returns java.io.IOException: Server returned HTTP response code: 400 for URL 服务器返回URL的HTTP响应代码:400:java.io.IOException - Server returned HTTP response code: 400 for URL : java.io.IOException java.io.IOException:服务器返回HTTP响应代码:400表示URL - java.io.IOException: Server returned HTTP response code: 400 for URL 获取java.io.IOException:服务器返回的HTTP响应代码:URL为400,但可以在浏览器中正常访问 - Get an java.io.IOException: Server returned HTTP response code: 400 for URL but can access it fine in browser URLConnection错误 - java.io.IOException:服务器返回HTTP响应代码:URL为400 - URLConnection Error - java.io.IOException: Server returned HTTP response code: 400 for URL java.io.IOException:服务器为URL返回HTTP响应代码:400 - java.io.IOException: Server returned HTTP response code: 400 for URL: HttpURLConnection错误-java.io.IOException:服务器返回的HTTP响应代码:URL的400 - HttpURLConnection Error - java.io.IOException: Server returned HTTP response code: 400 for URL 获取 java.io.IOException:服务器返回 HTTP 响应代码:URL 的 400:使用返回 400 状态代码的 url 时 - Getting java.io.IOException: Server returned HTTP response code: 400 for URL: when using a url which return 400 status code java.io.IOException:HTTP响应代码:400用于URL - java.io.IOException: HTTP response code: 400 for URL java.io.IOException:服务器返回HTTP响应代码:502 - java.io.IOException: Server returned HTTP response code: 502
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM