【问题标题】:How to post data with "Content-Type " header as "multipart/form-data"如何将带有“Content-Type”标头的数据发布为“multipart/form-data”
【发布时间】:2013-03-28 09:26:18
【问题描述】:

我正在尝试使用multipart/form-data 发布数据,但它不适用于 httpPost 方法。我必须从 API 以 json 字符串的形式发送数据,但是如果我删除 content-type 标头,服务器会给出错误的响应,响应说没有编辑以下数据的权限, 我必须使用 POST 从 API 发送 2 个参数,

下面是我的代码:

public String webPostMultiForm(List<NameValuePair> params) {
        String postUrl = webServiceUrl;
        httpPost = new HttpPost(postUrl);
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            httpPost.addHeader("Cookie",appStatus.getSharedStringValue(appStatus.AUTH_KEY)); 
            httpPost.addHeader("Content-Type","multipart/form-data; boundary=assdsfdffafasf");
            httpPost.addHeader("Content-Disposition", "form-data; name= args");
            //httpPost.setHeader("Transfer-Encoding","chunked");
        } catch (UnsupportedEncodingException uee) {
            Log.e(TAG, uee.getMessage());
        }
        Log.e(TAG,"WebGetURL: " + postUrl);
        try {           
            response = httpClient.execute(httpPost);
        } catch (Exception e) {
            if (e.getMessage() != null) {
                Log.e(TAG, "httpClient.execute(httpPost) Exception: " + e.getMessage());
            } else {
                Log.e(TAG, "httpClient.execute(httpPost) Exception: " + e.getClass().toString());
            }
        }
        try {
            strResponse = EntityUtils.toString(response.getEntity());
        } catch (Exception e) {
            if (e.getMessage() != null) {
                Log.e(TAG, e.getMessage());
            } else {
                Log.e(TAG, e.getClass().toString());
            }
        }
        return strResponse;
    }

有什么我做错了,或者任何其他方法。

【问题讨论】:

    标签: java android http-headers mime-types


    【解决方案1】:

    http://loopj.com/android-async-http/ 使用它怎么样。 你不必担心任何事情。 它的重量很轻。

    【讨论】:

      猜你喜欢
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      • 2021-04-17
      • 2019-05-27
      • 2018-12-16
      相关资源
      最近更新 更多