服务端发送xml请求java代码示例
/**
*
*/
package com.autoyol.pay.cmb.core; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; /**
* @author xxx
* @function
* @date 2016年3月23日
* @version
*/
public class Sender { /**
*
* @param url
* @param postDataXML
* @return
* @throws Exception
*/
public static String sendRQ(String url, String postDataXML) throws Exception{
String result = null;
OutputStream out = null;
InputStream is = null;
ByteArrayOutputStream bos = null;
HttpURLConnection urlc = null;
try {
URL urlacc = new URL(url);
urlc = (HttpURLConnection) urlacc.openConnection();
urlc.setConnectTimeout(100000);
urlc.setReadTimeout(100000);
urlc.setDoOutput(true);
urlc.setDoInput(true);
urlc.setUseCaches(false);
urlc.setInstanceFollowRedirects(false);//是否自动处理重定向
urlc.setRequestMethod("POST"); out = urlc.getOutputStream();
out.write(postDataXML.getBytes());
out.flush();
out.close(); is = urlc.getInputStream();
if(is!=null){
bos = new ByteArrayOutputStream();
byte[] receiveBuffer = new byte[2048];
int readBytesSize = is.read(receiveBuffer);
while(readBytesSize != -1){
bos.write(receiveBuffer, 0, readBytesSize);
readBytesSize = is.read(receiveBuffer);
}
result = new String(bos.toByteArray(), "UTF-8");
// respXml= ParseUtil.parseXML(reqData,transInfo);
//xml解析
System.out.println("result="+result);
}
} catch (Exception e) {
// System.out.println("发送TR1失败");
e.printStackTrace();
// logger.error("sendTR1 Exception2:",e); //sendTR1 Exception2: java.net.NoRouteToHostException: 没有到主机的路由
// throw e; //异常再次抛出,解决sendTR1 Exception2: java.net.ConnectException: 连接超时 网络异常的情况。 160201 huangjing
} finally{
if(bos != null){
bos.close();
}
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out != null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(urlc != null){
urlc.disconnect();
}
}
return result;
} /**
*
* @param url
* @param postDataXML
* @return
* @throws IOException
* @throws KeyStoreException
* @throws UnrecoverableKeyException
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public static String sendPost(String url, String postDataXML) throws IOException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException {
String result = null;
HttpPost httpPost = new HttpPost(url);
System.out.println("API,POST过去的数据是:" + postDataXML);
//得指明使用UTF-8编码,否则到API服务器XML的中文不能被成功识别
StringEntity postEntity = new StringEntity(postDataXML, "UTF-8");
httpPost.addHeader("Content-Type", "text/xml");
httpPost.setEntity(postEntity);
//设置请求器的配置
CloseableHttpClient httpClient = HttpClients.custom().build();
// CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//根据默认超时限制初始化requestConfig
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(30000).build();
httpPost.setConfig(requestConfig);
System.out.println("executing request:" + httpPost.getRequestLine());
try {
HttpResponse response = httpClient.execute(httpPost);
int httpStatusCode = response.getStatusLine().getStatusCode();
if (httpStatusCode == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
System.out.println("result="+result);
}else{
System.err.println("Error,httpStatusCode is '{"+httpStatusCode+"}'");
}
} catch (ConnectionPoolTimeoutException e) {
System.err.println("http get throw ConnectionPoolTimeoutException(wait time out)");
} catch (ConnectTimeoutException e) {
System.err.println("http get throw ConnectTimeoutException");
} catch (SocketTimeoutException e) {
System.err.println("http get throw SocketTimeoutException");
} catch (Exception e) {
System.err.println("http get throw Exception");
} finally {
httpPost.abort();
}
return result;
} }
服务端发送xml请求java代码示例的更多相关文章
- HttpClient服务端发送http请求
本来以为对跨域问题的处理已经比较熟练了.可以通过jsonp.document.domain+iframe.window.name.window.postMessage.服务器上设置代理页面来解决.但还 ...
- jQuery表单 Ajax向PHP服务端发送文件请求并返回数据
ImageAjaxUpLoad.htm <!DOCTYPE html> <head> <meta charset='utf-8'> <title>< ...
- Query通过Ajax向PHP服务端发送请求并返回JSON数据
Query通过Ajax向PHP服务端发送请求并返回JSON数据 服务端PHP读取MYSQL数据,并转换成JSON数据,传递给前端Javascript,并操作JSON数据.本文将通过实例演示了jQuer ...
- jQuery通过Ajax向PHP服务端发送请求并返回JSON数据
SON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写,同时也易于机器解析和生成.JSON在前后台交互的过程中发挥着相当出色的作用.请接着往下看教 ...
- 通过http请求传递xml流和接收xml流的代码示例
通过http请求传递xml流和接收xml流的代码示例 //1.在servlet中post一个xml流:import java.io.OutputStreamWriter;import org.jdom ...
- c# .NET RSA结合AES加密服务端和客户端请求数据
这几天空闲时间就想研究一下加密,环境是web程序,通过js请求后台返回数据,我想做的事js在发送请求前将数据加密,服务端收到后解密,待服务端处理完请求后,将处理结果加密返回给客户端,客户端在解密,于是 ...
- 极光推送JAVA代码示例
一. 准备工作 1. 登录极光推送官网https://www.jpush.cn/,注册账号并登录 2. 创建应用 创建应用过程,详见百度经验:http://jingyan.baidu.com/arti ...
- 利用laravel-echo主动向服务端发送消息,实现在线状态管理
之前在网上翻了半天,也没有找到关于如何 通过laravel-echo主动发送消息 和 在laravel-websockets中自定义控制器 的文章或教程.无奈之下只能翻laravel-echo和lar ...
- Kafka技术内幕 读书笔记之(六) 存储层——服务端处理读写请求、分区与副本
如下图中分区到 日 志的虚线表示 : 业务逻辑层的一个分区对应物理存储层的一个日志 . 消息集到数据文件的虚线表示 : 客户端发送的消息集最终会写入日志分段对应的数据文件,存储到Kafka的消息代理节 ...
随机推荐
- 获取月份对应的day
function getDaysInMonth(month, year) { , , , , , , , , , , , ]; //主要处理二月份的天数 ) && (year % == ...
- IOS学习:在工程中添加百度地图SDK
1.将下载下来的sdk中的inc文件夹.mapapi.bundle.libbaidumapapi.a添加到工程中,其中libbaiduapi.a有两个,一个对应模拟器一个对应真机,导入方法如下: 第一 ...
- Unity3d Shortcuts
参考:http://www.ceeger.com/Manual/ 场景视图导航 Click-drag to drag the camera around. 点击拖拽平移场景视图 Hold Alt a ...
- hdu 2647 Reward
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2647 Reward Description Dandelion's uncle is a boss o ...
- win8.1上安装vc6
win8.1上安装vc6 1.以管理员方式运行SETUP.EXE,然后一路下一步 2.这里需要一点点耐心,等10分钟左右就能过去,电脑会比较卡,有点像假死,还是没有死掉,等等就好了 3.这里选择vc6 ...
- 利用python scrapy 框架抓取豆瓣小组数据
因为最近在找房子在豆瓣小组-上海租房上找,发现搜索困难,于是想利用爬虫将数据抓取. 顺便熟悉一下Python. 这边有scrapy 入门教程出处:http://www.cnblogs.com/txw1 ...
- svn版本控制器在vs2013中的使用
下面记录常用的几种用法: a) SVN检出 将SVN服务端所保存的数据下载到个人工作平台. 组长上传初始项目后,各组员可以到服务器上检出项目 1. 打开Visual Studio 2010-& ...
- 远航1617团队alpha版本分数分配与人员调动
一.根据项目开始初期的分数分配要求及项目发布后大家的讨论,我们对组内成员的分数分配如下: 刘昊岩 20.5 周 萱 20.0 林谋武 19.0 杨 帆 18.5 高小洲 21.0 谢勤政 21.5 ...
- 软件工程随堂小作业——随机四则运算Ⅱ之算法思路(C++)
1.题目避免重复: (1)利用系统时间来产生随机数,重复率会降低. (2)建立链表,逐个判断.可读取写入文件. 2.可定制(数量/打印方式): (1)格式有默认值; (2)可以选择重新设置分几列和每行 ...
- java设计模式类图大全
近来在看书实现GoF的23个设计模式,自己一点点地用建模工具按照自己的理解画出类图(是比较符合我个人思考理解的,个人觉得比通用类图更详细些),碰巧找到了一个挺好用的UML建模工具StarUML,也刚好 ...