HttpClient PostMethod方式
来源:互联网 发布:2016淘宝店铺二级域名 编辑:程序博客网 时间:2024/05/02 17:05
一、PostMethod一般请求
/** * POST方式 * @return * @throws Exception */ public static String PostMethodTest() throws Exception{ System.out.println("开始"); HttpClient client = new HttpClient(); PostMethod method = new PostMethod(URI); try{ method.addRequestHeader(new Header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ); // method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); method.addParameter(new NameValuePair("appid", "XXXX") ); method.addParameter(new NameValuePair("appkey", "XXXX") ); // method.addParameter("appid", "XXXX"); // method.addParameter("appkey", "XXXX"); int result = client.executeMethod(method); if (result == HttpStatus.SC_OK) { InputStream in = method.getResponseBodyAsStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = in.read(buffer)) != -1) { baos.write(buffer, 0, len); } return URLDecoder.decode(baos.toString(), "UTF-8"); } else { throw new Exception("HTTP ERROR Status: " + method.getStatusCode() + ":" + method.getStatusText()); } }finally { method.releaseConnection(); } }
二、PostMethod带文件方式
/** * POST方式 传带文件的调用 * @return * @throws Exception */ public static String PostMethodFileTest() throws Exception{ System.out.println("开始"); HttpClient client = new HttpClient(); PostMethod method = new PostMethod(URI); try{ FilePart filePart = new FilePart("file",new File("D:\\8\\5972-41-2017-06-07-1440-16406.wav"));//文件参数 StringPart questionId = new StringPart("questionId","10001");//普通参数 StringPart userId = new StringPart("userId","765709");//普通参数 StringPart homeworkId = new StringPart("homeworkId","950");//普通参数 Part[] parts ={filePart,questionId,userId,homeworkId}; MultipartRequestEntity mre=new MultipartRequestEntity(parts ,method.getParams()); //封装了普通字段和文件字段 method.setRequestEntity(mre ); int result = client.executeMethod(method); if (result == HttpStatus.SC_OK) { InputStream in = method.getResponseBodyAsStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = in.read(buffer)) != -1) { baos.write(buffer, 0, len); } return URLDecoder.decode(baos.toString(), "UTF-8"); } else { throw new Exception("HTTP ERROR Status: " + method.getStatusCode() + ":" + method.getStatusText()); } }finally { method.releaseConnection(); } }
阅读全文
0 0
- HttpClient PostMethod方式
- HttpClient PostMethod
- httpClient PostMethod
- HttpClient postmethod check a checkbox?
- HTTPClient PostMethod 中文乱码处理
- HttpClient PostMethod 中文乱码问题解决
- HttpClient PostMethod提交自定义参数
- httpclient 设置代理 PostMethod 示例
- httpclient中PostMethod和HttpPost
- HttpClient PostMethod提交自定义参数
- HTTPClient PostMethod 中文乱码问题解决方案
- HttpClient PostMethod/GetMethod 转码方案
- java HTTPClient PostMethod 中文乱码问题解决方法
- JAVA HTTPClient PostMethod 中文乱码问题解决方法
- [HttpClient]HTTPClient PostMethod 中文乱码问题解决方案(2种)
- [HttpClient]HTTPClient PostMethod 中文乱码问题解决方案(2种)
- [HttpClient]HTTPClient PostMethod 中文乱码问题解决方案(2种)
- httpClient使用postMethod方法发送请求,携带参数并解决中文乱码问题
- python之实战----KNN之手写数字位图
- 关于单链表的反转一种复杂的想法
- andriodjie——xRecyclerView网络获取数据简单实现
- android 断电续传
- Windows下libevent C++封装类实现
- HttpClient PostMethod方式
- zabbix3.2.7自定义key类型之计算(Calculated items)
- 网页的瀑布流布局
- Mac OS OpenCV安装及终端和Xcode的使用
- Bootstrap(一)
- QT 快捷键整理
- NOIP2017普及组考前问题总结
- 【CTF 攻略】如何绕过四个字符限制getshell
- css凹凸文字简单设置