【问题标题】:Connection to localhost refused in Android在 Android 中连接到本地主机被拒绝
【发布时间】:2014-06-05 13:19:40
【问题描述】:

我正在尝试在 Android 中使用 WCF 服务。我已将服务托管为

http://localhost:8080/UserService.svc

这是我在 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>

我可以浏览。我还检查了 WebServiceStudio 中服务的一种方法 GetUser 的输出。结果很好。然后我创建了一个 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() );
        }  

我在HttpResponse response = httpClient.execute(method); 中得到如下异常

Connection to http://localhost:8080 refused 

我正在设备上运行应用程序,并在 mainefest 中添加了以下权限:

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

我通过this solution 尝试在我的IP 地址上托管该服务,但没有成功。

【问题讨论】:

  • 您是否在 android 设备上本地托管 WCF 服务?
  • 我在本地托管。
  • Localhost 在 android 上不起作用,试试 10.0.0.1 之类的。
  • 本地?本地在您的 android 设备上还是本地在您的开发 PC 上或本地在您的家庭服务器上?
  • 本地在我的开发 PC 上。

标签: android web-services wcf


【解决方案1】:

Localhost 通常仅在服务和客户端位于同一主机时才起作用。

在客户端请求中使用主机名,如:

http://MyHostMachine:8080/...

如果您不是使用 IIS 而是使用 Windows 服务来托管 WCF 服务,请在应用配置中确保各个端点的基地址为

http://MyHostMachine:8080/

【讨论】:

    【解决方案2】:

    “localhost”指的是本地设备,而不是您的 WCF-Service-host。

    在你的开发PC上是127.0.0.1,指的是这台机器。

    在你的 android-device 上是一样的,这里指的是设备。

    阅读this concerning "Localhost"

    您必须将“localhost”替换为运行 WCF 服务的计算机的 IP 地址。

    【讨论】:

      【解决方案3】:

      当您在转换为 127.0.0.1 的 IP 地址中指示 localhost 时,您不能将 localhost 引用到应用程序的代码中,因为它会尝试连接到自身以接收数据。 恕我直言,正确的解决方案是将“localhost”替换为局域网中机器的 IP 地址。如果您有 Windows,请尝试运行终端并键入“ipconfig”以发现正确的 IP。

      【讨论】:

        猜你喜欢
        • 2013-11-29
        • 1970-01-01
        • 1970-01-01
        • 2015-07-30
        • 2020-03-14
        • 2017-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多