Invalid Content-Length sent to server?

I am trying to upload a photo to the popular Dailybooth service through my new API using the method described here .

The problem is that the server is responding:

<html><head><title>411 Length Required</title>...

The code I use to send this data is here:

// 2: Build request
HttpClient httpclient = new DefaultHttpClient();
SharedPreferences settings = DailyboothShared.getPrefs(DailyboothTakePhoto.this);
String oauth_token = settings.getString("oauth_token", "");
HttpPost httppost = new HttpPost(
        "https://api.dailybooth.com/v1/pictures.json?oauth_token=" + oauth_token);
Log.d("upload", "Facebook: " + facebook);
Log.d("upload", "Twitter: " + twitter);
try {
    InputStream f = getContentResolver().openInputStream(snap_url);
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    entity.addPart("picture", new InputStreamBody(f, snap_url.getLastPathSegment()));
    entity.addPart("blurb", new StringBody(blurb));
    entity.addPart("publish_to[facebook]", new StringBody(facebook));
    entity.addPart("publish_to[twiter]", new StringBody(twitter));
    httppost.setEntity(entity);
    HttpResponse response = httpclient.execute(httppost);
    Log.d("upload", response.toString());
    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode == 200) {
        // do something?
    } else {
        Log.d("upload", "Something went wrong :/");
    }
    Log.d("upload", EntityUtils.toString(response.getEntity()));
} catch (Exception ex) {
    ex.printStackTrace();
}

I have no idea what I'm doing wrong.

+3
source share
1 answer

StringBody InputStreamBody, MultipartEntity. , StringBody.getContentLength() , InputStreamBody -1, , , , , , .

, , , InputStreamBody :

new InputStreamBody(f, snap_url.getLastPathSegment()) {

    public long getContentLength() {
        return /*your length*/;
    }
}

byte[] ByteArrayInputStream InputStreamBody, , , , ...

, , File ? , FileBody, content-length.

+8

Source: https://habr.com/ru/post/1785749/


All Articles