如何使用Android的WCF服务

我在.NET中创build一个服务器,为Android创build一个客户端应用程序。 我想实现一个身份validation方法,发送用户名和密码到服务器和服务器发回会话string。

我不熟悉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; } 

但在服务器端到目前为止,我还没有弄清楚什么。

我真的很感激,如果有人可以解释如何创build一个适当的方法stringlogin(string用户名,string密码)与适当的App.config设置和接口与适当的[OperationContract]签名,以便从客户端读取这两个参数和答复会话string。

谢谢!

要开始使用WCF,对Web服务绑定使用默认的SOAP格式和HTTP POST(而不是GET)可能是最简单的。 最简单的HTTP绑定工作是“basicHttpBinding”。 以下是您的login服务的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服务托pipe,或者您可以像在正常的ASP.NET Web(服务)应用程序中一样将其托pipe在IIS中。 这里有很多的教程。

WCF服务configuration可能如下所示:

 <?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.exetesting,并暴露服务的元数据)。

您将不得不修改您的Java代码以将SOAP消息发布到服务。 当与非WCF客户端进行交互操作时,WCF可能会有些挑剔,所以您必须稍微弄一下POST头才能使其工作。 一旦运行完毕,就可以开始调查login的安全性(可能需要使用其他绑定来获得更好的安全性),或者可能使用WCF REST来允许使用GET而不是SOAP / POST进行login。

这里是一个HTTP POST应该看起来像Java代码的例子。 有一个名为“ Fiddler ”的工具,对debuggingWeb服务非常有用。

 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> 

另一种select可能是避免使用WCF,只使用.NET HttpHandler。 HttpHandler可以从你的GET中获取查询stringvariables,然后回写一个对Java代码的响应。

除了你的WCF服务有一个REST接口之外,你还需要一些更多的http请求来与WCF服务交互。 查找在android上运行的SOAP Web服务API或使您的服务成为RESTful。 您将需要.NET 3.5 SP1来执行WCF REST服务:

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

从我最近的经验,我会build议ksoap库消耗肥皂WCF服务,其实很简单,这anddev 线程也帮助你。

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