注:以下用的是commons-httpclient-3.1.jar,往后面的版本写法不一样。
post提交示例:
/** * Url Post请求 * @param url url地址 * @param charset 字符编码 * @param params 参数 * @return */ public String doPost(String url, String charset,NameValuePair[] params) { StringBuffer response = new StringBuffer(); HttpClient client = new HttpClient(); PostMethod postMethod = new PostMethod(url); //表单域的值 // NameValuePair[] data = {new NameValuePair("name", "test")}; postMethod.setRequestBody(params); //解决中文乱码问题 postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8"); try { int statusCode = client.executeMethod(postMethod); if (statusCode == HttpStatus.SC_OK) { BufferedReader reader = new BufferedReader(new InputStreamReader( postMethod.getResponseBodyAsStream(), charset)); String line; while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); } } catch (HttpException e) { SysLog.sysLogError(e.getMessage()); } catch (UnsupportedEncodingException e) { SysLog.sysLogError(e.getMessage()); } catch (IOException e) { SysLog.sysLogError(e.getMessage()); }finally { postMethod.releaseConnection(); } return response.toString(); }
get提交示例:
/** * Url Get请求 * @param url url地址 * @param charset 字符编码 * @return */ public String doGet(String url, String charset) { StringBuffer response = new StringBuffer(); HttpClient client = new HttpClient(); HttpMethod method = null; try { // String urlPath = URIUtil.encodePath(url); // String urlPath = URIUtil.encodePath(url, "GBK"); method = new GetMethod(url); client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_OK) { BufferedReader reader = new BufferedReader(new InputStreamReader( method.getResponseBodyAsStream(), charset)); String line; while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); } } catch (URIException e) { SysLog.sysLogError("[HTTP GET请求URL字符串编码异常]:: " + e.getMessage()); } catch (IOException e) { SysLog.sysLogError("[HTTP GET请求URL读写异常]:: " + e.getMessage()); } finally { method.releaseConnection(); } return response.toString(); }
注:如果url中有中文参数需要转码,不然会抛异常。
URLEncoder.encode("软件园", "UTF-8")
相关推荐
HttpClient实现POST GET和文件下载 HttpClient实现POST GET和文件下载
httpclient的用法,发送get请求和post请求,设置header
HttpClient模拟http发送post和get请求
httpClient httpPost httpGet 所需jar包 httpClient httpPost httpGet 所需jar包
http请求工具类HttpClientUtil,get,post请求(csdn)————程序
HttpClient Post提交多文件及多个普通参数,已经封装成工具类。 需传入 要请求的url 普通参数map 例 map.put("param1","张三"); 需要传入的文件流map 其中key为文件名 服务端接收无乱码。
利用c# httpclient ,post json数据到web api
利用httpclient-4.3.1.jar、httpcore-4.3.jar包,很简单的用java实现httpget和httppost请求。
一个简单的易学的 基于HttpClient 4.3发送psot及get请求,返回数据,适合初学者,适合初学者
HttpClientUtil工具类发送get和post请求,支持http和https,支持发送文件
java HttpClient 发送GET请求和带有表单参数的POST请求教程例子
MFC使用H偷偷跑Client的GET和POST方法 MFC使用H偷偷跑Client的GET和POST方法
HttpClient发送http请求(post+get)需要的jar包+内符java代码案例+注解详解(这款工具类是我精心挑选适合广大java程序猿使用)
httpclient get/post请求工具类(map参数封装),方便调用
采用 HttpClient GET、POST 方式 提交数据到服务器
本代码是httpclient发送get请求和post请求demo,具体描述参考博客 HttpClient发送get请求和post请求
NULL 博文链接:https://javasam.iteye.com/blog/2117845
具体来说,它创建了一个HttpClient实例和一个HttpPost对象,设置了请求的URL、请求头和请求体,然后执行请求并获取响应。最后,它将响应内容输出到控制台。 这个代码的意义在于展示了如何使用Java中的HttpClient库...
mfc http post 和get 请求