我需要從ksoap響應中獲取http狀態碼,以指定例外情況,如403-未授權(我使用客戶端證書進行身份驗證 - 當證書無效時)。 我試圖在ResponseProperties中找到一個http狀態(應該是我認爲的),但是我沒有。KSOAP2:獲取Web服務響應的http狀態碼
代碼:
...
HttpsTransportSE transport=new HttpsTransportSE(host, port, service, timeout);
try {
SoapObject request=new SoapObject(namespace, method);
request.addProperty("param", param);
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
transport.call(getStatusAction, envelope);
SoapPrimitive response=(SoapPrimitive)envelope.getResponse();
nRet=Integer.parseInt(response.toString());
}
catch(Exception e) {
// Here I can get HeaderProperties by transport.getConnection().getResponseProperties();
// But http status code is not present
e.printStackTrace();
}
...
感謝。
感謝breceivemail,但我使用KSOAP2並沒有直接訪問HttpURLConnection。用示例代碼更新了問題。 –
Aha ...你在調用一些SOAP代碼之前將它作爲預檢查。謝謝,我試過了,這很有幫助。當我不發送客戶證書時,我得到了正確的403迴應。但是,當我的證書不存在於服務器配置中時,我沒有401-Unauthorized,但拋出了IOException「接收到認證挑戰爲空」。很明顯,但401會很精確。 –
所以選擇它作爲答案,如果它有幫助。 – breceivemail