繁体   English   中英

如何使用 Android 消费 WCF 服务

[英]How to Consume WCF Service with Android

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

我对 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] 签名的接口创建适当的方法字符串登录(字符串用户名,字符串密码),以便从客户端读取这两个参数并回复,我将非常感激session 字符串。

谢谢!

要开始使用 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;
    }
}

You can host this as a windows service using a ServiceHost, or you can host it in IIS like a normal ASP.NET web (service) application. 这两个都有很多教程。

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-services 非常有用。

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,而只使用 .NET HttpHandler。 HttpHandler 可以从您的 GET 中获取查询字符串变量,然后将响应写回 Java 代码。

除非您的 WCF 服务具有 Z5043760F447F650EE3 接口,否则您将需要 http 请求与 WCF 服务交互。 要么寻找在 ZC31B32364CE19CA8ZFCD150A417ECCE5 上运行的 SOAP web 服务 API 或使您的服务。 您将需要 .NET 3.5 SP1 来执行 WCF REST 服务:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM