Created
July 31, 2012 07:39
-
-
Save jyukutyo/3214543 to your computer and use it in GitHub Desktop.
HttpClient 4でリクエストボディに画像のバイナリを書きこんでpostする
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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