如何使用WCF服务与Android [英] How to Consume WCF Service with Android
问题描述
我在创建.NET服务器和Android的客户端应用程序。我想实现它发送用户名和密码,服务器和服务器发送回一个会话字符串的身份验证方法。
I am creating a server in .NET and a client application for Android. I would like to implement an authentication method which sends username and password to server and a server sends back a session string.
我不熟悉与WCF所以我真的AP preciate你的帮助。
I'm not familiar with WCF so I would really appreciate your help.
在java中,我写了下面的方法:
In java I've written the following method:
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;
}
但在服务器端,到目前为止我还没有想出什么。
But on the server side so far I haven't figured out anything.
我会很感激,如果有人可以解释如何才能读取客户端这两个参数创建一个适当的方法字符串登录(用户名字符串,字符串密码)用适当的App.config中的设置和接口,具有适当的[OperationContract的]签名并与会话串答复。
I would be really thankful if anyone could explain how to create an appropriate method string Login(string username, string password) with appropriate App.config settings and Interface with appropriate [OperationContract] signature in order to read these two parameters from client and reply with session string.
谢谢!
推荐答案
要开始使用WCF,这可能是最简单的只使用默认的SOAP格式和HTTP POST(而不是GET)为Web服务绑定。最简单的HTTP绑定拿到工作是basicHttpBinding的。这里的的ServiceContract / OperationContract的可能看起来像你的登录服务的例子:
To get started with WCF, it might be easiest to just use the default SOAP format and HTTP POST (rather than GET) for the web-service bindings. The easiest HTTP binding to get working is "basicHttpBinding". Here is an example of what the ServiceContract/OperationContract might look like for your login service:
[ServiceContract(Namespace="http://mycompany.com/LoginService")]
public interface ILoginService
{
[OperationContract]
string Login(string username, string password);
}
该服务的实现可能是这样的:
The implementation of the service could look like this:
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举办这个作为一个窗口服务,也可以承载它在IIS中像一个正常的ASP.NET Web(服务)的应用程序。有很多的教程在那里为这两种。
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. There are a lot of tutorials out there for both of these.
WCF服务配置可能是这样的:
The WCF service config might look like this:
<?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测试,并公开服务元数据)。
(The MEX stuff is optional for production, but is needed for testing with WcfTestClient.exe, and for exposing the service meta-data).
您将不得不修改Java code来发布SOAP消息发送到服务。 WCF可以当与非WCF客户端互操作的一个小挑剔,所以你必须要乱用POST头一点点地得到它的工作。一旦你得到这个运行,你就可以开始使用WCF休息,以便登录了GET而不是SOAP / POST调查安全登录(可能需要使用不同的结合来获得更好的安全性),或者可能。
You'll have to modify your Java code to POST a SOAP message to the service. WCF can be a little picky when inter-operating with non-WCF clients, so you'll have to mess with the POST headers a little to get it to work. Once you get this running, you can then start to investigate security for the login (might need to use a different binding to get better security), or possibly using WCF REST to allow for logins with a GET rather than SOAP/POST.
下面是对HTTP POST应该是什么样子从Java code的例子。有一个名为工具小提琴手,可用于调试网络服务非常有用的。
Here is an example of what the HTTP POST should look like from the Java code. There is a tool called "Fiddler" that can be really useful for debugging 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服务与Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!