我正在使用apache轴在Java项目中发送SOAP请求。因此,我想通过重写createCall()方法来添加HTTP标头以进行授权:
private java.lang.String headers;
// then there are codes to fulfill headers which will be a stringified JSON
@Override
public Call createCall() {
_call = new org.apache.axis.client.Call(this) {
@Override
public void setRequestMessage(Message msg) {
super.setRequestMessage(msg);
MimeHeaders mimeHeaders = msg.getMimeHeaders();
if( headers != null )
{
JSONObject jsonHeaders = null;
try {
jsonHeaders = new JSONObject(headers.trim());
Iterator<String> keys = jsonHeaders.keys();
while (keys.hasNext()) {
String key = keys.next();
if (jsonHeaders.get(key) instanceof String) {
String value = jsonHeaders.get(key).toString();
mimeHeaders.addHeader(key, value);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
};
return _call;
}
但是会导致错误异常:
(400)Bad Request
AxisFault
faultCode: {http://xml.apache.org/axis/}HTTP
faultSubcode:
faultString: (400)Bad Request
faultActor:
faultNode:
faultDetail:
{}:return code: 400
{http://xml.apache.org/axis/}HttpErrorCode:400
我可以正确设置标题吗?
答案 0 :(得分:0)
发现这是标题格式错误。标头的键必须带有大写的首字母,所以我添加:
key = key.substring(0, 1).toUpperCase() + key.substring(1);
mimeHeaders.addHeader(key, value);