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
| public static String postURL(String url, HashMap<String, String> params) throws XedixException {
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
String response = null;
ArrayList<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
for (String p : params.keySet())
parameters.add(new BasicNameValuePair(p, params.get(p)));
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters);
post.setEntity(entity);
// Définition du Handler pour la réponse Http
ResponseHandler<byte[]> handler = new ResponseHandler<byte[]>() {
public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
HttpEntity entity = response.getEntity();
if (entity != null)
return EntityUtils.toByteArray(entity);
else
return null;
}
};
System.out.println("REQUESTING " + post.getURI().toURL().toString() + " with parameters :");
for(String k : params.keySet())
System.out.println("\t- " + k + " : " + params.get(k));
long t = System.currentTimeMillis();
response = new String(client.execute(post, handler));
System.out.println("(Duration : " + (System.currentTimeMillis() - t) + "ms)\nRESPONSE :\n" + response);
post.abort();
return response;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
} |
Partager