【问题标题】:How to set header for multipart httppost?如何为多部分 httppost 设置标题?
【发布时间】:2014-05-28 11:01:39
【问题描述】:

我正在尝试使用此代码在服务器上上传图像和文本,但使用此代码时出现一些错误“content-length header already present”,或者如果我没有设置内容长度,则返回“需要 411 长度"

请给我一些解决方案

这是我的代码

    String url = webserviceURLs.createQuestion+"?token"+OkayUserProfileProps.tocken; 
    try { 
        HttpClient httpclient = new DefaultHttpClient(); 
        HttpPost httppost = new HttpPost(url); 
        httppost.setHeader("Content-Type", "multipart/form-data");
        httppost.addHeader("Accept", "application/json");
        byte[] abc = convertImageToByte(Uri.parse(imageName.get(0)));

        InputStream in = new ByteArrayInputStream(abc);
        ContentBody mimePart = new InputStreamBody(in, "file1"); 
        MultipartEntity reqEntity = new MultipartEntity();

        reqEntity.addPart("userquestion[question_id]", new StringBody("2")); 
        reqEntity.addPart("userquestion[looks][0][photo]", mimePart);
        httppost.setEntity(reqEntity);   

        int a=abc.length;
        httppost.setHeader(HTTP.CONTENT_LEN,""+abc.length);

        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity resEntity = response.getEntity(); 


        if (resEntity != null) {       
            String retSrc = EntityUtils.toString(resEntity);
            System.out.println("Responce "+retSrc);

        }



        return null;
    } catch (Exception e) { 
        // TODO Auto-generated catch block
        Log.e("Exception", e.getMessage() + ""); 
        return null;
    } 

【问题讨论】:

  • 请给我一些解决方案

标签: android web-services http-post


【解决方案1】:

试试这个,

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("image", new FileBody(new File("your image path"));

【讨论】:

  • 显示 411 Length Required

    411 Length Required


    nginx/1.2.1
  • 使用 httppost.addHeader("Content-Type", "multipart/form-data");
  • request.addHeader("Accept-Encoding", "gzip, deflate");
  • 这里有什么要求?
  • 请求表示httppost
猜你喜欢
  • 2012-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-14
  • 2012-03-16
  • 2017-01-09
  • 2013-09-17
相关资源
最近更新 更多