描述: http通讯基础类
package com.founder.ec.web.util.payments.payeco.http;
import com.founder.ec.web.util.payments.payeco.http.ssl.SslConnection;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.SortedMap;
/**
* 描述: http通讯基础类
*
*/
public class HttpClient {
private int status;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public static String REQUEST_METHOD_GET = "GET";
public static String REQUEST_METHOD_POST = "POST";
public String send(String postURL, String requestBody,
String sendCharset, String readCharset) {
return send(postURL,requestBody,sendCharset,readCharset,300,300);
}
public String send(String postURL, String requestBody,
String sendCharset, String readCharset,String RequestMethod) {
return send(postURL,requestBody,sendCharset,readCharset,300,300,RequestMethod);
}
/**
* @param postURL 访问地址
* @param requestBody paramName1=paramValue1¶mName2=paramValue2
* @param sendCharset 发送字符编码
* @param readCharset 返回字符编码
* @param connectTimeout 连接主机的超时时间 单位:秒
* @param readTimeout 从主机读取数据的超时时间 单位:秒
* @return 通讯返回
*/
public String send(String url, String requestBody,
String sendCharset, String readCharset,int connectTimeout,int readTimeout) {
try {
return connection(url,requestBody,sendCharset,readCharset,connectTimeout,readTimeout,REQUEST_METHOD_POST);
} catch (Exception ex) {
ex.printStackTrace();
System.out.print("发送请求[" + url + "]失败," + ex.getMessage());
return null;
}
}
/**
* @param postURL 访问地址
* @param requestBody paramName1=paramValue1¶mName2=paramValue2
* @param sendCharset 发送字符编码
* @param readCharset 返回字符编码
* @param connectTimeout 连接主机的超时时间 单位:秒
* @param readTimeout 从主机读取数据的超时时间 单位:秒
* @param RequestMethod GET或POST
* @return 通讯返回
*/
public String send(String url, String requestBody,
String sendCharset, String readCharset,int connectTimeout,int readTimeout,String RequestMethod) {
try {
return connection(url,requestBody,sendCharset,readCharset,connectTimeout,readTimeout,RequestMethod);
} catch (Exception ex) {
ex.printStackTrace();
System.out.print("发送请求[" + url + "]失败," + ex.getMessage());
return null;
}
}
public String connection(String postURL, String requestBody,
String sendCharset, String readCharset,int connectTimeout,int readTimeout,String RequestMethod)throws Exception {
if(REQUEST_METHOD_POST.equals(RequestMethod)){
return postConnection(postURL,requestBody,sendCharset,readCharset,connectTimeout,readTimeout,null);
}else if(REQUEST_METHOD_GET.equals(RequestMethod)){
return getConnection(postURL,requestBody,sendCharset,readCharset,connectTimeout,readTimeout,null);
}else{
return "";
}
}
@SuppressWarnings("rawtypes")
public String getConnection(String url, String requestBody,
String sendCharset, String readCharset,int connectTimeout,int readTimeout,SortedMap reqHead)throws Exception {
// Post请求的url,与get不同的是不需要带参数
HttpURLConnection httpConn = null;
try {
if (!url.contains("https:")) {
URL postUrl = new URL(url);
// 打开连接
httpConn = (HttpURLConnection) postUrl.openConnection();
} else {
SslConnection urlConnect = new SslConnection();
httpConn = (HttpURLConnection) urlConnect.openConnection(url);
}
httpConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded; charset=" + sendCharset);
if(reqHead!=null&&reqHead.size()>0){
Iterator iterator =reqHead.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
String val = (String)reqHead.get(key);
httpConn.setRequestProperty(key,val);
}
}
// 设定传送的内容类型是可序列化的java对象
// (如果不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException) httpUrlConnection.setRequestProperty("Content-type", "application/x-java-serialized-object");
// 连接主机的超时时间(单位:毫秒)
httpConn.setConnectTimeout(1000 * connectTimeout);
// 从主机读取数据的超时时间(单位:毫秒)
httpConn.setReadTimeout(1000 * readTimeout);
// 连接,从postUrl.openConnection()至此的配置必须要在 connect之前完成,
// 要注意的是connection.getOutputStream会隐含的进行 connect。
httpConn.connect();
int status = httpConn.getResponseCode();
setStatus(status);
if (status != HttpURLConnection.HTTP_OK) {
System.out.print("发送请求失败,状态码:[" + status + "] 返回信息:"
+ httpConn.getResponseMessage());
return null;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn
.getInputStream(), readCharset));
StringBuffer responseSb = new StringBuffer();
String line = null;
while ((line = reader.readLine()) != null) {
responseSb.append(line.trim());
}
reader.close();
return responseSb.toString().trim();
} finally {
httpConn.disconnect();
}
}
@SuppressWarnings("rawtypes")
public String postConnection(String postURL, String requestBody,
String sendCharset, String readCharset,int connectTimeout,int readTimeout,SortedMap reqHead)throws Exception {
// Post请求的url,与get不同的是不需要带参数
HttpURLConnection httpConn = null;
try {
if (!postURL.contains("https:")) {
URL postUrl = new URL(postURL);
// 打开连接
httpConn = (HttpURLConnection) postUrl.openConnection();
} else {
SslConnection urlConnect = new SslConnection();
httpConn = (HttpURLConnection) urlConnect.openConnection(postURL);
}
// 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在
// http正文内,因此需要设为true, 默认情况下是false;
httpConn.setDoOutput(true);
// 设置是否从httpUrlConnection读入,默认情况下是true;
httpConn.setDoInput(true);
// 设定请求的方法为"POST",默认是GET
httpConn.setRequestMethod("POST");
// Post 请求不能使用缓存
httpConn.setUseCaches(false);
//进行跳转
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded; charset=" + sendCharset);
if(reqHead!=null&&reqHead.size()>0){
Iterator iterator =reqHead.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
String val = (String)reqHead.get(key);
httpConn.setRequestProperty(key,val);
}
}
// 设定传送的内容类型是可序列化的java对象
// (如果不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException) httpUrlConnection.setRequestProperty("Content-type", "application/x-java-serialized-object");
// 连接主机的超时时间(单位:毫秒)
httpConn.setConnectTimeout(1000 * connectTimeout);
// 从主机读取数据的超时时间(单位:毫秒)
httpConn.setReadTimeout(1000 * readTimeout);
// 连接,从postUrl.openConnection()至此的配置必须要在 connect之前完成,
// 要注意的是connection.getOutputStream会隐含的进行 connect。
httpConn.connect();
DataOutputStream out = new DataOutputStream(httpConn.getOutputStream());
out.write(requestBody.getBytes(sendCharset));
out.flush();
out.close();
int status = httpConn.getResponseCode();
setStatus(status);
if (status != HttpURLConnection.HTTP_OK) {
System.out.print("发送请求失败,状态码:[" + status + "] 返回信息:"
+ httpConn.getResponseMessage());
return null;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn
.getInputStream(), readCharset));
StringBuffer responseSb = new StringBuffer();
String line = null;
while ((line = reader.readLine()) != null) {
responseSb.append(line.trim());
}
reader.close();
return responseSb.toString().trim();
} finally {
httpConn.disconnect();
}
}
public static void main(String[] args) {
}
}
描述: http通讯基础类的更多相关文章
- 基于 Paramiko 的 SSH 通讯类
# -*- coding: UTF-8 -*-import paramikoimport time################################################### ...
- 多平台Client TCP通讯组件
Beetle.NetPackage是一个多平台Client Socket TCP通讯组件(Apache License 2.0),组件制统一的对象协议制定规则,可以灵活方便地通过对象来描述TCP通讯交 ...
- C# 串口操作系列(5)--通讯库雏形
C# 串口操作系列(5)--通讯库雏形 标签: 通讯c#数据分析byteclassstring 2010-08-09 00:07 21378人阅读 评论(73) 收藏 举报 分类: 通讯类库设计(4 ...
- 发送http请求
public static String httpGetSend(String url) { String responseMsg = ""; HttpClient httpCli ...
- java socket编程(也是学习多线程的例子)详细版----转
7.2 面向套接字编程 我们已经通过了解Socket的接口,知其所以然,下面我们就将通过具体的案例,来熟悉Socket的具体工作方式 7.2.1使用套接字实现基于TCP协议的服务器和客户机程序 ...
- [置顶] Django 微信开发(一)——环境搭建
Django 微信开发(一)——环境搭建 随着移动互联网时代的到来,微信——一个改变着我们生活的产品悄悄走近了我们的生活.我们不得不觉得自己很幸运,自己能在这个世界上遇到像QQ.微博.微信这样优秀的产 ...
- 参考sectools,每个人至少查找5种安全工具、库等信息并深入研究至少两种并写出使用教程
1.Nessus Nessus是免费网络漏洞扫描器,它可以运行于几乎所有的UNIX平台之上.它不仅能永久升级,还免费提供多达11000种插件(但需要注册并接受EULA-acceptance--终端用户 ...
- 利用Java反射机制优化简单工厂设计模式
之前项目有个需求,审批流程的时候要根据配置发送信息:发送短信.发送邮件.当时看到这个就想到要用工厂模式,为什么要用工厂模式呢?用工厂模式进行大型项目的开发,可以很好的进行项目并行开发.就是一个程序员和 ...
- 工控随笔_22_关于Profibus网络接线的规则
最近在做一个项目调试,用的是西门子的PLC,416-2 DP,下面挂了几个DP子网,在进行现场网络测试的时候,有几个走的DP网络的 绝对值编码器,无论怎么弄DP网络不能联通. 一开始我以为DP网线接的 ...
随机推荐
- 虚拟机搭建和安装Hadoop及启动
马士兵hadoop第一课:虚拟机搭建和安装hadoop及启动 马士兵hadoop第二课:hdfs集群集中管理和hadoop文件操作 马士兵hadoop第三课:java开发hdfs 马士兵hadoop第 ...
- mongodb crud
//添加数据 db.users.insert({,"gender":"男"}); db.users.insert({"name":" ...
- ASP.NET WebAPI构建API接口服务实战演练
一.课程介绍 一.王小二和他领导的第一次故事 有一天王小二和往常一下去上早班,刚吃完早餐刚一打开电脑没一会儿.王小二的领导宋大宝走到他的面前,我们现在的系统需要提供服务给其他内部业务系统,我看你平时喜 ...
- mysql从库Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'报错处理
年后回来查看mysql运行状况与备份情况,登录mysql从库查看主从同步状态 mysql> show slave status\G; *************************** . ...
- ThinikPhp 将数据库模型的增、删、改操作写入日志
Thinkphp中的模型可以对数据库字段进行验证规则的设置和设置一些字段的默认值(比如字段为当前时间)以及在操作数据时的的一些回调方法等 基本上每一个模型都需要设置一些验证规则和字段默认值的设置, ...
- 马斯克:有62%的程序员认为人工智能会被武器化 #精选AR人工智能算法
当地时间 9 月 13 日,马斯克在自己的个人推特账号上转推了一篇名为<Hackers Have Already Started to Weaponize Artificial Intellig ...
- (原)ubuntu中使用conda安装tensorflow-gpu
转载请注明出处: https://www.cnblogs.com/darkknightzh/p/9834567.html 参考网址: https://www.anaconda.com/blog/dev ...
- mysql 5.5.x zip直接解压版 报1076
到官网下载mysql-5.5.10-win32.zip,然后将mysql解压到任意路径,如:C:\mysql-5.5.10-win32 打开计算机->属性->高级系统设置->环境变量 ...
- MDK-ARM输出HEX文件重命名设置
输出的可执行文件和库的名称就是在这里定义.比如我们常见输出Hex文件,其名称就是这里定义的.
- SNF快速开发平台MVC-各种级联绑定方式,演示样例程序(包含表单和表格控件)
做了这么多项目,经常会使用到级联.联动的情况. 如:省.市.县.区.一级分类.二级分类.三级分类.仓库.货位. 方式:有表单需要做级联的,还是表格行上需要做级联操作的. 实现:实现方法也有很多种方式. ...