Multipart posts upload images - setting transfer encoding, setting headers for each part
Using java, I need to开发者_如何学Python do a multipart form request to Constant Contacts api to upload an image. I need to create one part with XML per their specifications to describe the file and the second part needs to be the file itself. Not a problem. Problem is properly constructing httppost. Heres an excerpt of the relative code:
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, null);
entity.addPart("Body", new StringBody(content,"application/atom+xml",Charset.forName( "UTF-8" )));
entity.addPart("File", new FileBody (new File("C:\temp\removeRed.gif"), "image/gif"));
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
return response;
I am always getting a 404 response. I know I have a valid authenticated connection and the link I am posting to is correct. Their specification say that the first part content-type header must be set to application/atom+xml. The other specification they give is that the second part, the file, transfer-encoding must be set to binary. Can some one please shed some light as to how to properly set headers, transfer-encoding etc, for each “part” Any help would be much appreciated.
entity.addPart("Body",
new StringBody(stringbufferObject,Charset.forName( "UTF-8" )));
it has to be set in andriod also if u r using some special charecters in StringBuffer
精彩评论