从移动应用程序调用localhost Web服务时出现访问被拒绝错误

时间:2016-11-15 16:43:32

标签: android asp.net web-services sqlite iis

我正在构建Web服务,以便使用IIS 7在设备SQLite和服务器MS SQL数据库之间复制数据。从移动浏览器执行Web服务,而不会出现WiFi问题。执行transporte.call后,我收到访问被拒绝错误。我试图从三星Note Edge设备执行。

请注意,使用VB.Net我能够从网络上的另一台计算机成功使用这两种Web服务方法。

感谢。

主要活动:

    import android.os.StrictMode;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.Toast;
    import org.json.JSONArray;
    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapPrimitive;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;

    public class MainActivity extends AppCompatActivity {
        private final String NAMESPACE = "http://192.168.0.2/";
        private final String URL = "http://192.168.0.2/MimsWeb/MimsWeb.asmx";
        private final String SOAP_ACTION = "MimsWeb";
        private final String METHOD_NAME = "RetrieveS";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            if (android.os.Build.VERSION.SDK_INT > 9) {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
            }
            SoapObject  request = new SoapObject(NAMESPACE,METHOD_NAME);
            request.addProperty("select * from Product","Prduct");

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);

            try {
                HttpTransportSE transporte = new HttpTransportSE(URL);
                transporte.call(SOAP_ACTION,envelope);

                SoapPrimitive resultadoXML = (SoapPrimitive)envelope.getResponse();
        //Log.e("Valor de respuesta",resultadoXML.toString());
        String resultado = resultadoXML.toString();
    }catch (Exception e){
    e.printStackTrace();
            Toast.makeText(getBaseContext(),e.getMessage().toString(),Toast.LENGTH_LONG).show();
        }

        }
    }

清单:

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
            <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
            <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>

日志

    I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
    I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
    W/art: Debugger attempted to resume all threads without having suspended them all before.
    W/System.err: java.net.SocketException: socket failed: EACCES (Permission denied)
    Disconnected from the target VM, address: 'localhost:8606', transport: 'socket'

网络服务网址:

    http://192.168.0.2/MimsWeb/MimsWeb.asmx


    The following operations are supported. For a formal definition, please review the Service Description.

    InsertS
    RetrieveS

检索:

    http://192.168.0.2/MimsWeb/MimsWeb.asmx?op=RetrieveS


    Click here for a complete list of operations.

    RetrieveS

    Test

    To test the operation using the HTTP POST protocol, click the 'Invoke' button.
    Parameter   Value
    s:  
    t:  
    Invoke
    SOAP 1.1

    The following is a sample SOAP 1.1 request and response. The    placeholders shown need to be replaced with actual values.

    POST /MimsWeb/MimsWeb.asmx HTTP/1.1
    Host: 192.168.0.2
    Content-Type: text/xml; charset=utf-8
    Content-Length: length
    SOAPAction: "http://192.168.0.2/RetrieveS"

    <?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>
        <RetrieveS xmlns="http://192.168.0.2/">
        <s>string</s>
        <t>string</t>
        </RetrieveS>
    </soap:Body>
    </soap:Envelope>
    HTTP/1.1 200 OK
    Content-Type: text/xml; charset=utf-8
    Content-Length: length

    <?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>
        <RetrieveSResponse xmlns="http://192.168.0.2/">
        <RetrieveSResult>xmlxml</RetrieveSResult>
        </RetrieveSResponse>
      </soap:Body>
    </soap:Envelope>
    SOAP 1.2

    The following is a sample SOAP 1.2 request and response. The placeholders shown need to be replaced with actual values.

    POST /MimsWeb/MimsWeb.asmx HTTP/1.1
    Host: 192.168.0.2
    Content-Type: application/soap+xml; charset=utf-8
    Content-Length: length

    <?xml version="1.0" encoding="utf-8"?>
    <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
      <soap12:Body>
        <RetrieveS xmlns="http://192.168.0.2/">
          <s>string</s>
          <t>string</t>
        </RetrieveS>
      </soap12:Body>
    </soap12:Envelope>
    HTTP/1.1 200 OK
    Content-Type: application/soap+xml; charset=utf-8
    Content-Length: length

    <?xml version="1.0" encoding="utf-8"?>
    <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
      <soap12:Body>
        <RetrieveSResponse xmlns="http://192.168.0.2/">
          <RetrieveSResult>xmlxml</RetrieveSResult>
        </RetrieveSResponse>
       </soap12:Body>
    </soap12:Envelope>
    HTTP GET

    The following is a sample HTTP GET request and response. The placeholders shown need to be replaced with actual values.

    GET /MimsWeb/MimsWeb.asmx/RetrieveS?s=string&t=string HTTP/1.1
    Host: 192.168.0.2
    HTTP/1.1 200 OK
    Content-Type: text/xml; charset=utf-8
    Content-Length: length

    <?xml version="1.0" encoding="utf-8"?>
    <DataTable xmlns="http://192.168.0.2/">xmlxml</DataTable>
    HTTP POST

    The following is a sample HTTP POST request and response. The placeholders shown         need to be replaced with actual values.

    POST /MimsWeb/MimsWeb.asmx/RetrieveS HTTP/1.1
    Host: 192.168.0.2
    Content-Type: application/x-www-form-urlencoded
    Content-Length: length

    s=string&t=string
    HTTP/1.1 200 OK
    Content-Type: text/xml; charset=utf-8
    Content-Length: length

    <?xml version="1.0" encoding="utf-8"?>
    <DataTable xmlns="http://192.168.0.2/">xmlxml</DataTable>

1 个答案:

答案 0 :(得分:0)

AndroidManifest.xml中,uses-permission标记应写在application标记之外。

您缺少清单中的INTERNET权限。这就是错误的原因。

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

这是您要创建的请求。

<RetrieveS xmlns="http://192.168.0.2/">
  <s>string</s>
  <t>string</t>
</RetrieveS>

这是您创建上述请求的方式,

SoapObject  request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("s","sValue");
request.addProperty("t","tValue");
envelope.setOutputSoapObject(request);

如果您想获得回复XML

HttpTransportSE transporte = new HttpTransportSE(URL);
transporte.debug = true;
try {
    transporte.call(SOAP_ACTION,envelope);

    String resultado = transporte.responseDump;
} catch (Exception e){
    e.printStackTrace();
    Toast.makeText(getBaseContext(),e.getMessage().toString(),Toast.LENGTH_LONG).show();
}