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
| public class Client {
public Client() {
// Instanciation du POST
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"mon_url");
// Creation d'un POST multipart
MultipartEntity multipartEntity = new MultipartEntity();
try {
// Architecture d'un part
FormBodyPart identifiantPart = new FormBodyPart("identifiant", new StringBody("value1"));
identifiantPart.addField("Content-ID", "value2");
FormBodyPart sessionPart = new FormBodyPart("session", new StringBody("value3"));
sessionPart.addField("Content-ID", "value3");
// Ajout du part au POST
multipartEntity.addPart(identifiantPart);
multipartEntity.addPart(sessionPart);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
// Ajout du multiPart dans le post
httpPost.setEntity(multipartEntity);
// Envoie du Post et Reception de la reponse
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
// Affichage de la réponse
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
System.out.println("Status Line : "
+ httpResponse.getStatusLine());
System.out.println("All Headers :");
for (int i = 0; i < httpResponse.getAllHeaders().length; i++) {
System.out.println(httpResponse.getAllHeaders()[i]);
}
if (entity != null) {
System.out.println("entity utils : "
+ EntityUtils.toString(entity));
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
new Client();
}
} |
Partager