Android DefaultHttpClient POST方法无法设置Content Length,得到Http Error 411

时间:2013-04-19 03:17:31

标签: java android post httpclient http-status-code-411

我在Android 4.2上遇到HttpPost问题。我试图调用.NET WebAPI服务中托管的身份验证服务。

该服务要求将请求作为POST方法,并提供多个自定义请求标头。请求的主体将Json值发送给服务。

以下是我撰写请求的方式;

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://api.fekke.com/api/account/login");
httppost.addHeader(HTTP.TARGET_HOST, "api.fekke.com");
httppost.addHeader("Accept", "application/json");
httppost.addHeader("Fekke-AccessKey", "some-access-key");
httppost.addHeader("Date", dateString);
httppost.addHeader("Fekke-Signature", "some-encoded-value");
httppost.addHeader("Content-Type", "application/json; charset=utf-8");

String jsonmessage = "{\"Username\": \"myusername\", \"Password\": \"mypassword987\"}";
httppost.setEntity(new StringEntity(jsonmessage, HTTP.UTF_8));

HttpResponse response = httpclient.execute(httppost);

我尝试使用“Content-Length”标头调用它,并且它会抛出一个异常,说明请求中已存在标头。

我也尝试使用HttpURLConnection对象来调用它,谷歌建议从这一点开始使用它,但这也会遇到同样的问题。

我已经能够毫无问题地从iOS和.NET发出此请求调用,所以我知道它不是服务。

更新#1

我通过本地版本的服务运行了以下示例,并且我能够将错误隔离到我在其中一个http标头中传递散列值时。我正在使用JWT安全令牌,该令牌使用HMAC SHA-256哈希算法来设置服务使用的值。此散列值会导致执行请求爆炸。

更新#2

我终于能够解决问题了。问题是由Base64 encodeToString方法引起的,该方法为值添加了一个不可见的字符。它迫使标题中的腐败。我使用默认设置0或Base64.DEFAULT。我将值更改为Base64.NO_WRAP,它解决了问题。

1 个答案:

答案 0 :(得分:0)

如果使用正确的方法,您不必担心在帖子上设置'content-length'标题。使用Post,您通常会使用一些重载版本的'SetEntity($ Type-stream / string / encodedArray ...)'

查看此source(搜索长度),以查看API如何处理帖子上的长度的示例。

你应该评估你将用于http的lib,并找到显示像'setEntity'这样的方法的POST示例来设置帖子的正文。这样,您就不会收到与内容长度相关的错误。见example

示例代码“HttpConnection”类 - 使用我评论中的链接...

对于JSON或位图:

try {                       
    HttpResponse response = null;
    switch (method) {
    case GET:
        HttpGet httpGet = new HttpGet(url);             
        response = httpClient.execute(httpGet);
        break;
    //Note on NIO - using a FileChannel and  mappedByteBuffer may be NO Faster
    // than using std httpcore  http.entity.FileEntity
    case POST:
        //TODO bmp to stream to bytearray for data entity
        HttpPost httpPost = new HttpPost(url); //urlends "audio OR "pic" 
        if (  !url.contains("class") ){                 
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            //can alter belo for smaller uploads to parse .JPG , 40,strm
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);               
            httpPost.setEntity(new ByteArrayEntity(stream.toByteArray()));

        }else if(data != null && (url.contains("class"))){
            //bug data is blank
            httpPost.setEntity(new StringEntity(data));
            Log.d(TAG, "DATA in POST run-setup " +data);
        }

        response = httpClient.execute(httpPost);
        break;
    case PUT:
        HttpPut httpPut = new HttpPut(url);
        httpPut.setEntity(new StringEntity(data));
        response = httpClient.execute(httpPut);
        break;
    case DELETE:
        response = httpClient.execute(new HttpDelete(url));
        break;
    case BITMAP:
        response = httpClient.execute(new HttpGet(url));
        processBitmapEntity(response.getEntity());
        break;
    }
    if (method < BITMAP)
        processEntity(response.getEntity());
} catch (Exception e) {
    handler.sendMessage(Message.obtain(handler,
            HttpConnection.DID_ERROR, e));
}

对于Post中的XML主体:

try {
    HttpResponse response = null;
    switch (method) {

    case POST:
        HttpPost httpPost = new HttpPost(url);
        if (data != null){
            System.out.println(" post data not null ");
            httpPost.setEntity(new StringEntity(data));
        }
        if (entry != null){
            ContentProducer cp = new ContentProducer() {
                public void writeTo(OutputStream outstream) throws IOException {

                     ExtensionProfile ep = new ExtensionProfile();
                     ep.addDeclarations(entry);
                     XmlWriter xmlWriter = new XmlWriter(new OutputStreamWriter(outstream, "UTF-8"));
                     entry.generate(xmlWriter, ep);
                     xmlWriter.flush();
                }
            };
            httpPost.setEntity(new EntityTemplate(cp));
        }
        httpPost.addHeader("GData-Version", "2");
        httpPost.addHeader("X-HTTP-Method-Override", "PATCH");
        httpPost.addHeader("If-Match", "*");
        httpPost.addHeader("Content-Type", "application/xml");
        response = httpClient.execute(httpPost);

        break;
    }
    if (method < BITMAP)
        processEntity(response.getEntity());
} catch (Exception e) {
    handler.sendMessage(Message.obtain(handler,
            HttpConnection.DID_ERROR, e));
}