WS-Security Webservice avec HttpClient
Bonjour,
Je souhaiterai consommer des webservice https securisé (wsse) avec HttpClient (Je n'ai que cet API de disponible pour ce projet).
Voici le code d'appel :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
HttpClient httpClient = getNewHttpClient();
final String url = "https://xxxxxx/MobileService.svc";
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("SOAPAction", "GetCurrentUser");
httpPost.setHeader("Content-Type", "text/xml; charset=utf-8");
try
{
String sXML = "<?xml version='1.0' encoding='UTF-8'?>" +
"<S:Envelope xmlns:S=\"http://www.w3.org/2003/05/soap-envelope\" " +
"xmlns:wsse11=\"http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd\" " +
"xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" " +
"xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" " +
"xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">" +
"<S:Header>" +
"<To xmlns=\"http://www.w3.org/2005/08/addressing\">http://xxxxxxxxx/MobileService.svc/mobile</To>" +
"<Action xmlns=\"http://www.w3.org/2005/08/addressing\" S:mustUnderstand=\"true\">http://tempuri.org/IMobileService/GetCurrentUser</Action>" +
"<ReplyTo xmlns=\"http://www.w3.org/2005/08/addressing\"><Address>http://www.w3.org/2005/08/addressing/anonymous</Address></ReplyTo>" +
"<FaultTo xmlns=\"http://www.w3.org/2005/08/addressing\"><Address>http://www.w3.org/2005/08/addressing/anonymous</Address></FaultTo>" +
"<MessageID xmlns=\"http://www.w3.org/2005/08/addressing\">uuid:0fddc9fd-763e-4fd3-9a65-7ea215ef8bb0</MessageID>" +
"<wsse:Security S:mustUnderstand=\"true\">" +
"<wsse:UsernameToken xmlns:ns15=\"http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512\" " +
"xmlns:ns14=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
"wsu:Id=\"uuid_6ec3adc3-6c78-4dcf-8f98-c7c061348570\">" +
"<wsse:Username>MonUser</wsse:Username><wsse:Password " +
"Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">MonPassword</wsse:Password>" +
"</wsse:UsernameToken></wsse:Security>" +
"</S:Header>" +
"<S:Body>" +
"<ns3:GetCurrentUser xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\" " +
"xmlns:ns2=\"http://schemas.datacontract.org/2004/07/Thot.InnerData\" " +
"xmlns:ns3=\"http://tempuri.org/\" " +
"xmlns:ns4=\"http://schemas.microsoft.com/2003/10/Serialization/\" " +
"xmlns:ns5=\"http://schemas.datacontract.org/2004/07/Pragma.Technical.Business\" " +
"xmlns:ns6=\"http://schemas.datacontract.org/2004/07/Thot.Entities\"/>" +
"</S:Body>" +
"</S:Envelope>";
StringEntity se = new StringEntity(sXML);
se.setContentType("text/xml");
httpPost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");
httpPost.setEntity(se);
System.out.println("Result : " + new String(post(httpClient, url, sXML)));
}
catch (ClientProtocolException e) {e.printStackTrace();}
catch (IOException e) {e.printStackTrace();}
catch (Exception e) {e.printStackTrace();} |
Mais je ne reçois aucun message en retour de mon webservice.
Le message xml est celui généré par Metro dans un autre projet pour les même webservice et cela fonctionne très bien.
Pour info voila le code de mes methodes utilisées :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
public static HttpClient getNewHttpClient()
{
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
return new DefaultHttpClient(ccm, params);
} catch (Exception e) {
return new DefaultHttpClient();
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
public byte[] post(HttpClient httpClient, String url, String body)
throws Exception
{
byte data[] = null;
HttpPost post = new HttpPost(url);
HttpEntity entity = new StringEntity(body);
post.setEntity(entity);
HttpResponse respones = httpClient.execute(post);
data = EntityUtils.toByteArray(respones.getEntity());
return data;
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
private String convertStreamToString(InputStream is)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is),10000);
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null)
{
sb.append((line + "\n"));
}
}
catch (IOException e) {e.printStackTrace();}
finally
{
try
{
is.close();
}
catch (IOException e) {e.printStackTrace();}
}
return sb.toString();
} |
Je ne comprends pas pourquoi cela ne fonctionne pas.
quelqu'un aurait il une idée svp ?
Merci d'avance