如何在WCF中启用webHttp连接?

发布于 2024-07-15 20:02:02 字数 2405 浏览 8 评论 0原文

我正在开发一个将数据从 Android 手机传输到服务器的解决方案(用 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;
    }
}

I am developing a solution for transfering data from android phone to the server (written in C#/.NET).

I created a WCF service and testing with emulator everything worked fine. Then when I tried to login from mobile phone (connected to home wifi network) I got the following exception message:

org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.1.5:8000 refused

I would really appreciate if anyonecould give a look at the config file and the interface and give any advice on how to enable connection.

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>

Interface:

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

Service implementation:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

失退 2024-07-22 20:02:02

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

In order to connect to your service, it needs to be hosted on a public web server. It looks like the address you're using 192.168.1.5:8000 is a home network address, which is not accessible from the outside world (a phone).

梦幻的味道 2024-07-22 20:02:02

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

netsh http add urlacl url=http://+:8000 / 用户=域\用户

if you are using vista OS ,you have to add the address into your firewall

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

贱贱哒 2024-07-22 20:02:02

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

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

Use your LAN ip to connect your emulator with Web Service,,For EX: http://192.168.1.78:8080/webservice

But your Local Area Connection must be enable
Goto: Control Panel\Network and Internet\Network Connections

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文