如何在WCF中启用webHttp连接?

3
我正在开发一个解决方案,用于将数据从Android手机传输到服务器(使用C#/.NET编写)。我创建了一个WCF服务,并在模拟器上进行测试,一切都正常。然后,当我尝试从连接到家庭WiFi网络的移动电话登录时,出现了以下异常消息:org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.1.5:8000 refused。如果有人能查看配置文件和接口并给出任何有关如何启用连接的建议,我将不胜感激。

web.config:

<configuration>
  <system.web>
    <compilation debug="true"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="DefaultBinding"
                 allowCookies="true"
                 bypassProxyOnLocal="true" />
      </webHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="RESTFriendly">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service name="RESTServer.LoginService">
        <endpoint address=""
                  behaviorConfiguration="RESTFriendly"
                  binding="webHttpBinding"
                  bindingConfiguration="DefaultBinding"
                  contract="RESTServer.ILoginService" />
      </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  </system.serviceModel>
</configuration>

接口:

[ServiceContract()]
public interface ILoginService
{
    [WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/username={username}&password={password}")]
    [OperationContract]
    Message Login(string username, string password);
}

服务实现:

public class LoginService : ILoginService
{
    public Message Login(string username, string password)
    {
        Message message = new Message();
        SQLWorks sqlWorks = new SQLWorks();
        string strSession = sqlWorks.Login(username, password);
        string strMessage;
        message.Session = strSession;
        if(strSession == "")
        {
            strMessage = "Login failed! Please check your username/password!";
        }
        else
        {
            strMessage = "Login Successful";
        }
        message.ErrorMessage = strMessage;
        return message;
    }
}
3个回答

0
为了连接到您的服务,它需要托管在公共 Web 服务器上。看起来您正在使用的地址 192.168.1.5:8000 是家庭网络地址,无法从外部世界(手机)访问。

我在本地网络上进行了测试,因此我相信它应该是可访问的。 - Niko Gamulin
你是如何测试的?192.168.1.5不是你计算机的公共IP地址。如果你访问像“whatismyip.com”这样的网站,你会看到你的公共IP地址。为了从外部访问你的计算机,你需要使用公共IP地址(你可能需要与你的ISP交谈)。 - Andy White

0

如果您使用的是Vista操作系统,您需要将地址添加到防火墙中

netsh http add urlacl url=http://+:8000/ user=DOMAIN\user


0

使用您的局域网IP地址连接模拟器和Web服务,例如:http://192.168.1.78:8080/webservice

但是您的本地连接必须启用 前往:控制面板\网络和Internet\网络连接


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接