I have two byte[] arrays from the Android Camera Service. I want to POST them, and a few parameters to my AppEngine server, running the python webapp framework.
PROBLEM: I keep getting empty HTTP request arguments on the server side.
My main approach has been Apache HttpClient:
1) Android 2.x doesn't include the MultiPartEntity class, needed for multipart w/ binaries. So I've added httpmime-4.0.1.jar and apache-mime4j-0.6.1.jar to the build path.
2) Android side, I'm doing the POST like this:
public String post(String URI, byte[] jpeg, String description) {
// Setup MultiPartEntity
MultipartEntity args = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
args.addPart("mydescription", new StringBody(description));
// Now add the file
InputStream s1 = new ByteArrayInputStream(jpeg);
args.addPart("myfile", new InputStreamBody(s1, "image/jpeg", "1.jpeg"));
HttpClient httpclient = new DefaultHttpClient();
// HTTP 1.1 is much faster with HttpClient, same issues w/o it
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httpost = new HttpPost(URI);
httpost.setEntity(args);
HttpResponse response = httpclient.execute(httpost);
// blah blah blah, process response
}
3) Python AppEngine side, my handler looks like:
class UploadHandler(webapp.RequestHandler):
def post(self, request):
logging.info(self.request.arguments())开发者_运维技巧
logging.info(self.request.POST)
4) The arguments are empty -> empty arrays are printed to the log. The lower-level self.request._request__body()
from webob is also empty. bad sign!
5) If I don't add the InputStreamBody to the MultipartEntity (only StringBody args) everything works fine, and the mydescription argument shows up.
6) I setup a PHP server and tried posting: the POST works with PHP!
7) Something about the format HttpClient is sending causes webapp/webob/wsgi/cgi.FieldStorage or something problems. I can't figure out where its breaking.
8) I've also tried writing raw http multipart/form according to RFC 2388 using URLConnection, with similar results. What RFC is webapp/webob/wsgi/whatever following?
Thanks all!
This is my first major stackoverflow question, hope I've formatted everything right ;-)
精彩评论