我正在向服务器发送HttpsURLConnection请求,该服务器需要使用它进行基本身份验证。我已经提供了授权值并对其进行了验证。但是我得到了Http status 500: Internal Server error
。这是我的代码:
public String httptest(String url, File f, String username, String password) throws Exception {
// Task attachments endpoint
HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
// Set basic auth header
String apiKey = username + ":" + password;
String basicAuth = "Basic " + new String(new Base64().encode(apiKey.getBytes()));
connection.setRequestProperty("Authorization", basicAuth);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
logger.info("basicAuth: " + basicAuth);
// Indicate a POST request
connection.setDoOutput(true);
// A unique boundary to use for the multipart/form-data
String boundary = Long.toHexString(System.currentTimeMillis());
String fboundary = "----WebKitFormBoundary" + boundary + "u0gW";
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + fboundary);
// Construct the body of the request
logger.info("boundary: " + fboundary);
PrintWriter writer = null;
try {
writer = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
String fileName = f.getName();
String ffname = fileName.substring(fileName.lastIndexOf("\\")+1);
writer.append("--" + fboundary).append(LINE_FEED);
writer.append("Content-Disposition: multipart/form-data; name=\"file\"; filename=\"" + ffname + "\"").append(LINE_FEED);
writer.append("Content-Type: application/x-zip-compressed").append(LINE_FEED);
writer.append(LINE_FEED);
logger.info("fileName: " + fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
for (String line; (line = reader.readLine()) != null; ) {
writer.append(line).append(LINE_FEED);
}
} finally {
if (reader != null) try {
reader.close();
} catch (IOException logOrIgnore) {
}
}
writer.append(LINE_FEED);
writer.append("----" + fboundary).append(LINE_FEED);
writer.append(LINE_FEED);
writer.flush();
writer.close();
} catch (Exception e) {
System.out.append("Exception writing file" + e);
} finally {
if (writer != null) writer.close();
}
logger.info("ResponseCode: " + connection.getResponseCode());
//System.out.println(connection.getResponseCode()); // Should be 200
return connection.getResponseMessage();
}
当我通过邮递员客户端发送相同的请求时,它工作正常。以下是邮递员请求中的标题:
授权:基本c2dveWFsQHF1YXJrLmNvbTpRdWFyazEyMw == 缓存控制:无缓存邮递员令牌: d6fac5f0-e844-9bac-2bce-51580fdd5557内容类型: 多部分/格式的数据;边界= ---- WebKitFormBoundary7MA4YWxkTrZu0gW
---- WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition:form-data; NAME = “文件”; filename =“Tiger522.zip”内容类型: 应用程序/ x-ZIP压缩的
---- WebKitFormBoundary7MA4YWxkTrZu0gW
请告诉我该怎么做?
答案 0 :(得分:0)
我可以看到两个可能的错误:
正如StephaneM指出的那样:文件上传的内容处理必须是" form-data",而不是" multipart / form-data":
writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + ffname + "\"").append(LINE_FEED);
您指定"内容类型:application / x-zip-compressed"用于文件上传。这意味着zip格式。然而,您只需使用InputStreamReader在那里编写文件。这似乎不是您正在编写的zip格式。