Skip to content

Instantly share code, notes, and snippets.

@jyukutyo
Created July 31, 2012 07:39
Show Gist options
  • Save jyukutyo/3214543 to your computer and use it in GitHub Desktop.
Save jyukutyo/3214543 to your computer and use it in GitHub Desktop.
HttpClient 4でリクエストボディに画像のバイナリを書きこんでpostする
String url = "https://...";
URIBuilder builder = null;
try {
builder = new URIBuilder(url);
builder.addParameter("param", "テスト");
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(builder.build());
byte[] bytes = IOUtils.toByteArray(Thread.currentThread().getContextClassLoader().getResource("1.jpg"));
ByteArrayEntity entity = new ByteArrayEntity(bytes, ContentType.create("image/jpeg"));
post.setEntity(entity);
HttpResponse response = client.execute(post);
System.out.println(response.getStatusLine().getStatusCode());
String responseBody = IOUtils.toString(response.getEntity().getContent());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment