Android:HttpPost作为GET而不是POST发送:如何纠正?

时间:2015-09-24 16:07:52

标签: android

看起来我的Android httpPost仅限于5K字节!

public static boolean upload(String url, String content)
        throws IOException {

    Log.d(TAG, "upload data begin to url:" + url);

    HttpClient httpClient = createHttpClient();
    Uri uri = Uri.parse(url);

    Uri.Builder builder = uri.buildUpon();
    builder.appendQueryParameter("key", content);
    HttpPost httpPost = new HttpPost(builder.build().toString());

    try {
        HttpResponse postResponse = httpClient.execute(httpPost);
        int statusCode = postResponse.getStatusLine().getStatusCode();
        postResponse.getEntity().consumeContent();
        return statusCode == HttpStatus.SC_OK;
    } catch (ClientProtocolException e) {
        Log.w(TAG, "upload data failure", e);
        throw e;
    } catch (IOException e) {
        Log.w(TAG, "upload data failure", e);
        throw e;
    } catch(Exception e) {
        Log.w(TAG, "upload data failure", e);
    }
    return false;
}

在查看我得到的Apache日志时:

[error] [client ] request failed: URI too long (longer than 8190)

它看起来像是GET而不是POST

2 个答案:

答案 0 :(得分:0)

您应该首先使用POST,而不是GET

另一方面,理论上没有限制,它只受配置值的限制。有关更多信息,请参阅此处的Apache文档:

http://httpd.apache.org/docs/2.2/mod/core.html#limitrequestline

答案 1 :(得分:0)

  

所以它看起来像是作为GET发送而不是POST

没有。您只需将所有内容放入URL即可。构造函数采用URL,而不是POST正文。

由于HttpClient已从Android 6.0中的Android SDK中删除,因此我无法轻松将您与Android HttpPost文档相关联。但是,AFAIK,它类似于the 4.5.1 API,其中构造函数参数是URL。