2011-11-10 14 views
1

ログイン用にREST API(https://localhost/server/api/login)があります。これには、以下に示すxml形式のパラメータを受け入れることができます。xml本体でREST APIを呼び出す方法

<?xml version="1.0" encoding="UTF-8" ?> 
<Request xmlns="http://www.xxxx.com/center/cbm/1.0.0"> 
    <Users> 
    <User> 
     <Type>userType</Type> 
     <Name>admin</Name> 
     <Password>password</Password> 
     <Captcha>On</Captcha> 
    </User> 
    </Users> 
</Request> 

どのようにすれば、Javaで呼び出して応答を受け取ることができますか。

+3

だけで、次のHttpClientをを使用するか、またはURLConnectionの –

+0

データをポストに役立つことがあります。http://blog.bdoughan.com/2010/08/creating- restful-web-service-part-55.html –

答えて

2

あなたはApache HTTP Client 4.xを使用してHTTP POSTリクエストを行うことができます。

String xmlString = "... your data ..." 

HttpPost httpRequest = new HttpPost("https://localhost/server/api/login"); 
httpRequest.setEntity(new StringEntity(xmlString)); 

HttpClient httpclient = new DefaultHttpClient(); 
HttpResponse httpResponse = httpclient.execute(httpRequest, new BasicHttpContext()); 

if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK && httpResponse.getEntity() != null) { 
    //handle response ok 
} else { 
    //handle error 
} 
関連する問題