[英]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.