1. package com.ricoh.rapp.ezcx.iwbservice.webservice;
  2.  
  3. import java.io.BufferedOutputStream;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.io.OutputStreamWriter;
  7. import java.net.HttpURLConnection;
  8. import java.net.SocketException;
  9. import java.net.SocketTimeoutException;
  10. import java.net.URL;
  11.  
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14.  
  15. import com.alibaba.fastjson.JSON;
  16. import com.alibaba.fastjson.JSONObject;
  17. import com.ricoh.rapp.ezcx.iwbservice.util.Constant;
  18. import com.ricoh.rapp.ezcx.iwbservice.util.Utils;
  19.  
  20. public class HttpRequest {
  21. private final Logger logger = LoggerFactory.getLogger(HttpRequest.class);
  22.  
  23. /**
  24. * http请求
  25. *
  26. * @param urlPath
  27. * 请求路径http://127.0.0.1:8080/index
  28. * @param requestStr
  29. * 请求参数
  30. *
  31. */
  32. public JSONObject execute(String urlPath, String requestStr) {
  33. HttpURLConnection httpConnection = null;
  34. try {
  35. URL url = new URL(urlPath);
  36. httpConnection = (HttpURLConnection) url.openConnection();
  37.  
  38. httpConnection.setRequestMethod("POST");
  39. httpConnection.setDoOutput(true); // post请求为true;get请求为false并且不用out写入数据
  40. httpConnection.setDoInput(true); // 有相应数据
  41. httpConnection.setConnectTimeout("3 * 1000");
  42. httpConnection.setReadTimeout("Charset");
  43.  
  44. // set http header
  45. httpConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
  46. httpConnection.setRequestProperty("Charset", "UTF-8");
  47. httpConnection.connect();
  48.  
  49. // send data
  50. OutputStream buf = httpConnection.getOutputStream();
  51. buf = new BufferedOutputStream(buf);
  52.  
  53. OutputStreamWriter out = new OutputStreamWriter(buf);
  54. if (requestStr == null || requestStr.length() == 0) {
  55. requestStr = new JSONObject().toString();
  56. }
  57. out.write(requestStr);
  58. out.flush();
  59. out.close();
  60.  
  61. // receive data
  62. InputStream inputStream = httpConnection.getInputStream();
  63. if (httpConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
  64. logger.error("connect ezcs service failed: " + httpConnection.getResponseCode());
  65. JSONObject responseJson = new JSONObject();
  66. responseJson.put(ResponseKey.KEY_RESULT,
  67. com.ricoh.rapp.unifiedPlatform.dsdkService.constant.HttpConstant.ResultCode.ERROR_SERVER_HTTP_ERROR);
  68. return responseJson;
  69. }
  70. String response = Utils.convertStreamToString(inputStream, Constant.ENCODING_UTF_8);
  71. logger.debug("response from ezcs service: " + response);
  72. JSONObject responseJson = JSON.parseObject(response);
  73. return responseJson;
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. logger.debug("connect local ezcs service exception: " + e.getMessage());
  77. JSONObject responseJson = new JSONObject();
  78. if (e instanceof SocketTimeoutException || e instanceof SocketException) {
  79. responseJson.put(ResponseKey.KEY_RESULT,
  80. com.ricoh.rapp.unifiedPlatform.dsdkService.constant.HttpConstant.ResultCode.ERROR_SERVER_HTTP_ERROR);
  81. } else {
  82. responseJson.put(ResponseKey.KEY_RESULT,
  83. com.ricoh.rapp.unifiedPlatform.dsdkService.constant.HttpConstant.ResultCode.ERROR_INNER_ERROR);
  84. }
  85. return responseJson;
  86. } finally {
  87. if (httpConnection != null) {
  88. httpConnection.disconnect();
  89. }
  90. }
  91. }
  92.  
  93. public static class ResponseKey {
  94. public static final String KEY_RESULT = "result";
  95. public static final String KEY_REASON = "reason";
  96. public static final String KEY_DATA = "data";
  97. public static final String KEY_EXTRA = "extra";
  98. }
  99. }

