简体   繁体   English

在Android中拒绝连接到本地主机

[英]Connection to localhost refused in Android

I am trying to consume WCF service in Android. 我正在尝试在Android中使用WCF服务。 I have hosted the service as 我已经将服务托管为

http://localhost:8080/UserService.svc

This is my serviceModel section in web.config web.config : 这是我在web.config web.config中的serviceModel部分:

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="SimpleBasic">
          <security mode="None"/>
        </binding>
        <binding name="BasicOverHttps">
          <security mode="Transport"/>
        </binding>
      </basicHttpBinding>
    </bindings>
        <services>
            <service behaviorConfiguration="AndroidService.Service1Behavior" name="AndroidService.Service1">
                <endpoint address="" binding="wsHttpBinding" contract="AndroidService.IService1">
                    <identity>
                        <dns value="localhost"/>
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            </service>
            <service behaviorConfiguration="AndroidService.UserServiceBehavior" name="AndroidService.UserService">
                <endpoint address="ws" binding="wsHttpBinding" contract="AndroidService.IUserService">
                    <identity>
                        <dns value="localhost"/>
                    </identity>
                </endpoint>
        <endpoint address="basic" binding="basicHttpBinding" bindingConfiguration="SimpleBasic" contract="AndroidService.IUserService"/>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <endpoint address="web" binding="webHttpBinding" contract="AndroidService.IUserService" />
      </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="AndroidService.Service1Behavior">
                    <serviceMetadata httpGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="false"/>
                </behavior>
                <behavior name="AndroidService.UserServiceBehavior">
                    <serviceMetadata httpGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="false"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
    </system.serviceModel>

which I can browse. 我可以浏览。 I also checked the output for one method GetUser of service in WebServiceStudio. 我还检查了WebServiceStudio中服务的一种方法GetUser的输出。 It came out to be fine. 出来很好。 I then created an Android client application wherein I am trying to consume it. 然后,我创建了一个Android客户端应用程序,在其中尝试使用它。

HttpClient httpClient = new DefaultHttpClient();
          try
          {
              String url = "http://localhost:8080/UserService.svc/GetUser?name=Nitish";

            HttpGet method = new HttpGet( new URI(url) );
            HttpResponse response = httpClient.execute(method);
            if ( response != null )
            {
              Log.i( "login", "received " + getResponse(response.getEntity()) );
            }
            else
            {
              Log.i( "login", "got a null response" );
            }
          } catch (IOException e) {
            Log.e( "error", e.getMessage() );
          } catch (URISyntaxException e) {
            Log.e( "error", e.getMessage() );
          }
          catch (Exception e) {
            // TODO: handle exception
              Log.e( "error", e.getMessage() );
        }  

I get exception as follows in HttpResponse response = httpClient.execute(method); 我在HttpResponse response = httpClient.execute(method);得到如下异常

Connection to http://localhost:8080 refused 

I am running the application on device and have added following premissions in mainefest: 我正在设备上运行该应用程序,并在维护中添加了以下权限:

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

I went through this solution and tried to host the service on my IP address but no success. 我经历了此解决方案,并尝试将服务托管在我的IP地址上,但没有成功。

Localhost generally works only when the service and the client sit in the same host. 通常,仅当服务和客户端位于同一主机中时,Localhost才能工作。

Use host machine name in the client request, like : 在客户端请求中使用主机名,例如:

http://MyHostMachine:8080/...

If you are not using IIS but a Windows service to host the WCF service, in the app config, make sure the base address of respective endpoints is 如果您不是使用IIS,而是使用Windows服务来承载WCF服务,请在应用程序配置中,确保各个端点的基址为

http://MyHostMachine:8080/

"localhost" referes to the local device, and not your WCF-Service-host. “本地主机”是指本地设备,而不是WCF服务主机。

On your development-PC it is 127.0.0.1, which referes to this machine. 在您的开发PC上,它是127.0.0.1,它是指此计算机。

On your android-device it is the same, and this referes to the device. 在您的android设备上,它是相同的,并且这指的是设备。

Read this concerning "Localhost" 阅读有关“ Localhost”的内容

You have to replace "localhost" with the IP-address of your Machine on which the WCF-Service is running. 您必须将“ localhost”替换为运行WCF服务的计算机的IP地址。

When you indicate localhost in an IP address that is translate into 127.0.0.1 so you can't refer to localhost into app's code because it tries to connect to itself to receive data. 当您在转换为127.0.0.1的IP地址中指示localhost时,您将无法在应用程序的代码中引用localhost,因为它试图连接到自身以接收数据。 The correct solution IMHO is to replace "localhost" with the machine's IP address in the LAN. 正确的解决方案恕我直言,是用LAN中的计算机IP地址替换“ localhost”。 If you have Windows try to run a terminal and type "ipconfig" to discover the correct IP. 如果您使用Windows,请尝试运行终端并键入“ ipconfig”以发现正确的IP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM