一、HttpClient类

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStream;
  6. import java.net.HttpURLConnection;
  7. import java.net.MalformedURLException;
  8. import java.net.URL;
  9.  
  10. public class HttpClient {
  11. public static String doGet(String httpurl) {
  12. HttpURLConnection connection = null;
  13. InputStream is = null;
  14. BufferedReader br = null;
  15. String result = null;// 返回结果字符串
  16. try {
  17. // 创建远程url连接对象
  18. URL url = new URL(httpurl);
  19. // 通过远程url连接对象打开一个连接,强转成httpURLConnection类
  20. connection = (HttpURLConnection) url.openConnection();
  21. // 设置连接方式:get
  22. connection.setRequestMethod("GET");
  23. // 设置连接主机服务器的超时时间:15000毫秒
  24. connection.setConnectTimeout(15000);
  25. // 设置读取远程返回的数据时间:60000毫秒
  26. connection.setReadTimeout(60000);
  27. // 发送请求
  28. connection.connect();
  29. // 通过connection连接,获取输入流
  30. if (connection.getResponseCode() == 200) {
  31. is = connection.getInputStream();
  32. // 封装输入流is,并指定字符集
  33. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  34. // 存放数据
  35. StringBuffer sbf = new StringBuffer();
  36. String temp = null;
  37. while ((temp = br.readLine()) != null) {
  38. sbf.append(temp);
  39. sbf.append("\r\n");
  40. }
  41. result = sbf.toString();
  42. }
  43. } catch (MalformedURLException e) {
  44. e.printStackTrace();
  45. } catch (IOException e) {
  46. e.printStackTrace();
  47. } finally {
  48. // 关闭资源
  49. if (null != br) {
  50. try {
  51. br.close();
  52. } catch (IOException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56.  
  57. if (null != is) {
  58. try {
  59. is.close();
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64.  
  65. connection.disconnect();// 关闭远程连接
  66. }
  67.  
  68. return result;
  69. }
  70.  
  71. public static String doPost(String httpUrl, String param) {
  72.  
  73. HttpURLConnection connection = null;
  74. InputStream is = null;
  75. OutputStream os = null;
  76. BufferedReader br = null;
  77. String result = null;
  78. try {
  79. URL url = new URL(httpUrl);
  80. // 通过远程url连接对象打开连接
  81. connection = (HttpURLConnection) url.openConnection();
  82. // 设置连接请求方式
  83. connection.setRequestMethod("POST");
  84. // 设置连接主机服务器超时时间:15000毫秒
  85. connection.setConnectTimeout(15000);
  86. // 设置读取主机服务器返回数据超时时间:60000毫秒
  87. connection.setReadTimeout(60000);
  88.  
  89. // 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true
  90. connection.setDoOutput(true);
  91. // 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无
  92. connection.setDoInput(true);
  93. // 设置传入参数的格式:请求参数应该是 name1=value1&name2=value2 的形式。
  94. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  95. // 设置鉴权信息:Authorization: Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0
  96. connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  97. // 通过连接对象获取一个输出流
  98. os = connection.getOutputStream();
  99. // 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的
  100. os.write(param.getBytes());
  101. // 通过连接对象获取一个输入流,向远程读取
  102. if (connection.getResponseCode() == 200) {
  103.  
  104. is = connection.getInputStream();
  105. // 对输入流对象进行包装:charset根据工作项目组的要求来设置
  106. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  107.  
  108. StringBuffer sbf = new StringBuffer();
  109. String temp = null;
  110. // 循环遍历一行一行读取数据
  111. while ((temp = br.readLine()) != null) {
  112. sbf.append(temp);
  113. sbf.append("\r\n");
  114. }
  115. result = sbf.toString();
  116. }
  117. } catch (MalformedURLException e) {
  118. e.printStackTrace();
  119. } catch (IOException e) {
  120. e.printStackTrace();
  121. } finally {
  122. // 关闭资源
  123. if (null != br) {
  124. try {
  125. br.close();
  126. } catch (IOException e) {
  127. e.printStackTrace();
  128. }
  129. }
  130. if (null != os) {
  131. try {
  132. os.close();
  133. } catch (IOException e) {
  134. e.printStackTrace();
  135. }
  136. }
  137. if (null != is) {
  138. try {
  139. is.close();
  140. } catch (IOException e) {
  141. e.printStackTrace();
  142. }
  143. }
  144. // 断开与远程地址url的连接
  145. connection.disconnect();
  146. }
  147. return result;
  148. }
  149. }

二、main方法

  1. import java.util.Date;
  2.  
  3. public class Main {
  4.  
  5. public static void main(String[] args) {
  6. str = HttpClient.doPost("https://www.baidu.com?tstmp="+Math.random(),"p=1");
  7. System.out.println(str);
  8. }
  9.  
  10. }

原生HttpClient详细使用示例的更多相关文章

  1. oracle中to_date详细用法示例(oracle日期格式转换)

    这篇文章主要介绍了oracle中to_date详细用法示例,包括期和字符转换函数用法.字符串和时间互转.求某天是星期几.两个日期间的天数.月份差等用法 TO_DATE格式(以时间:2007-11-02 ...

  2. MySQL备份与还原详细过程示例

    MySQL备份与还原详细过程示例 一.MySQL备份类型 1.热备份.温备份.冷备份 (根据服务器状态) 热备份:读.写不受影响: 温备份:仅可以执行读操作: 冷备份:离线备份:读.写操作均中止: 2 ...

  3. httpclient详细介绍

    1.HttpClient简介 HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 ...

  4. React Native:使用 JavaScript 构建原生应用 详细剖析

    数月前,Facebook 对外宣布了正在开发的 React Native 框架,这个框架允许你使用 JavaScript 开发原生的 iOS 应用——就在今天,Beta 版的仓库释出了! 基于 Pho ...

  5. HttpClient配置及示例代码

    HttpComponents是Apache 旗下的项目.其中有一个HttpClient,即HTTP客户端. ... ... 大多时候我们只需要HttpClient,httpCore是开发服务端的我们可 ...

  6. ZooKeeper(3.4.5) - 原生 API 的简单示例

    一.创建会话 1. 创建一个基本的ZooKeeper会话实例 package com.huey.dream.demo; import java.util.concurrent.CountDownLat ...

  7. JAVA: httpclient 详细说明——第四章;

    httpclient 具体解释--第一章. httpclient 具体解释--第二章: httpclient 具体解释--第三章: httpclient 具体解释--第四章: httpclient 具 ...

  8. HttpClient详细解释

    Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日后我们再讨论),它不仅是客户端发送Http请求变得容易,而且 ...

  9. java 原生 HttpClient

    package org.rx.socks.http; import com.google.common.base.Strings; import lombok.SneakyThrows; import ...

随机推荐

  1. sqlite 数据库 boolean类型的小小测试

    根据官方文档的介绍: SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored ...

  2. jforum(1)--环境搭建

    JForum 是采用Java开发的功能强大且稳定的论坛系统.它提供了抽象的接口.高效的论坛引擎以及易于使用的管理界面,同时具有完全的权限控制.多语言支持(包括中文).高性能.可自定义的用户接口.安全. ...

  3. numpy中的随机数模块

    https://www.cnblogs.com/td15980891505/p/6198036.html numpy.random模块中提供啦大量的随机数相关的函数. 1 numpy中产生随机数的方法 ...

  4. python+turtle 笔记

    用Python+turtle绘制佩琪: from turtle import * def nose(x,y):#鼻子 penup()#提起笔 goto(x,y)#定位 pendown()#落笔,开始画 ...

  5. Python并发编程之同步\异步and阻塞\非阻塞

    一.什么是进程 进程: 正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 进程和程序的区别: 程序仅仅只是一堆代码而已,而进程指的是程序的运行过程. 需要强调的是:同一个程序执行两次,那也 ...

  6. ETL过程跑完后,使用python发送邮件

    目标库中,如果有行数为0的表,使用python发送邮件 # -*- coding:utf-8 -*- # Author: zjc # Description:send monitor info to ...

  7. ACM-ICPC 2018 徐州赛区网络预赛 B BE, GE or NE(记忆化搜索)

    https://nanti.jisuanke.com/t/31454 题意 两个人玩游戏,最初数字为m,有n轮,每轮三个操作给出a b c,a>0表示可以让当前数字加上a,b>0表示可以让 ...

  8. [再寄小读者之数学篇](2014-06-23 二阶导数估计 [中国科学技术大学2013年高等数学B 考研试题])

    设 $f(x)$ 二阶连续可导, $f(0)=f(1)=0$, $\dps{\max_{0\leq x\leq 1}f(x)=2}$. 证明: $$\bex \min_{0\leq x\leq 1}f ...

  9. docker学习------docker私有仓库的搭建

    192.168.138.102:23451.私有仓库的搭建(docker pull registry),拉取最新的镜像 2.查看拉取的仓库镜像(docker images) 3.启用registry镜 ...

  10. I. Max answer(RMQ预处理前缀和)

    题目链接: https://nanti.jisuanke.com/t/38228 题目大意:给你n个数,让你找出一个区间中f的最大值,具体的f计算方法,这段区间的和乘以这段区间的最小值. 具体思路:我 ...