【问题标题】:How to Consume WCF Service with Android如何使用 Android 使用 WCF 服务
【发布时间】:2010-10-14 18:16:44
【问题描述】:

我正在.NET 中创建服务器和Android 客户端应用程序。我想实现一种身份验证方法,将用户名和密码发送到服务器,服务器发回会话字符串。

我不熟悉 WCF,因此非常感谢您的帮助。

在java中我写了以下方法:

private void Login()
{
  HttpClient httpClient = new DefaultHttpClient();
  try
  {
      String url = "http://192.168.1.5:8000/Login?username=test&password=test";

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

private String getResponse( HttpEntity entity )
{
  String response = "";

  try
  {
    int length = ( int ) entity.getContentLength();
    StringBuffer sb = new StringBuffer( length );
    InputStreamReader isr = new InputStreamReader( entity.getContent(), "UTF-8" );
    char buff[] = new char[length];
    int cnt;
    while ( ( cnt = isr.read( buff, 0, length - 1 ) ) > 0 )
    {
      sb.append( buff, 0, cnt );
    }

      response = sb.toString();
      isr.close();
  } catch ( IOException ioe ) {
    ioe.printStackTrace();
  }

  return response;
}

但在服务器端到目前为止我还没有弄清楚任何事情。

如果有人能解释如何使用适当的 App.config 设置和具有适当 [OperationContract] 签名的接口创建适当的方法字符串登录(字符串用户名,字符串密码),以便从客户端读取这两个参数,我将非常感激并回复会话字符串。

谢谢!

【问题讨论】:

  • 我很想看到一种使用在 android 上序列化的 wcf 二进制文件的方法。现在那会很酷。

标签: .net android wcf rest


【解决方案1】:

要开始使用 WCF,对 Web 服务绑定使用默认的 SOAP 格式和 HTTP POST(而不是 GET)可能是最简单的。最简单的 HTTP 绑定是“basicHttpBinding”。以下是登录服务的 ServiceContract/OperationContract 的示例:

[ServiceContract(Namespace="http://mycompany.com/LoginService")]
public interface ILoginService
{
    [OperationContract]
    string Login(string username, string password);
}

服务的实现可能如下所示:

public class LoginService : ILoginService
{
    public string Login(string username, string password)
    {
        // Do something with username, password to get/create sessionId
        // string sessionId = "12345678";
        string sessionId = OperationContext.Current.SessionId;

        return sessionId;
    }
}

您可以使用 ServiceHost 将其托管为 Windows 服务,也可以像普通的 ASP.NET Web(服务)应用程序一样将其托管在 IIS 中。两者都有很多教程。

WCF 服务配置可能如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>


    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="LoginServiceBehavior">
                    <serviceMetadata />
                </behavior>
            </serviceBehaviors>
        </behaviors>

        <services>
            <service name="WcfTest.LoginService"
                     behaviorConfiguration="LoginServiceBehavior" >
                <host>
                    <baseAddresses>
                        <add baseAddress="http://somesite.com:55555/LoginService/" />
                    </baseAddresses>
                </host>
                <endpoint name="LoginService"
                          address=""
                          binding="basicHttpBinding"
                          contract="WcfTest.ILoginService" />

                <endpoint name="LoginServiceMex"
                          address="mex"
                          binding="mexHttpBinding"
                          contract="IMetadataExchange" />
            </service>
        </services>
    </system.serviceModel>
</configuration>

(MEX 的内容对于生产来说是可选的,但在使用 WcfTestClient.exe 进行测试和公开服务元数据时需要)。

您必须修改 Java 代码以将 SOAP 消息发布到服务。 WCF 在与非 WCF 客户端进行互操作时可能会有点挑剔,因此您必须稍微弄乱 POST 标头才能使其正常工作。一旦你开始运行它,你就可以开始调查登录的安全性(可能需要使用不同的绑定来获得更好的安全性),或者可能使用 WCF REST 来允许使用 GET 而不是 SOAP/POST 登录。

以下是 Java 代码中 HTTP POST 的示例。有一个名为“Fiddler”的工具对于调试 Web 服务非常有用。

POST /LoginService HTTP/1.1
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://mycompany.com/LoginService/ILoginService/Login"
Host: somesite.com:55555
Content-Length: 216
Expect: 100-continue
Connection: Keep-Alive

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<Login xmlns="http://mycompany.com/LoginService">
<username>Blah</username>
<password>Blah2</password>
</Login>
</s:Body>
</s:Envelope>

【讨论】:

  • 关于如何获得双工 wcf 通信的任何想法?轮询或真正的推送无关紧要。
  • 我会推荐 REST 选项,SOAP 给您带来的问题多于优势。如果您使用带有 SSL 加密的 REST,您的 Web 服务将非常安全。
  • 是的,我现在也推荐 REST,这个答案来自几年前,当时 REST/JSON 还没有现在流行。
【解决方案2】:

另一种选择可能是完全避免使用 WCF,而只使用 .NET HttpHandler。 HttpHandler 可以从您的 GET 中获取查询字符串变量,然后将响应写回 Java 代码。

【讨论】:

  • 你可以这样做,但是如果没有适当的框架,感觉这将是脆弱的并且难以维护。您将如何向客户端记录 REST 接口?如果你想要 JSON 怎么办?等等等等。
【解决方案3】:

除非您的 WCF 服务具有 REST 接口,否则您将需要比 http 请求更多的东西来与 WCF 服务交互。要么寻找在 android 上运行的 SOAP Web 服务 API,要么让你的服务 RESTful。您将需要 .NET 3.5 SP1 来执行 WCF REST 服务:

http://msdn.microsoft.com/en-us/netframework/dd547388.aspx

【讨论】:

    【解决方案4】:

    根据我最近的经验,我会推荐 ksoap 库来使用 Soap WCF 服务,它实际上非常简单,这个 anddev thread 也可以帮助你。

    【讨论】:

    • ksoap 使用 XML,建议使用 REST 进行此类操作。
    【解决方案5】:

    如果我这样做,我可能会在服务器上使用 WCF REST,在 Java/Android 客户端上使用 REST library

    【讨论】:

      猜你喜欢
      • 2012-09-26
      • 2015-05-03
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-04
      相关资源
      最近更新 更多