File upload through Android gives 503, but works f

2019-09-20 00:00发布

I have a web service written in PHP (runs on LAMP stack) that I use to upload a file and process it.

When I call this from my android app, the server returns error 503.

In Android, if I remove the file upload part and pass other parameters, it works !!!

If I comment all the code, still it gives error. So it looks like for some reason the file upload via Android is not working !!

Note : It was working until last week. Last week I moved my web server to new hosting (different IP) - That's all.

If I test from Chrome Rest Client, it works perfectly fine !!!

Same script on a different server (different domain name and IP) works fine.

What could be the error ? Why does it fail only when I call from Android app !!

Following is the Android code:

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(web_url);

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

InputStream is = PBAApplication.getInstance().getContext().getContentResolver().openInputStream(fileURI);
InputStreamBody isb = new InputStreamBody(is, fileName);

builder.addPart("type", new StringBody("APPM"));
builder.addPart("file", isb);

httpPost.setEntity(builder.build());

// Execute HTTP Request
HttpResponse response = httpClient.execute(httpPost);

Server error log shows this:

(32)Broken pipe: [client 219.74.158.38:21214] AH01075: Error dispatching request to : (sending stdin)

1条回答
Emotional °昔
2楼-- · 2019-09-20 00:03

There are a few things you can do to troubleshoot:

  1. Modify the server to dump the error.
  2. Try setting body content type to application/x-www-form-urlencoded
  3. Make sure the url is correct (http vs https).
  4. Look at the cookies in your rest client and make sure your headers match in the app.

To log all traffic going through HttpClient, use these terminal commands:

//        adb shell setprop log.tag.org.apache.http VERBOSE
//        adb shell setprop log.tag.org.apache.http.wire VERBOSE

Add this to your Application class:

    java.util.logging.Logger.getLogger("org.apache.http.wire").setLevel(java.util.logging.Level.FINEST);
java.util.logging.Logger.getLogger("org.apache.http.headers").setLevel(java.util.logging.Level.FINEST);
System.setProperty("org.apache.commons.logging.Log","org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "debug");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.headers", "debug");
System.setProperty("http.keepAlive", "false");
查看更多
登录 后发表回答