Java的http post请求01之HttpURLConnection
- package com.ricoh.rapp.ezcx.iwbservice.webservice;
- import java.io.BufferedOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.net.HttpURLConnection;
- import java.net.SocketException;
- import java.net.SocketTimeoutException;
- import java.net.URL;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.ricoh.rapp.ezcx.iwbservice.util.Constant;
- import com.ricoh.rapp.ezcx.iwbservice.util.Utils;
- public class HttpRequest {
- private final Logger logger = LoggerFactory.getLogger(HttpRequest.class);
- /**
- * http请求
- *
- * @param urlPath
- * 请求路径http://127.0.0.1:8080/index
- * @param requestStr
- * 请求参数
- *
- */
- public JSONObject execute(String urlPath, String requestStr) {
- HttpURLConnection httpConnection = null;
- try {
- URL url = new URL(urlPath);
- httpConnection = (HttpURLConnection) url.openConnection();
- httpConnection.setRequestMethod("POST");
- httpConnection.setDoOutput(true); // post请求为true;get请求为false并且不用out写入数据
- httpConnection.setDoInput(true); // 有相应数据
- httpConnection.setConnectTimeout("3 * 1000");
- httpConnection.setReadTimeout("Charset");
- // set http header
- httpConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
- httpConnection.setRequestProperty("Charset", "UTF-8");
- httpConnection.connect();
- // send data
- OutputStream buf = httpConnection.getOutputStream();
- buf = new BufferedOutputStream(buf);
- OutputStreamWriter out = new OutputStreamWriter(buf);
- if (requestStr == null || requestStr.length() == 0) {
- requestStr = new JSONObject().toString();
- }
- out.write(requestStr);
- out.flush();
- out.close();
- // receive data
- InputStream inputStream = httpConnection.getInputStream();
- if (httpConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
- logger.error("connect ezcs service failed: " + httpConnection.getResponseCode());
- JSONObject responseJson = new JSONObject();
- responseJson.put(ResponseKey.KEY_RESULT,
- com.ricoh.rapp.unifiedPlatform.dsdkService.constant.HttpConstant.ResultCode.ERROR_SERVER_HTTP_ERROR);
- return responseJson;
- }
- String response = Utils.convertStreamToString(inputStream, Constant.ENCODING_UTF_8);
- logger.debug("response from ezcs service: " + response);
- JSONObject responseJson = JSON.parseObject(response);
- return responseJson;
- } catch (Exception e) {
- e.printStackTrace();
- logger.debug("connect local ezcs service exception: " + e.getMessage());
- JSONObject responseJson = new JSONObject();
- if (e instanceof SocketTimeoutException || e instanceof SocketException) {
- responseJson.put(ResponseKey.KEY_RESULT,
- com.ricoh.rapp.unifiedPlatform.dsdkService.constant.HttpConstant.ResultCode.ERROR_SERVER_HTTP_ERROR);
- } else {
- responseJson.put(ResponseKey.KEY_RESULT,
- com.ricoh.rapp.unifiedPlatform.dsdkService.constant.HttpConstant.ResultCode.ERROR_INNER_ERROR);
- }
- return responseJson;
- } finally {
- if (httpConnection != null) {
- httpConnection.disconnect();
- }
- }
- }
- public static class ResponseKey {
- public static final String KEY_RESULT = "result";
- public static final String KEY_REASON = "reason";
- public static final String KEY_DATA = "data";
- public static final String KEY_EXTRA = "extra";
- }
- }
Java的http post请求01之HttpURLConnection的更多相关文章
- Java中简单Http请求
1. 概述 在这篇快速教程中,我们将使用Java内置类HttpUrlConnection来实现一个Http请求. 2. HttpUrlConnection HttpUrlConnection类允许我们 ...
- Http请求之基于HttpUrlConnection,支持Header,Body传值,支持Multipart上传文件:
Http请求之基于HttpUrlConnection,支持Header,Body传值,支持Multipart上传文件: public static String post(String actionU ...
- 使用java的 htpUrlConnection post请求 下载pdf文件,然后输出到页面进行预览和下载
使用java的 htpUrlConnection post请求 下载pdf文件,然后输出到页面进行预览和下载 2018年06月07日 10:42:26 守望dfdfdf 阅读数:235 标签: jav ...
- Java GET和POST请求
从表面来看GET和POST请求: GET请求是在url后直接附上请求体,url和请求体之间用"?"分割,不同参数之间用"&"分隔,%XX中的XX为该符号 ...
- 接口测试-Java代码实现接口请求并封装
前言:在接口测试和Java开发中对接口请求方法进行封装都非常有必要,无论是在我们接口测试的时候还是在开发自测,以及调用某些第三方接口时,都能为我们调用和调试接口提供便捷: Java实现对http请求的 ...
- Java多线程系列--“JUC锁”01之 框架
本章,我们介绍锁的架构:后面的章节将会对它们逐个进行分析介绍.目录如下:01. Java多线程系列--“JUC锁”01之 框架02. Java多线程系列--“JUC锁”02之 互斥锁Reentrant ...
- java httpclient发送json 请求 ,go服务端接收
/***java客户端发送http请求*/package com.xx.httptest; /** * Created by yq on 16/6/27. */ import java.io.IOEx ...
- Java 之 I/O 系列 01 ——基础
Java 之 I/O 系列 目录 Java 之 I/O 系列 01 ——基础 Java 之 I/O 系列 02 ——序列化(一) Java 之 I/O 系列 02 ——序列化(二) 整理<疯狂j ...
- Java过滤器处理Ajax请求,Java拦截器处理Ajax请求,拦截器Ajax请求
Java过滤器处理Ajax请求,Java拦截器处理Ajax请求,拦截器Ajax请求 >>>>>>>>>>>>>>&g ...
随机推荐
- 国外很便宜的服务器 一年 2核2G 一年20美元
今年 服务器 //=====================================一下是去年的================================= 优惠码:zhujicepin ...
- git推送项目到github并使用gitee做镜像仓库
2022最新版github入门教程,教你如何一步步创建自己的github账号并初始化仓库,然后使用git工具配置个人工作环境.配合gitee仓库,作为github的镜像仓库使用.这篇文章很基础,对萌新 ...
- Solution -「AGC 013E」「AT 2371」Placing Squares
\(\mathcal{Description}\) Link. 给定一个长度为 \(n\) 的木板,木板上有 \(m\) 个标记点,第 \(i\) 个标记点距离木板左端点的距离为 \(x_i\ ...
- Solution -「NOI 2018」「洛谷 P4768」归程
\(\mathcal{Description}\) Link. 给定一个 \(n\) 个点 \(m\) 条边的无向连通图,边形如 \((u,v,l,a)\).每次询问给出 \(u,p\),回答 ...
- Redis_RDB持久化之写时复制技术的应用
背景: 最近生产环境中某个Set的Redis集群经常出现短暂的内存降低现象,经过查看日志是因为在RDB持久化所造成的内存突降(日志中:RDB: 4929 MB of memory used by co ...
- [源码解析] NVIDIA HugeCTR,GPU 版本参数服务器---(7) ---Distributed Hash之前向传播
[源码解析] NVIDIA HugeCTR,GPU 版本参数服务器---(7) ---Distributed Hash之前向传播 目录 [源码解析] NVIDIA HugeCTR,GPU 版本参数服务 ...
- [Python]从哪里开始学习写代码(未完待续)
预警:这只是我在学习中的一点感受,可能并不完全准确,也不包括面向对象编程的思想(我还不太懂),也有水文的嫌疑,大佬请温和批评指正或者绕道. 计算机语言 语言,是用来交流的.计算机是不能直接听懂人的语言 ...
- IP网络主动测评系统
一.IT网络运维面临的挑战 1. 网络性能可视化 • 与公有云和SaaS平台连接的可靠性 • 广域网线路性能 • 互联网专线性能 2.诊断工具 • 现场无IT工程师覆盖 • 诊断的人力费用 • 网络与 ...
- Gerrit的用法及与gitlab的区别
来到一个新的团队,开发的代码被同事覆盖了.找同事核实,同事却说根本没有看到我的代码.经过一番沟通了解,原来他们的代码没有直接在gitlab上操作,而是先提交到gerrit,然后在提交到git.但是代码 ...
- proto编译组件使用
proto编译组件使用 前提:所有组件已经安装好,包括: protoc protoc-gen-go protoc-gen-grpc-gateway protoc-gen-swagger 怎么装再开一篇 ...