【httpclient】httpclient发送表单POST请求
编程技术  /  houtizong 发布于 3年前   63
浏览器可以通过提交表单的方式向服务器发起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 直接支持通过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; }
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); } }
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接