HttpClient的get和post方式提交数据的使用
数据 方式 get 提交 post httpclient 使用
2023-09-14 08:59:01 时间
/** * Http工具类 */ public class HttpUtil { // 创建HttpClient对象 public static HttpClient httpClient = new DefaultHttpClient(); public static final String BASE_URL = ""; /** * get请求 * * @param url * 发送请求的URL * @return 服务器响应字符串 * @throws Exception */ public static String doGet(String url) throws Exception { // 创建HttpGet对象。 HttpGet get = new HttpGet(url); // 发送GET请求 HttpResponse httpResponse = httpClient.execute(get); // 如果服务器成功地返回响应 if (httpResponse.getStatusLine().getStatusCode() == 200) { // 获取服务器响应字符串 HttpEntity entity = httpResponse.getEntity(); InputStream content = entity.getContent(); return convertStreamToString(content); } return null; } /** * post请求 * * @param url * 发送请求的URL * @param params * 请求参数 * @return 服务器响应字符串 * @throws Exception */ public static String doPost(String url, Map<String, String> rawParams) throws Exception { // 创建HttpPost对象。 HttpPost post = new HttpPost(url); // 如果传递参数个数比较多的话可以对传递的参数进行封装 List<NameValuePair> params = new ArrayList<NameValuePair>(); for (String key : rawParams.keySet()) { // 封装请求参数 params.add(new BasicNameValuePair(key, rawParams.get(key))); } // 设置请求参数 post.setEntity(new UrlEncodedFormEntity(params, "utf-8")); // 发送POST请求 HttpResponse httpResponse = httpClient.execute(post); // 如果服务器成功地返回响应 if (httpResponse.getStatusLine().getStatusCode() == 200) { // 获取服务器响应字符串 HttpEntity entity = httpResponse.getEntity(); InputStream content = entity.getContent(); return convertStreamToString(content); } return null; } /** * 获取服务器的响应,转换为字符串 */ private static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } }
相关文章
- R_Studio(关联)对dvdtrans.csv数据进行关联规则分析
- 安卓换苹果如何转移便签数据?
- ASP.NET MVC 数据传递进阶 从数据库拿到数据后的三种方式
- 使用Exchanger实现两个线程之间的数据交互
- 海量积分数据实时排名处理方式介绍一
- 大数据时代数据管理方式研究
- 多域名THINKPHP利用MEMCACHE方式共享SESSION数据
- C语言/C++常见习题问答集锦(四十六) 之定义学生结构体(学生数据均以二进制方式输出)
- 如何使用代码的方式删除 SAP CRM 订单 Text 数据
- json格式数据 ,将数据库中查询的结果转换为json(方式2)
- SAP成都研究院小伙伴们开发的一个SAP C4C POC - 通过名片扫描的方式录入联系人数据到系统
- Keras之ML~P:基于Keras中建立的回归预测的神经网络模型(根据200个数据样本预测新的5+1个样本)——回归预测
- Keras之MLPR:利用MLPR算法(1to1+【Input(1)→8(relu)→O(mse)】)实现根据历史航空旅客数量数据集(时间序列数据)预测下月乘客数量问题
- 有什么轻量级的大数据技术
- ClickHouse 合并树表引擎 MergeTree 索引与数据存储方式
- getline数据来源你的三种方式
- 数据分析师培训机构带你3分钟快速入门数据分析
- varint算法——本质上是牺牲最高位作为标识数据结束位,达到变长编码,说白了就是贪心的分割位
- WPF-数据绑定:日期时间格式
- 基于小波变换的数据峰值检测matlab仿真
- 用户画像数据指标体系之用户消费维度
- 【大数据开发运维解决方案】Ogg For Bigdata 同步Oracle数据到KAFKA(包括初始化历史数据)