Android에서 WCF 서비스를 사용하는 방법
.NET에서 서버와 Android 용 클라이언트 응용 프로그램을 만들고 있습니다. 사용자 이름과 암호를 서버에 보내고 서버가 세션 문자열을 다시 보내는 인증 방법을 구현하고 싶습니다.
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 설정을 사용하여 적절한 메서드 문자열 Login (string username, string password)을 만들고 적절한 [OperationContract] 서명을 사용하여 인터페이스를 만드는 방법을 설명 할 수 있다면 정말 감사 할 것입니다. 세션 문자열.
감사!
WCF를 시작하려면 웹 서비스 바인딩에 기본 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;
}
}
ServiceHost를 사용하여 Windows 서비스로 호스팅하거나 일반적인 ASP.NET 웹 (서비스) 응용 프로그램처럼 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로 테스트하고 서비스 메타 데이터를 노출하는 데 필요합니다.)
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.
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>
Another option might be to avoid WCF all-together and just use a .NET HttpHandler. The HttpHandler can grab the query-string variables from your GET and just write back a response to the Java code.
You will need something more that a http request to interact with a WCF service UNLESS your WCF service has a REST interface. Either look for a SOAP web service API that runs on android or make your service RESTful. You will need .NET 3.5 SP1 to do WCF REST services:
http://msdn.microsoft.com/en-us/netframework/dd547388.aspx
From my recent experience i would recommend ksoap library to consume a Soap WCF Service, its actually really easy, this anddev thread migh help you out too.
If I were doing this I would probably use WCF REST on the server and a REST library on the Java/Android client.
참고URL : https://stackoverflow.com/questions/669764/how-to-consume-wcf-service-with-android
'program story' 카테고리의 다른 글
Scala 특성과 Java 8 인터페이스의 차이점과 유사점은 무엇입니까? (0) | 2020.10.15 |
---|---|
Ruby Gemspec 종속성 : git 분기 종속성이 가능합니까? (0) | 2020.10.15 |
Java에 조건부 및 조건부 또는 연산자의 복합 할당 버전이없는 이유는 무엇입니까? (0) | 2020.10.15 |
Node.js 및 AngularJS 애플리케이션 구조화 (0) | 2020.10.15 |
클래스 경로 리소스 (XML 파일)에서 입력 스트림 가져 오기 (0) | 2020.10.14 |