【问题标题】:How to enable webHttp connection in WCF?如何在 WCF 中启用 webHttp 连接?
【发布时间】:2009-03-24 04:59:35
【问题描述】:

我正在开发一种将数据从安卓手机传输到服务器的解决方案(用 C#/.NET 编写)。

我创建了 WCF 服务并使用模拟器进行测试,一切正常。然后当我尝试从手机登录(连接到家庭 wifi 网络)时,我收到以下异常消息:

org.apache.http.conn.HttpHostConnectException:连接到http://192.168.1.5:8000被拒绝

如果有人可以查看配置文件和界面并就如何启用连接提供任何建议,我将不胜感激。

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;
    }
}

【问题讨论】:

    标签: wcf rest webhttpbinding


    【解决方案1】:

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

    【讨论】:

    • 我在本地网络上测试过,所以我认为它应该可以访问
    • 你是如何测试它的? 192.168.1.5 不是您计算机的公共 IP 地址。如果您访问“whatismyip.com”之类的网站,您将看到您的公共 IP 地址。为了从外界攻击您的计算机,您需要使用公共 IP 地址(并​​且您可能需要与您的 ISP 联系)。
    【解决方案2】:

    如果您使用的是 vista 操作系统,则必须将地址添加到防火墙中

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

    【讨论】:

      【解决方案3】:

      使用您的 LAN ip 将您的模拟器与 Web 服务连接,例如:http://192.168.1.78:8080/webservice

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

      【讨论】:

        猜你喜欢
        • 2010-11-26
        • 2011-12-29
        • 2011-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-23
        • 1970-01-01
        • 2020-06-30
        相关资源
        最近更新 更多