HttpClient POST 乱码问题(httpmime-4.3.5)

86193952 2014-08-15

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.util.Log;

public class HttpClientUtil {

    public final static String Method_POST = "POST";

    public final static String Method_GET = "GET";

    /**
     * multipart/form-data form
     */
    public static boolean submitForm(MultipartForm form) {

        boolean result = false;

        // response string
        //String responseStr = "";

        // create HttpClient instance
        HttpClient httpClient = new DefaultHttpClient();
        //        httpClient.getParams().setParameter("http.protocol.content-charset", HTTP.UTF_8);
        //        httpClient.getParams().setParameter(HTTP.CONTENT_ENCODING, HTTP.UTF_8);
        //        httpClient.getParams().setParameter(HTTP.CHARSET_PARAM, HTTP.UTF_8);
        //        httpClient.getParams().setParameter(HTTP.DEFAULT_PROTOCOL_CHARSET, HTTP.UTF_8);

        try {
            HttpPost httpPost = new HttpPost(form.getAction());
            //            httpPost.getParams().setParameter("http.protocol.content-charset", HTTP.UTF_8);
            //            httpPost.getParams().setParameter(HTTP.CONTENT_ENCODING, HTTP.UTF_8);
            //            httpPost.getParams().setParameter(HTTP.CHARSET_PARAM, HTTP.UTF_8);
            //            httpPost.getParams().setParameter(HTTP.DEFAULT_PROTOCOL_CHARSET, HTTP.UTF_8);
            //            httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");

            // create MultipartEntityBuilder
            MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();

            // add text field
            List<String> normalKeyField = form.getNormalKeyField();
            List<String> normalField = form.getNormalField();
            for (int i = 0; i < normalField.size(); i++) {
                String key = normalKeyField.get(i);
                String value = normalField.get(i);
                if (value == null || value.length() <= 0) {
                    continue;
                }
                ContentType contentType = ContentType.create(HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8);
                entityBuilder.addPart(key, new StringBody(value, contentType));
                //                entityBuilder.addPart(key, new StringBody(value, ContentType.TEXT_PLAIN));
            }

            // add file field
            List<String> fileFieldKey = form.getFileKeyField();
            List<File> fileField = form.getFileField();
            for (int i = 0; i < fileFieldKey.size(); i++) {
                String key = fileFieldKey.get(i);
                File file = fileField.get(i);
                entityBuilder.addPart(key, new FileBody(file));
            }

            // set request entity
            httpPost.setEntity(entityBuilder.build());

            // send request
            HttpResponse response = httpClient.execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();
            // get response
            HttpEntity resEntity = response.getEntity();
            String responseStr;
            if (200 == statusCode) {
                if (resEntity != null) {
                    responseStr = EntityUtils.toString(resEntity);
                    Log.i("WV_DEBUG", "submit form responseStr: " + responseStr);
                }
                result = true;
            } else {
                result = false;
            }
        } catch (Exception e) {
            Log.e("RecipeUtil.TAG", "submit form failure: ", e);
        } finally {
            httpClient.getConnectionManager().shutdown();
        }

        return result;
    }

    /** form Bean */
    public class MultipartForm implements Serializable {
        /** ID */
        private static final long serialVersionUID = -2138044819190537198L;

        /** URL **/
        private String action = "";

        /** method:POST/GET **/
        private String method = "POST";

        /** text Field **/
        private List<String> normalField = new ArrayList<String>();

        private List<String> normalKeyField = new ArrayList<String>();

        /** file Field **/
        private List<File> fileField = new ArrayList<File>();

        private List<String> fileKeyField = new ArrayList<String>();

        public String getAction() {
            return action;
        }

        public void setAction(String action) {
            this.action = action;
        }

        public String getMethod() {
            return method;
        }

        public void setMethod(String method) {
            this.method = method;
        }

        public List<String> getNormalField() {
            return normalField;
        }

        public void setNormalField(List<String> normalField) {
            this.normalField = normalField;
        }

        public List<String> getNormalKeyField() {
            return normalKeyField;
        }

        public void setNormalKeyField(List<String> normalKeyField) {
            this.normalKeyField = normalKeyField;
        }

        public List<File> getFileField() {
            return fileField;
        }

        public void setFileField(List<File> fileField) {
            this.fileField = fileField;
        }

        public List<String> getFileKeyField() {
            return fileKeyField;
        }

        public void setFileKeyField(List<String> fileKeyField) {
            this.fileKeyField = fileKeyField;
        }

        public void addFileField(String key, File value) {
            fileKeyField.add(key);
            fileField.add(value);
        }

        public void addNormalField(String key, String value) {
            normalKeyField.add(key);
            normalField.add(value);
        }
    }

}

相关推荐