.net - 如何使用 Android 使用 WCF 服务

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

谢谢!

最佳答案

要开始使用 WCF,对 Web 服务绑定(bind)使用默认的 SOAP 格式和 HTTP POST(而不是 GET)可能是最简单的。最简单的 HTTP 绑定(bind)是“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 header 才能使其正常工作。一旦你开始运行它,你就可以开始调查登录的安全性(可能需要使用不同的绑定(bind)来获得更好的安全性),或者可能使用 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>

https://stackoverflow.com/questions/669764/

相关文章:

android - 获取 android.content.res.Resources$NotFoun

android - 更改抽屉导航中菜单项的文本颜色

android - 如何从 Sqlite 获取最后一条记录?

android - 如何去除 ImageButton 的标准背景图片?

android - 使用 Glide 库设置完成图像加载后进度条的可见性

android - 输入时清除 EditText 中的文本

Android - fragment 事务上的自定义动画未运行

android - Android Studio v 1.1/1.2 中的渲染问题

android - ADB - Android - 获取当前 Activity 的名称

android - 月份名称作为字符串