访问SOAP服务只能在Android Emulator上运行



我正试图从我的android应用程序访问SOAP服务。

我正在使用测试我的请求http://www.requestmaker.com/所以我最后提出了这个请求:

发送的请求标头:

POST /WebserviceHCX.asmx HTTP/1.1
Content-Type: text/xml;charset=UTF-8
SOAPAction: hcxwords/installAcknowledge
Host: webservice.hcxwords.eu
Accept-Charset: utf-8
Accept: text/xml,application/text+xml,application/soap+xml
Content-Length: 382

请求数据:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <installAcknowledge xmlns="hcxwords">
      <deviceID>test</deviceID>
      <refCode2></refCode2>
    </installAcknowledge>
  </soap:Body>
</soap:Envelope>

由于这在http://www.requestmaker.com/我只是想在我的安卓应用程序中做同样的事情。

    final String SOAPRequestXML = "<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><installAcknowledge xmlns="hcxwords"><deviceID>"+deviceID+"</deviceID><refCode2>"+refCode2+"</refCode2></installAcknowledge></soap:Body></soap:Envelope>";
    String url = "http://webservice.hcxwords.eu/WebserviceHCX.asmx";
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    httppost.addHeader("Content-type", "text/xml;charset=UTF-8"); 
    httppost.addHeader("SOAPAction", "hcxwords/installAcknowledge");
    httppost.addHeader("Host", "webservice.hcxwords.eu");
    httppost.addHeader("Accept-Charset", "utf-8");
    httppost.addHeader("Accept", "text/xml,application/text+xml,application/soap+xml");

    StringEntity se;
    try {
        se = new StringEntity(SOAPRequestXML, HTTP.UTF_8);
        se.setContentType("text/xml;charset=UTF-8");
        se.setContentEncoding("utf-8");
        httppost.setEntity(se);
        // Execute HTTP Post Request
        BasicHttpResponse httpResponse =
                (BasicHttpResponse)httpclient.execute(httppost);
        return httpResponse;
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        throw new IllegalStateException("ClientProtocolException ");
    } catch (IOException e) {
        e.printStackTrace();
        throw new IllegalStateException("IOException ");
    }

这在我的Emulator(200 OK)中工作得很好,在我的Nexus 4上它是(404),而在我的三星平板电脑上它抛出IOException:

IOException:无法解析主机"webservice.hcxwords.eu":没有与>主机名关联的地址

最有可能的原因是模拟器与web服务器在同一网络上(因此可以解析主机名),但设备却不在。

两种可能的解决方案:

  1. 使用VPN将您的设备接入网络(假设网络支持VPN)。JunosPulse是一个很好的免费VPN客户端,你可以下载到你的Android设备上。

  2. 将web服务器暴露在防火墙之外。然而,您可能无法做到这一点。选项1通常是您的最佳选择。

另一种可能是某些东西阻止了设备解析服务器名称。尝试通过IP地址访问服务器,例如:

String url = "http://<ip_address_of_server>/WebserviceHCX.asmx";

最新更新