Apache HttpClient 작성 다중 파트 양식 게시
나는 HttpClient에 대해 매우 녹색이며 문서가 부족하거나 뻔뻔스럽게 잘못된 문서가 매우 실망 스럽습니다. Apache Http Client를 사용하여 다음 게시물 (아래에 나열 됨)을 구현하려고하지만 실제로 수행하는 방법을 모릅니다. 나는 다음 주 동안 문서에 내 자신을 묻을 것입니다.하지만 아마도 더 경험이 많은 HttpClient 코더가 더 빨리 답을 얻을 수있을 것입니다.
게시하다:
Content-Type: multipart/form-data; boundary=---------------------------1294919323195
Content-Length: 502
-----------------------------1294919323195
Content-Disposition: form-data; name="number"
5555555555
-----------------------------1294919323195
Content-Disposition: form-data; name="clip"
rickroll
-----------------------------1294919323195
Content-Disposition: form-data; name="upload_file"; filename=""
Content-Type: application/octet-stream
-----------------------------1294919323195
Content-Disposition: form-data; name="tos"
agree
-----------------------------1294919323195--
HttpMime 라이브러리의 MultipartEntityBuilder 를 사용하여 원하는 요청을 수행하십시오.
내 프로젝트에서 나는 이렇게합니다.
HttpEntity entity = MultipartEntityBuilder
.create()
.addTextBody("number", "5555555555")
.addTextBody("clip", "rickroll")
.addBinaryBody("upload_file", new File(filePath), ContentType.create("application/octet-stream"), "filename")
.addTextBody("tos", "agree")
.build();
HttpPost httpPost = new HttpPost("http://some-web-site");
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity result = response.getEntity();
이것이 도움이되기를 바랍니다.
(이 게시물은 @mtomy 코드를 예로 사용하여 더 이상 사용되지 않는 MultipartEntity 대신 MultipartEntityBuilder를 사용하도록 업데이트했습니다.)
MultipartEntity now shows up as deprecated. I am using apache httpclient 4.3.3 - does anyone know what we are supposed to use instead? I find the google searches to be so full of MultipartEntity examples I can't find anything. – vextorspace Mar 31 '14 at 20:36
Here is the sample code in HttpClient 4.3.x
import org.apache.http.entity.mime.MultipartEntityBuilder;
HttpPost httppost = new HttpPost("http://localhost:8080" +
"/servlets-examples/servlet/RequestInfoExample");
FileBody bin = new FileBody(new File(args[0]));
StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("bin", bin)
.addPart("comment", comment)
.build();
httppost.setEntity(reqEntity);
To use the class MultipartEntityBuilder, you need httpmime, which is a sub project of HttpClient
HttpClient 4.3.x:
http://hc.apache.org/httpcomponents-client-4.3.x/index.html
httpmime 4.3.x:
http://hc.apache.org/httpcomponents-client-4.3.x/httpmime/dependency-info.html
if use org.apache.commons.httpclient.HttpClient package, maybe that can help you!
HttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
//here should set HttpConnectionManagerParams but not important for you
HttpClient httpClient = new HttpClient(httpConnectionManager);
PostMethod postMethod = new PostMethod("http://localhost/media");
FilePart filePart = new FilePart("file", new File(filepath));
StringPart typePart = new StringPart("type", fileContent.getType(), "utf-8");
StringPart fileNamePart = new StringPart("fileName", fileContent.getFileName(), "utf-8");
StringPart timestampPart = new StringPart("timestamp", ""+fileContent.getTimestamp(),"utf-8");
Part[] parts = { typePart, fileNamePart, timestampPart, filePart };
MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, postMethod.getParams());
postMethod.setRequestEntity(multipartRequestEntity);
httpClient.executeMethod(postMethod);
String responseStr = postMethod.getResponseBodyAsString();
참고URL : https://stackoverflow.com/questions/2304663/apache-httpclient-making-multipart-form-post
'Program Tip' 카테고리의 다른 글
C에서 함수를 호출하기 전의 매개 변수 평가 순서 (0) | 2020.11.11 |
---|---|
Tomcat에서 모든 기본 HTTP 오류 응답 콘텐츠 비활성화 (0) | 2020.11.11 |
커밋 된 스냅 샷 및 스냅 샷 격리 수준 읽기 (0) | 2020.11.11 |
C ++ RTTI를 사용하는 것이 바람직하지 않게 만드는 이유는 무엇입니까? (0) | 2020.11.11 |
솔루션 전체에서 균일 한 버전 관리를위한 공유 AssemblyInfo (0) | 2020.11.11 |