authentfication via u server webpshere commerce
bonsoir , j ai besoin de votre aide sur ce truc d'authentification avec CredentialsProvider , j ai rechercher partout sur le net je pense bien coder mais rien ne passe toujours les information ne sont pas envoyées , pouvez vous m aider a voir pourquoi les informations ne sont pas envoyées car je galère avec truc .
voici mon web service en gras la partie authentification
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
public class WebService {
private String response;
private String url;
String username="19851985";
String password="19851985";
private ClientConnectionManager clienConnectionManager ;
private HttpContext context;
private HttpParams params;
public String getResponse() {
return response;
}
public WebService(String url) {
this.url = url;
}
/**
* cette methode permet de traiter notre demande
*
* @throws KeyStoreException
* @throws IOException
* @throws CertificateException
* @throws NoSuchAlgorithmException
**/
public void executeRequest() throws IOException {
/** creer uun client http **/
SchemeRegistry registre= new SchemeRegistry();
registre.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 8002));
registre.register(new Scheme("https", new EasySSLSocketFactory(), 443));
params = new BasicHttpParams();
params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS,1);
params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(1));
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
HttpProtocolParams.setVersion(params,HttpVersion.HTTP_1_1 );
HttpProtocolParams.setContentCharset(params,"utf8");
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials( new AuthScope ("https://localhost",443),
new UsernamePasswordCredentials(username, password));
clienConnectionManager = new ThreadSafeClientConnManager(params,registre);
HttpClient client = new DefaultHttpClient(clienConnectionManager, params);
context = new BasicHttpContext();
context.setAttribute("http.auth.credentials-provider", credentialsProvider);
// HttpClient client = new MyHttpClient( context);
/**
* declarer variable reponse de type HttpResponse pour traiter les
* demande de nos requete
**/
HttpResponse httpResponse;
/** recuperer url par httpGet **/
HttpGet get = new HttpGet(url);
/** ajouter notre header **/
get.addHeader("User-Agent", "Android");
try {
/** Exécute notre requête qui va **/
httpResponse = client.execute(get,context);
HttpEntity entity = httpResponse.getEntity();
/**
* on teste si la variable entity est non null si oui on recuperer
* le flux d entrée via getContent() qui retour InputStream qui va
* etre lu par la methode stream2String()
**/
if (entity != null) {
/** **/
InputStream instream = entity.getContent();
response = stream2String(instream);
// Closing the input stream will trigger connection release
instream.close();
}
} catch (ClientProtocolException e) {
client.getConnectionManager().shutdown();
e.printStackTrace();
} catch (IOException e) {
client.getConnectionManager().shutdown();
e.printStackTrace();
}
}
/**
* cette methode prend en parametre le flux d entée pour la lecture de des
* donnés
*
* @throws UnsupportedEncodingException
**/
private String stream2String(InputStream stream)
throws UnsupportedEncodingException {
String charSet = "iso-8859-1";
InputStreamReader reader = new InputStreamReader(stream, charSet);
BufferedReader buffer = new BufferedReader(reader);
StringBuilder sb = new StringBuilder();
try {
String cur;
while ((cur = buffer.readLine()) != null) {
sb.append(cur);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
} |
merci d avance