HttpURLConnection实现application/x-www-form-urlencoded类型提交

java之HttpURLConnection 2020-01-03 阅读 556 评论 0

application/x-www-form-urlencoded的数据被编码成以 '&' 分隔的键-值对,同时以 '=' 分隔键和值。这种类型不支持二进制数据(应使用 multipart/form-data 代替)。multipart/form-data的实现,可以参考HttpURLConnection实现multipart/form-data类型的提交

application/x-www-form-urlencoded的实现

application/x-www-urlencoded是浏览器默认的编码格式。下面是使用application/x-www-form-urlencoded类型,可以在android和java中使用。

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HttpPostForm {
    private HttpURLConnection httpConn;
    private Map<String, Object> queryParams;
    private String charset;

    /**
     * 构造方法
     *
     * @param requestURL    请求地址
     * @param charset       请求的编码
     * @param headers       请求头
     * @param queryParams   请求字段
     * @throws IOException
     */
    public HttpPostForm(String requestURL, String charset, Map<String, String> headers, Map<String, Object> queryParams) throws IOException {
        this.charset = charset;
        if (queryParams == null) {
            this.queryParams = new HashMap<>();
        } else {
            this.queryParams = queryParams;
        }
        URL url = new URL(requestURL);
        httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setUseCaches(false);
        httpConn.setDoOutput(true);    // 表明是post请求
        httpConn.setDoInput(true);
        httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        if (headers != null && headers.size() > 0) {
            Iterator<String> it = headers.keySet().iterator();
            while (it.hasNext()) {
                String key = it.next();
                String value = headers.get(key);
                httpConn.setRequestProperty(key, value);
            }
        }
    }

    public HttpPostForm(String requestURL, String charset, Map<String, String> headers) throws IOException {
        this(requestURL, charset, headers, null);
    }

    public HttpPostForm(String requestURL, String charset) throws IOException {
        this(requestURL, charset, null, null);
    }

    /**
     * 添加参数字段
     *
     * @param name
     * @param value
     */
    public void addFormField(String name, Object value) {
        queryParams.put(name, value);
    }

    /**
     * 添加请求头
     *
     * @param key
     * @param value
     */
    public void addHeader(String key, String value) {
        httpConn.setRequestProperty(key, value);
    }

    /**
     * 将请求字段转化成byte数组
     *
     * @param params
     * @return
     */
    private byte[] getParamsByte(Map<String, Object> params) {
        byte[] result = null;
        StringBuilder postData = new StringBuilder();
        for (Map.Entry<String, Object> param : params.entrySet()) {
            if (postData.length() != 0) {
                postData.append('&');
            }
            postData.append(this.encodeParam(param.getKey()));
            postData.append('=');
            postData.append(this.encodeParam(String.valueOf(param.getValue())));
        }
        try {
            result = postData.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 对键和值进行url编码
     *
     * @param data
     * @return
     */
    private String encodeParam(String data) {
        String result = "";
        try {
            result = URLEncoder.encode(data, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 完成请求,并接受服务器的回应
     *
     * @return 如果请求成功,状态码是200,返回服务器返回的字符串,否则抛出异常
     * @throws IOException
     */
    public String finish() throws IOException {
        String response = "";
        byte[] postDataBytes = this.getParamsByte(queryParams);
        httpConn.getOutputStream().write(postDataBytes);
        // 检查服务器返回状态
        int status = httpConn.getResponseCode();
        if (status == HttpURLConnection.HTTP_OK) {
            ByteArrayOutputStream result = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = httpConn.getInputStream().read(buffer)) != -1) {
                result.write(buffer, 0, length);
            }
            response = result.toString(this.charset);
            httpConn.disconnect();
        } else {
            throw new IOException("Server returned non-OK status: " + status);
        }
        return response;
    }
}

调用示例

try {
    // 请求头
    Map<String, String> headers = new HashMap<>();
    headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36");
    HttpPostForm httpPostForm = new HttpPostForm("http://localhost", "utf-8", headers);
    // post参数
    httpPostForm.addFormField("username", "用户名");
    httpPostForm.addFormField("password", "test_psw");
    // 返回信息
    String response = httpPostForm.finish();
    System.out.println(response);
} catch (Exception e) {
    e.printStackTrace();
}

下面是请求头信息,可以看到请求参数是由 '&' 符号拼接起来。

POST / HTTP/1.1
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36
Host: localhost
Cache-Control: no-cache
Pragma: no-cache
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Content-Length: 54
Connection: keep-alive

password=test_psw&username=%E7%94%A8%E6%88%B7%E5%90%8D


最后更新 2020-01-12