我無法將HTTP自定義標頭添加到我的Web服務請求中。我需要一個名爲「Authorization」的標頭,並且其值爲「Bearer token_id」,但我無法弄清楚如何做到這一點。Web服務HTTP標頭
使用SoapUI我可以通過按下「添加自定義HTTP標題」按鈕來添加它,它完美的工作,但現在我需要在我的自定義類中實現它。
這是我需要添加我的頭部分:
try {
//SERVICE
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endPointURL));
call.setSOAPActionURI(soapActionURI);
/*
MessageContext msgContext= call.getMessageContext();
MimeHeaders hd = msgContext.getMessage().getMimeHeaders();
hd.addHeader("Authorization", "Bearer token_id");
*/
System.out.println("Authorization WS");
//INVOKE
SOAPEnvelope response = (SOAPEnvelope) call.invoke(new Message(soap.asXML()));
Document doc = new DOMReader().read(response.getAsDocument());
return doc;
}
以下是控制檯輸出:
Authorization WS
AxisFault
faultCode: {http://xml.apache.org/axis/}HTTP
faultSubcode:
faultString: (401)Unauthorized
faultActor:
faultNode:
faultDetail:
{}:return code: 401
<soapenv:Fault xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><faultcode xmlns:axis2ns18="http://schemas.xmlsoap.org/soap/envelope/">axis2ns18:Client</faultcode><faultstring>Authentication Failure</faultstring><detail>Required OAuth credentials not provided</detail></soapenv:Fault>
{http://xml.apache.org/axis/}HttpErrorCode:401
(401)Unauthorized
如果我執行的註釋部分,我在得到一個java.lang.NullPointerException
錯誤MimeHeaders hd = msgContext.getMessage().getMimeHeaders();
line
註釋中的哪行會得到空指針嗎? – Nathan
對不起,這是行:'MimeHeaders hd = msgContext.getMessage()。getMimeHeaders();' – GiLA3