1

我需要从 ksoap 响应中获取 http 状态代码以指定 403-unauthorized 等异常(我使用客户端证书进行身份验证 - 当证书无效时)。我试图在 ResponseProperties 中找到一个 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();
}
...

谢谢。

4

2 回答 2

2

进入游戏很晚,这些天使用 SOAP 的人并不多......

当您捕获 org.ksoap2.transport.HttpResponseException 而不是 Exception 时,您可以获得 HTTP 响应代码

} catch (HttpResponseException httpException) {
    int statusCode = httpException.getStatusCode();
    List responseHeaders = httpException.getResponseHeaders();
} catch (Exception e) {
    e.printStackTrace();
}

响应标头是标头属性对象,例如

0 = {HeaderProperty@4668} 
 key = null
 value = "HTTP/1.1 404 Not Found"
1 = {HeaderProperty@4669} 
 key = "Content-Length"
 value = "0"
于 2016-12-23T09:50:45.880 回答
0
    URL myurl = new URL(url);
    URLConnection connection = myurl.openConnection();
    connection.setConnectTimeout(20 * 1000);
    HttpURLConnection httpConnection = (HttpURLConnection) connection;
    int responseCode = -1;

    if (responseCode == HttpURLConnection.HTTP_OK)
    {
        httpConnection.disconnect();
        SoapObject request = new SoapObject(namespace, methodName);
        //...
    }
    else if(...)
    {
        //...
    }
于 2012-05-14T12:09:11.073 回答