如何将UrlEncodedFormEntity添加到MultipartEntityBuilder而不是获得403?

时间:2015-11-18 18:58:23

标签: java android json http post

如何编码MultipartEntityBuilder而不是使用单独的方法?

我尝试了多种方法,但不断获得403状态代码。 这是一个例子:

private void doPost(int type) throws UnsupportedEncodingException {
    HttpPost post = new HttpPost("http://amazon.com");
    post.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded;charset=UTF-8");

    if(type == 1)
        post.setEntity(useBuilder());
    else if(type == 2)
        post.setEntity(useEncoder()); //<---- wanting to not have to do this
}

private HttpEntity useBuilder(){
    MultipartEntityBuilder me = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    me.addTextBody("name1", "value1");
    me.addTextBody("name2", "value2");
    return me.build();
}

private UrlEncodedFormEntity useEncoder() throws UnsupportedEncodingException {
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    list.add(new BasicNameValuePair("name1", "value1"));
    list.add(new BasicNameValuePair("name2", "value2"));
    return new UrlEncodedFormEntity(list, "UTF-8");
}

0 个答案:

没有答案