curl을 사용할 때 -F를 사용해 boundary를 사용한 예제이다.
curl -XPOST "http://up.google.com/up/" -F "key1=value1" -F "key2=value2" -v
* Trying ...
* Connected to up.google.com (1.1.1.1) port 80 (#0)
> POST /up/ HTTP/1.1
> Host: up.google.com
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Length: 465
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------fa94669c2a0ffcbe
>
< HTTP/1.1 100 Continue
< HTTP/1.1 200 OK
< Server: openresty
< Date: Fri, 09 Jun 2017 12:25:23 GMT
< Content-Type: application/json
< Transfer-Encoding: chunked
< Connection: keep-alive
<
* Connection #0 to host up.google left intact
이를 apache http를 사용해서 boundary 를 적용한 예제이다.
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
@Test
public void test() throws Exception {
HttpPost httpPost = new HttpPost("http://up.google.com/up/");
String BOUNDARY = "----------fa94669c2a0ffcbe";
httpPost.setHeader("Content-Type", "multipart/form-data;boundary=" + BOUNDARY);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.setBoundary(BOUNDARY);
builder.addPart("text_1", new StringBody("304"));
builder.addPart("text_2", new StringBody(toHex("사랑합니다💕 ")));
httpPost.setEntity(builder.build());
CloseableHttpClientFactory httpClientFactory = new CloseableHttpClientFactory();
CloseableHttpClient httpClient = httpClientFactory.getObject();
CloseableHttpResponse response = httpClient.execute(httpPost);
System.out.println("response : " + response);
if (response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 201) {
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
System.out.println("result : " + responseString);
} else {
System.out.println("error");
}
}
public String toHex(String arg) {
return String.format("%040x", new BigInteger(1, arg.getBytes()));
}
로그는 다음과 같이 나타난다.
21:49:43.806 [main] DEBUG o.a.h.c.protocol.RequestAddCookies - CookieSpec selected: default
21:49:43.814 [main] DEBUG o.a.h.c.protocol.RequestAuthCache - Auth cache not set in the context
21:49:43.816 [main] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection request: [route: {}->http://up.google.com:80][total kept alive: 0; route allocated: 0 of 5; total allocated: 0 of 30]
21:49:43.824 [main] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {}->http://up.google.com:80][total kept alive: 0; route allocated: 1 of 5; total allocated: 1 of 30]
21:49:43.825 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Opening connection {}->http://up.google.com:80
21:49:44.234 [main] DEBUG o.a.h.i.c.DefaultHttpClientConnectionOperator - Connecting to up.google.com/1.1.1.1:80
21:49:44.247 [main] DEBUG o.a.h.i.c.DefaultHttpClientConnectionOperator - Connection established 1.1.1.2:59752<->1.1.1.1:80
21:49:44.248 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Executing request POST /up/ HTTP/1.1
21:49:44.248 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Target auth state: UNCHALLENGED
21:49:44.249 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Proxy auth state: UNCHALLENGED
21:49:44.250 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> POST /up/ HTTP/1.1
21:49:44.250 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Content-Type: multipart/form-data;boundary=----------HV2ymHFg03ehbqgZCaKO6jyH
21:49:44.250 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Content-Length: 447
21:49:44.250 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Host: up.google.com
21:49:44.250 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Connection: Keep-Alive
21:49:44.250 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.4.1 (Java/1.8.0_101)
21:49:44.251 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "POST /up/ HTTP/1.1[\r][\n]"
21:49:44.251 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Type: multipart/form-data;boundary=----------HV2ymHFg03ehbqgZCaKO6jyH[\r][\n]"
21:49:44.251 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Length: 447[\r][\n]"
21:49:44.251 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Host: up.google.com[\r][\n]"
21:49:44.251 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
21:49:44.251 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.4.1 (Java/1.8.0_101)[\r][\n]"
21:49:44.251 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
21:49:44.251 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "------------HV2ymHFg03ehbqgZCaKO6jyH[\r][\n]"
21:49:44.251 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Disposition: form-data; name="text_1"[\r][\n]"
21:49:44.251 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
21:49:44.251 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "304"
21:49:44.251 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
21:49:44.251 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "------------HV2ymHFg03ehbqgZCaKO6jyH[\r][\n]"
21:49:44.252 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Disposition: form-data; name="text_2"[\r][\n]"
21:49:44.252 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
21:49:44.252 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "ec82aceb9e91ec9db420eb8498ecb998eb8a9420ed9598eba3a8ec9e85eb8b88eb8ba42ef09f929520ebb09beb8a9420eab283eb8f8420eca28beca780eba78c20ed919ced9884ed9598eb8a9420eab283eb8f8420eca491ec9a94ed959c20eab1b020ec9584ec8b9ceca3a03ff09f988a"
21:49:44.252 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
21:49:44.252 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "------------HV2ymHFg03ehbqgZCaKO6jyH--[\r][\n]"
21:49:45.401 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "HTTP/1.1 200 OK[\r][\n]"
21:49:45.401 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Server: openresty[\r][\n]"
21:49:45.401 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Date: Fri, 09 Jun 2017 12:49:45 GMT[\r][\n]"
21:49:45.401 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Content-Type: application/json[\r][\n]"
21:49:45.401 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Transfer-Encoding: chunked[\r][\n]"
21:49:45.401 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Connection: keep-alive[\r][\n]"
21:49:45.401 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "[\r][\n]"
21:49:45.401 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "338[\r][\n]"
21:49:45.401 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "{.......}}[\r][\n]"
21:49:45.401 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "0[\r][\n]"
21:49:45.401 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "[\r][\n]"
21:49:45.404 [main] DEBUG org.apache.http.headers - http-outgoing-0 << HTTP/1.1 200 OK
21:49:45.405 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Server: openresty
21:49:45.405 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Date: Fri, 09 Jun 2017 12:49:45 GMT
21:49:45.405 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Type: application/json
21:49:45.405 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Transfer-Encoding: chunked
21:49:45.405 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Connection: keep-alive
21:49:45.410 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Connection can be kept alive indefinitely
'general java' 카테고리의 다른 글
apache storm 1.0.1 설치 및 테스트 (0) | 2017.08.22 |
---|---|
[apache common collection] UnmodifiableMap.decorate 예제 (0) | 2017.06.16 |
이모티콘 저장시 mysql 관련 히스토리 (0) | 2017.06.05 |
mysql driver 버전 5에서 버전 6로 변경시 이슈 (0) | 2017.04.11 |
[spring] ClassPathBeanDefinitionScanner 사용과 관련된 spring3에서 spring4의 큰 변화 (0) | 2017.03.27 |