Java的http post请求01之HttpURLConnection的更多相关文章

  1. Java中简单Http请求

    1. 概述 在这篇快速教程中,我们将使用Java内置类HttpUrlConnection来实现一个Http请求. 2. HttpUrlConnection HttpUrlConnection类允许我们 ...

  2. Http请求之基于HttpUrlConnection,支持Header,Body传值,支持Multipart上传文件:

    Http请求之基于HttpUrlConnection,支持Header,Body传值,支持Multipart上传文件: public static String post(String actionU ...

  3. 使用java的 htpUrlConnection post请求 下载pdf文件,然后输出到页面进行预览和下载

    使用java的 htpUrlConnection post请求 下载pdf文件,然后输出到页面进行预览和下载 2018年06月07日 10:42:26 守望dfdfdf 阅读数:235 标签: jav ...

  4. Java GET和POST请求

    从表面来看GET和POST请求: GET请求是在url后直接附上请求体,url和请求体之间用"?"分割,不同参数之间用"&"分隔,%XX中的XX为该符号 ...

  5. 接口测试-Java代码实现接口请求并封装

    前言:在接口测试和Java开发中对接口请求方法进行封装都非常有必要,无论是在我们接口测试的时候还是在开发自测,以及调用某些第三方接口时,都能为我们调用和调试接口提供便捷: Java实现对http请求的 ...

  6. Java多线程系列--“JUC锁”01之 框架

    本章,我们介绍锁的架构:后面的章节将会对它们逐个进行分析介绍.目录如下:01. Java多线程系列--“JUC锁”01之 框架02. Java多线程系列--“JUC锁”02之 互斥锁Reentrant ...

  7. java httpclient发送json 请求 ,go服务端接收

    /***java客户端发送http请求*/package com.xx.httptest; /** * Created by yq on 16/6/27. */ import java.io.IOEx ...

  8. Java 之 I/O 系列 01 ——基础

    Java 之 I/O 系列 目录 Java 之 I/O 系列 01 ——基础 Java 之 I/O 系列 02 ——序列化(一) Java 之 I/O 系列 02 ——序列化(二) 整理<疯狂j ...

  9. Java过滤器处理Ajax请求,Java拦截器处理Ajax请求,拦截器Ajax请求

    Java过滤器处理Ajax请求,Java拦截器处理Ajax请求,拦截器Ajax请求 >>>>>>>>>>>>>>&g ...

随机推荐

  1. 国外很便宜的服务器 一年 2核2G 一年20美元

    今年 服务器 //=====================================一下是去年的================================= 优惠码:zhujicepin ...

  2. git推送项目到github并使用gitee做镜像仓库

    2022最新版github入门教程,教你如何一步步创建自己的github账号并初始化仓库,然后使用git工具配置个人工作环境.配合gitee仓库,作为github的镜像仓库使用.这篇文章很基础,对萌新 ...

  3. Solution -「AGC 013E」「AT 2371」Placing Squares

    \(\mathcal{Description}\)   Link.   给定一个长度为 \(n\) 的木板,木板上有 \(m\) 个标记点,第 \(i\) 个标记点距离木板左端点的距离为 \(x_i\ ...

  4. Solution -「NOI 2018」「洛谷 P4768」归程

    \(\mathcal{Description}\)   Link.   给定一个 \(n\) 个点 \(m\) 条边的无向连通图,边形如 \((u,v,l,a)\).每次询问给出 \(u,p\),回答 ...

  5. Redis_RDB持久化之写时复制技术的应用

    背景: 最近生产环境中某个Set的Redis集群经常出现短暂的内存降低现象,经过查看日志是因为在RDB持久化所造成的内存突降(日志中:RDB: 4929 MB of memory used by co ...

  6. [源码解析] NVIDIA HugeCTR,GPU 版本参数服务器---(7) ---Distributed Hash之前向传播

    [源码解析] NVIDIA HugeCTR,GPU 版本参数服务器---(7) ---Distributed Hash之前向传播 目录 [源码解析] NVIDIA HugeCTR,GPU 版本参数服务 ...

  7. [Python]从哪里开始学习写代码(未完待续)

    预警:这只是我在学习中的一点感受,可能并不完全准确,也不包括面向对象编程的思想(我还不太懂),也有水文的嫌疑,大佬请温和批评指正或者绕道. 计算机语言 语言,是用来交流的.计算机是不能直接听懂人的语言 ...

  8. IP网络主动测评系统

    一.IT网络运维面临的挑战 1. 网络性能可视化 • 与公有云和SaaS平台连接的可靠性 • 广域网线路性能 • 互联网专线性能 2.诊断工具 • 现场无IT工程师覆盖 • 诊断的人力费用 • 网络与 ...

  9. Gerrit的用法及与gitlab的区别

    来到一个新的团队,开发的代码被同事覆盖了.找同事核实,同事却说根本没有看到我的代码.经过一番沟通了解,原来他们的代码没有直接在gitlab上操作,而是先提交到gerrit,然后在提交到git.但是代码 ...

  10. proto编译组件使用

    proto编译组件使用 前提:所有组件已经安装好,包括: protoc protoc-gen-go protoc-gen-grpc-gateway protoc-gen-swagger 怎么装再开一篇 ...