【httpclient】httpclient发送表单POST请求

浏览器Form Post请求

浏览器可以通过提交表单的方式向服务器发起POST请求,这种形式的POST请求不同于一般的POST请求

1. 一般的POST请求,将请求数据放置于请求体中,服务器端以二进制流的方式读取数据,HttpServletRequest.getInputStream()。这种方式的请求可以处理任意数据形式的POST请求,比如请求数据是字符串或者是二进制数据

2. Form POST请求,只能提交字符串而且是键zhi也是请求的数据放于POST请求体中,服务器端可以通过request.getParameter的方法获得请求参数的值,取值的方式跟GET通过url取出请求参数一样。

3.浏览器在提交表单的POST请求,自动添加如下Content-Type这个Header:Content-Type:application/x-www-form-urlencoded

HttpClient 4.0

httpclient 4.0 直接支持通过POST方式提交请求参数,如下是代码片段:

private static HttpPost performFormPost(String url, Map<String, String> params) throws IOException {  
          
        HttpPost httpost = new HttpPost(url);  
        List<NameValuePair> nvps = new ArrayList <NameValuePair>();  
          
        Set<String> keySet = params.keySet();  
        for(String key : keySet) {  
            nvps.add(new BasicNameValuePair(key, params.get(key)));  
        }      
        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));  
     }  
        return httpost;  
    }

httpclient 3.1

 public String formPost(String url, Map<String, String> params) throws IOException {
        if (url == null || url.trim().length() <= 0) {
            throw new IllegalArgumentException("POST url can not be empty");
        }
        Map<String, String> response = new HashMap<String, String>();
        PostMethod poster = new PostMethod(url);
        String content = "";
        String encoding = HTTP.UTF_8;
        String contentType = "application/x-www-form-urlencoded;charset=" + encoding;

        if (params != null && params.size() > 0) {
            content = format(params, encoding);
        }
        StringRequestEntity entity = new StringRequestEntity(content, contentType, encoding);
        poster.setRequestEntity(entity);
        httpClient.executeMethod(poster);
        return responseBodyAsString(poster);
    }

    private static String format(Map<String, String> parameters, final String encoding) {
        final StringBuilder sb = new StringBuilder();
        Set<Map.Entry<String, String>> entrySet = parameters.entrySet();
        Iterator<Map.Entry<String, String>> iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            final String encodedName = encode(entry.getKey(), encoding);
            final String value = entry.getValue();
            final String encodedValue = value != null ? encode(value, encoding) : "";
            if (sb.length() > 0) {
                sb.append("&");
            }
            sb.append(encodedName);
            sb.append("=");
            sb.append(encodedValue);
        }
        return sb.toString();
    }

    public static String decode(final String content, final String encoding) {
        try {
            return URLDecoder.decode(content, encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET);
        } catch (UnsupportedEncodingException problem) {
            throw new IllegalArgumentException(problem);
        }
    }

    public static String encode(final String content, final String encoding) {
        try {
            return URLEncoder.encode(content,  encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET);
        } catch (UnsupportedEncodingException problem) {
            throw new IllegalArgumentException(problem);
        }
    }

 

猜你喜欢

转载自bit1129.iteye.com/blog/2110934