Apache HttpClient making multipart form post
Jestem całkiem zielony dla HttpClient i uważam, że brak (i lub rażąco niepoprawny) dokumentacji jest bardzo frustrujący. Próbuję zaimplementować poniższy post (wymieniony poniżej) z Klientem Http Apache, ale nie mam pojęcia, jak to zrobić. Mam zamiar zakopać się w dokumentacji na następny tydzień, ale być może bardziej doświadczeni koderzy HttpClient mogliby uzyskać mi odpowiedź wcześniej.
Post:
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--
3 answers
Użyj MultipartEntityBuilder z biblioteki HttpMime , aby wykonać żądane żądanie.
W moim projekcie robię to w ten sposób:
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();
Mam nadzieję, że to pomoże.
(Zaktualizowano ten post, aby użyć MultipartEntityBuilder zamiast przestarzałego MultipartEntity, używając kodu @ mtomy jako przykładu)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-11 11:33:25
MultipartEntity jest teraz przestarzały. Używam apache httpclient 4.3.3 - czy ktoś wie z czego mamy korzystać zamiast tego? Uważam, że wyszukiwarki google są tak pełne MultipartEntity przykłady niczego nie mogę znaleźć. - vextorspace Mar 31 '14 at 20: 36
Oto przykładowy kod w 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);
Aby użyć klasy MultipartEntityBuilder, potrzebujesz httpmime, która jest pod-projektem 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
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-22 01:35:46
Jeśli używasz org.Apacz.commons.httpclient.Pakiet HttpClient, może to ci pomoże!
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();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-06 13:02:14