java HttpUrlConnection无法使用setRequestProperty
有人对发生的事情有解释吗
下面的代码片段显示了我是如何准备连接的
问题是:
什么时候
connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
添加后,我会得到一个400 Bad request
,但是如果我删除该行,它就会工作
@Override
protected void prepareConnection(HttpURLConnection connection)
throws IOException {
String userPassword = taskProperties.getCadyUser() + ":" + taskProperties.getCadyPass();
String encodedAuthorization = new String(Base64.encode(userPassword.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
connection.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
super.prepareConnection(connection);
}
工作示例:
@Override
protected void prepareConnection(HttpURLConnection connection)
throws IOException {
String userPassword = taskProperties.getCadyUser() + ":" + taskProperties.getCadyPass();
String encodedAuthorization = new String(Base64.encode(userPassword.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
connection.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
super.prepareConnection(connection);
}
有人能解释这种现象吗
共 (0) 个答案