Java HttpClient两种数据传输方式
来源:互联网 发布:阿里云系统怎么root 编辑:程序博客网 时间:2024/05/18 02:52
一、HttpClient两种Post数据传输方式
package com.httpclient.util;import org.apache.http.HttpEntity;import org.apache.http.ParseException;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.ByteArrayEntity;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import org.junit.Test;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.List;public class HttpClientUtil{ @Test public void jUnitTestPost() { postParam(); } @Test public void jUnitTestGet() { get(); } /** * 第一种:post字节流 * @param lineTxt * @throws IOException */ public static void postByte(String lineTxt) throws IOException { // 创建默认的httpClient实例. CloseableHttpClient httpClient = HttpClients.createDefault(); // 创建httpPost HttpPost httpPost = new HttpPost("http://127.0.0.1:8080/data/store?cacheName=cache02"); try { httpPost.setEntity(new ByteArrayEntity(lineTxt.getBytes("utf8")));//使用字节流传输 httpPost.setHeader("Content-type", "text/xml;charset=utf8");//内容类型 httpPost.setHeader("Connection", "Close");//服务端发送完数据,即关闭连接 //HTTP 1.0规定浏览器与服务器只保持短暂的连接,浏览器的每次请求都需要与服务器建立一个TCP连接,服务器完成请求处理后立即断开TCP连接,服务器不跟踪每个客户也不记录过去的请求 CloseableHttpResponse response = httpClient.execute(httpPost); try { HttpEntity entity = response.getEntity(); if (entity != null) { String rs=EntityUtils.toString(entity, "UTF-8"); System.out.println(rs); } } finally { response.close(); return; } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭连接,释放资源 try { httpClient.close(); return; } catch (IOException e) { e.printStackTrace(); } } } /** * 第二种:post参数列表 */ public void postParam() { // 创建默认的httpClient实例. CloseableHttpClient httpclient = HttpClients.createDefault(); // 创建httppost HttpPost httppost = new HttpPost("https://c.api.weibo.com/2/comments/create/biz.json"); // 创建参数队列 List<BasicNameValuePair> formParams = new ArrayList<BasicNameValuePair>(); formParams.add(new BasicNameValuePair("access_token", "2.00SdyOsBnp71ED50b943f4670l75U8")); formParams.add(new BasicNameValuePair("id", "4087614108017463")); formParams.add(new BasicNameValuePair("comment", "text")); UrlEncodedFormEntity uefEntity; try { uefEntity = new UrlEncodedFormEntity(formParams, "UTF-8"); httppost.setEntity(uefEntity); System.out.println("executing request " + httppost.getURI()); CloseableHttpResponse response = httpclient.execute(httppost); try { HttpEntity entity = response.getEntity(); if (entity != null) { System.out.println("--------------------------------------"); System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8")); System.out.println("--------------------------------------"); } } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭连接,释放资源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 发送 get请求 */ public void get() { CloseableHttpClient httpclient = HttpClients.createDefault(); try { // 创建httpget. HttpGet httpget = new HttpGet("http://www.baidu.com/"); System.out.println("executing request " + httpget.getURI()); // 执行get请求. CloseableHttpResponse response = httpclient.execute(httpget); try { // 获取响应实体 HttpEntity entity = response.getEntity(); System.out.println("--------------------------------------"); // 打印响应状态 System.out.println(response.getStatusLine()); if (entity != null) { // 打印响应内容长度 System.out.println("Response content length: " + entity.getContentLength()); // 打印响应内容 System.out.println("Response content: " + EntityUtils.toString(entity)); } System.out.println("------------------------------------"); } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭连接,释放资源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } }}
二、server端的数据接收方式,使用@RequestBody接收二进制字节流,使用@RequestParam接收参数列表
@ResponseBody @RequestMapping(produces = "text/xml;charset=utf8", value = "/store", method = {RequestMethod.POST}) public String commitData(@RequestParam("cacheName") String cacheName, @RequestBody String requestData) { JSONObject responseData = new JSONObject(); try { JSONArray requestDatas = JSONArray.parseArray(requestData); logger.info("接受到数据:[" + requestData.length() + "]. AllData: " + requestData); JSONArray rs=dataStoreService.storeData(requestDatas,cacheName,configManager); responseData.put("rs", rs); responseData.put("success", true); } catch (Exception e) { logger.error("store documents exception:[" + e.getMessage() + "].", e); responseData.put("success", false); responseData.put("errorMsg", "store documents exception:[" + e.getMessage() + "]."); } finally { return responseData.toJSONString(); } }
0 0
- Java HttpClient两种数据传输方式
- 两种数据传输的方式-get/post
- http协议几种数据传输方式
- Form表单两种数据传输的方式——get和post的区别
- MVC_3种数据传递方式
- silabs 大话si4463两种数据传输模式
- 学习小记_1(几种数据传输方式)
- 自学小记_4(几种数据传输方式)
- anroid五种数据传递方式
- Activity五种数据传递方式
- Intent四种数据传递方式
- AJAX的几种数据传输
- Ajax之三种数据传输格式
- React实战-模块化条件下的6种数据传递方式
- 自学小记_2(几种数据传输技术)
- Fragment之间的数据传递 三种数据传递方式的相关操作
- grpc-服务端与客户端四种数据传递方式(2)
- 两种数据库连接方式
- 列表的常用内置方法笔记
- \t\tmatlab常见的程序出错问题整理【转载】
- 强大的find和grep命令
- \t\t大小写字母的转换
- \t\tRun-Time Check Failure #2
- Java HttpClient两种数据传输方式
- wampserver2.5局域网IP访问配置
- Jmeter发送SOAP请求对WebService接口压力测试
- \t\tASCII码表(转载)
- \t\t位运算-运算符(转载)
- 前端动画渲染引擎pixi.js系列(5)Text文本和Graphics画笔工具
- 广播的巧妙利用——仿QQ实现强制下线功能
- java语言的特性与工作原理
- \t\t美职业顾问公布15个谷歌面试题(转载)