簡體   English   中英

訪問SOAP服務僅可從Android Emulator運行

[英]Accessing SOAP service works only from 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/上工作得很好(200 OK),所以我只想在我的android應用中做同樣的事情。

    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 ");
    }

在Nexus 4(404)上,它在我的仿真器(200 OK)中非常有效,在三星平板電腦上,它拋出IOException:

IOException:無法解析主機“ webservice.hcxwords.eu”:沒有與主機名關聯的地址

原因很可能是模擬器與Web服務器位於同一網絡上(因此允許解析主機名),而設備卻不在。

兩種可能的解決方案:

  1. 使用VPN將設備連接到網絡(假設網絡支持VPN)。 JunosPulse是一個很好的免費VPN客戶端,您可以將其下載到Android設備。

  2. 將Web服務器暴露在防火牆之外。 但是,您可能無法執行此操作。 選項1通常是最好的選擇。

另一個可能是js阻止設備解析服務器名稱。 嘗試通過IP地址訪問服務器,例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM