首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Android中使用WCF服务

如何在Android中使用WCF服务
EN

Stack Overflow用户
提问于 2009-03-21 18:48:27
回答 5查看 103K关注 0票数 79

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

我不熟悉WCF,所以我会非常感谢你的帮助。

在java中,我编写了以下方法:

代码语言:javascript
复制
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签名,以便从客户端读取这两个参数和回复会话字符串,我将非常感谢。

谢谢!

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2009-03-24 02:05:31

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

代码语言:javascript
复制
[ServiceContract(Namespace="http://mycompany.com/LoginService")]
public interface ILoginService
{
    [OperationContract]
    string Login(string username, string password);
}

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

代码语言:javascript
复制
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服务配置可能如下所示:

代码语言:javascript
复制
<?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服务非常有用。

代码语言:javascript
复制
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>
票数 41
EN

Stack Overflow用户

发布于 2009-03-24 02:10:19

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

票数 7
EN

Stack Overflow用户

发布于 2009-03-23 00:01:32

除非您的WCF服务具有REST接口,否则您将需要比http请求更多的东西来与WCF服务交互。或者寻找运行在android上的SOAP web服务API,或者使您的服务成为RESTful。您将需要WCF3.5 SP1来执行.NET REST服务:

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

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/669764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档