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

标签 android web-services wcf

我正在尝试在 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 地址上托管该服务,但没有成功。

最佳答案

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

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

http://MyHostMachine:8080/...

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

http://MyHostMachine:8080/

关于android - 在 Android 中拒绝连接到本地主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24061356/

相关文章:

android - 在 Quickblox 中发送图像附件

android - Android 中水平寻呼机的两根手指滑动

.net - 从 XSD 生成类

java - Web 服务和 IIS 传递/重定向

WCF GZip 压缩请求/响应处理

android - Android Facebook 登录中的 key 哈希不匹配

android - 从 Android 服务中打开数据库连接时出错

c# - 如何让我的客户端应用程序将来 self 的网络服务的对象识别为它已知的类型?

c# - WCF 或自定义套接字体系结构

wcf - 在启用 REST 的 WCF 服务中映射复杂类型