HttpClient3.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package com.kingdee.eas.custom.ssPDF.utils;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import java.io.*;
  5. import java.net.HttpURLConnection;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. public class HttpClient3 {
  9. public static byte[] getBytesByNetURL(String urlStr) throws IOException {
  10. // RestTemplate restTemplate = new RestTemplate();
  11. // ResponseEntity<byte[]> responseEntity = restTemplate.exchange(urlStr, HttpMethod.GET, null, byte[].class);
  12. // byte[] fileContent = responseEntity.getBody();
  13. URL url = new URL(urlStr);
  14. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  15. // //设置超时时间
  16. conn.setConnectTimeout(5 * 1000);
  17. // //通过输入流获取图片数据
  18. InputStream in = conn.getInputStream();
  19. // //得到图片的二进制数据
  20. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  21. byte[] buffer = new byte[1024];
  22. int len;
  23. while ((len = in.read(buffer)) != -1) {
  24. outputStream.write(buffer, 0, len);
  25. }
  26. in.close();
  27. return outputStream.toByteArray();
  28. // return fileContent;
  29. }
  30. /**
  31. * http get请求
  32. * @param httpUrl 链接
  33. * @return 响应数据
  34. */
  35. public static InputStream doGet(String httpUrl){
  36. //链接
  37. HttpURLConnection connection=null;
  38. InputStream is=null;
  39. BufferedReader br = null;
  40. StringBuffer result=new StringBuffer();
  41. try {
  42. //创建连接
  43. URL url=new URL(httpUrl);
  44. connection= (HttpURLConnection) url.openConnection();
  45. //设置请求方式
  46. connection.setRequestMethod("GET");
  47. //设置连接超时时间
  48. connection.setConnectTimeout(15000);
  49. //设置读取超时时间
  50. connection.setReadTimeout(15000);
  51. //开始连接
  52. connection.connect();
  53. //获取响应数据
  54. if(connection.getResponseCode()==200){
  55. //获取返回的数据
  56. is=connection.getInputStream();
  57. // if(is!=null){
  58. // br=new BufferedReader(new InputStreamReader(is,"UTF-8"));
  59. // String temp = null;
  60. // while ((temp=br.readLine())!=null){
  61. // result.append(temp);
  62. // }
  63. // }
  64. }
  65. } catch (MalformedURLException e) {
  66. e.printStackTrace();
  67. } catch (IOException e) {
  68. e.printStackTrace();
  69. }finally {
  70. if(br!=null){
  71. try {
  72. br.close();
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. }
  76. }
  77. if(is!=null){
  78. try {
  79. is.close();
  80. } catch (IOException e) {
  81. e.printStackTrace();
  82. }
  83. }
  84. connection.disconnect();// 关闭远程连接
  85. }
  86. return is;
  87. }
  88. /**
  89. * post请求
  90. * @param httpUrl 链接
  91. * @param param 参数
  92. * @return
  93. */
  94. public static JSONObject doPost(String httpUrl, String param) {
  95. StringBuffer result=new StringBuffer();
  96. //连接
  97. HttpURLConnection connection=null;
  98. OutputStream os=null;
  99. InputStream is=null;
  100. BufferedReader br=null;
  101. JSONObject jsonObject = null;
  102. try {
  103. //创建连接对象
  104. URL url=new URL(httpUrl);
  105. //创建连接
  106. connection= (HttpURLConnection) url.openConnection();
  107. //设置请求方法
  108. connection.setRequestMethod("POST");
  109. //设置连接超时时间
  110. connection.setConnectTimeout(15000);
  111. //设置读取超时时间
  112. connection.setReadTimeout(15000);
  113. //设置是否可读取
  114. connection.setDoOutput(true);
  115. //设置响应是否可读取
  116. connection.setDoInput(true);
  117. //设置参数类型
  118. connection.setRequestProperty("Content-Type", "application/json");
  119. //拼装参数
  120. if(param!=null&&!param.equals("")){
  121. //设置参数
  122. os=connection.getOutputStream();
  123. //拼装参数
  124. os.write(param.getBytes("UTF-8"));
  125. }
  126. //设置权限
  127. //设置请求头等
  128. //开启连接
  129. //connection.connect();
  130. //读取响应
  131. if(connection.getResponseCode()==200){
  132. is=connection.getInputStream();
  133. if(is!=null){
  134. br=new BufferedReader(new InputStreamReader(is,"UTF-8"));
  135. String temp=null;
  136. if((temp=br.readLine())!=null){
  137. result.append(temp);
  138. }
  139. }
  140. jsonObject = JSON.parseObject(String.valueOf(result));
  141. }
  142. //关闭连接
  143. } catch (MalformedURLException e) {
  144. e.printStackTrace();
  145. } catch (IOException e) {
  146. e.printStackTrace();
  147. }finally {
  148. if(br!=null){
  149. try {
  150. br.close();
  151. } catch (IOException e) {
  152. e.printStackTrace();
  153. }
  154. }
  155. if(os!=null){
  156. try {
  157. os.close();
  158. } catch (IOException e) {
  159. e.printStackTrace();
  160. }
  161. }
  162. if(is!=null){
  163. try {
  164. is.close();
  165. } catch (IOException e) {
  166. e.printStackTrace();
  167. }
  168. }
  169. //关闭连接
  170. connection.disconnect();
  171. }
  172. return jsonObject;
  173. }
  174. public static void main(String[] str){
  175. /*String result=HttpURLConnectionUtil.doGet("http://localhost:8080/test");
  176. System.out.println(result);*/
  177. /*Map<String, Object> map=new HashMap<>();
  178. map.put("1","1");
  179. map.put("2","2");
  180. map.put("3","3");
  181. System.out.println(HttpURLConnectionUtil.getParamStr(map));*/
  182. }
  183. }