使用URL工具类调用webservice接口(soap)与http接口的实现方式
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class SoapTest {
public void testPost(String urlStr,String data) {
try {
//创建指定链接的url对象
URL url = new URL(urlStr);
//建立到url对象之间的链接
HttpURLConnection con = (HttpURLConnection) url.openConnection();
//如果打算使用 URL 连接进行输出,则将 DoOutput 标志设置为 true
con.setDoOutput(true);
//设置第一次请求的数据内容不被存储
con.setRequestProperty("Pragma:", "no-cache");
//设置请求的数据内容不被存储
con.setRequestProperty("Cache-Control", "no-cache");
//设置请求的字符集编码格式,如果不指定,则默认采用16进制的字符集编码格式
con.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
//构造向指定链接写入数据的的输出流
OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
//获取xml数据
String xmlInfo = getXmlInfo(data);
//向指定链接写入数据
out.write(new String(xmlInfo));
out.flush();
out.close();
//将从服务端返回的数据读取到内存中
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = "";
//构造一个空的StringBuffer对象,用于存储内存中的数据
StringBuffer buf = new StringBuffer();
for (line = br.readLine(); line != null; line = br.readLine()) {
//由于服务端返回的数据的字符集编码有可能不是utf-8,需要对返回的数据通过指定的字符集进行解码
buf.append(new String(line.getBytes(),"UTF-8"));
}
//获取服务端返回的HttpCode
int httpCode = con.getResponseCode();
System.out.println("HttpCode:"+httpCode+" "+map.get("msgType")+"接口:"+buf.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//处理xml数据
public static String getXmlInfo(String data){
//对webservice接口进行加密(SHA-1加密)
SoapKey soapKey = new SoapKey();
String key=soapKey.getMessageDigest(data, "SHA-1");
//处理传输数据的"<"与">"字符
String str = new String(data.replace("<","<")).replace(">",">");
String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.ws.gpo.yy.com/\">"+"<soapenv:Header/>"
+"<soapenv:Body><ser:sendRecv4Supplier><!--Optional:--><sUser>zhuwei</sUser><!--Optional:--><sPwd>password</sPwd><!--Optional:--><sJgbm>GYSNB001</sJgbm><!--Optional:--><sVersion>1.0.0.0</sVersion><!--Optional:--><sXxlx>GS102</sXxlx><!--Optional:--><sSign>"+key+"</sSign><!--Optional:-->"
+"<xmlData>"+str+"</xmlData>"
+"</ser:sendRecv4Supplier></soapenv:Body></soapenv:Envelope>";
return xml;
}
public static void main(String[] args) throws UnsupportedEncodingException {
String url = "http://192.168.1.208:8090/gpodec/webservice/supplierService";
new SoapTest().testPost(url);
}
}
使用URL工具类调用webservice接口(soap)与http接口的实现方式的更多相关文章
- UrlUtils工具类,Java URL工具类,Java URL链接工具类
UrlUtils工具类,Java URL工具类,Java URL链接工具类 >>>>>>>>>>>>>>>&g ...
- URL工具类
UrlUtils = { /** * 判断url是否存在(存在跨域问题) * @param {String} url */ isTrueUrl: function(_url) { result = f ...
- .Net Excel 导出图表Demo(柱状图,多标签页) .net工具类 分享一个简单的随机分红包的实现方式
.Net Excel 导出图表Demo(柱状图,多标签页) 1 使用插件名称Epplus,多个Sheet页数据应用,Demo为柱状图(Epplus支持多种图表) 2 Epplus 的安装和引用 新建一 ...
- Java使用OkHttps工具类调用外部接口
前言 现在公司业务已止不是传统的增删改查等简单的业务处理操作,而是对数据各种联调三方接口与其他系统进行交互等,那么就需要我们在后台java中进行外部接口的调用,本文采用OkHttps工具类对接微信接口 ...
- android 使用Ksoap2工具类实现WebService网络编程
1.下载Ksoap2,将jar包拷贝到libs目录下.然后右键点击拷贝进来的jar,在弹出菜单中点击Add As Library. 2.在AndroidManifest.xml中添加访问网络的权限 & ...
- Java工具类之——BigDecimal运算封装(包含金额的计算方式)
日常对于金额计算,应该都是用的BigDecimal, 可是苦于没有好的工具类方法,现在贡献一个我正在用的对于数字计算的工具类,项目中就是用的这个,简单粗暴好用,话不多说,代码奉上(该工具类需要引入g ...
- 使用HttpClient工具类测试WebService接口(soap)
import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import jav ...
- ssh框架,工具类调用service层方法
解决方法: @Component//声明为spring组件 public class CopyFileUtil{ @Autowired private DataFileManager dataFile ...
- PHP调用webservice遇到 Soap WSDL Error - "failed to load external entity
本人遇到的原因是服务器上没有安装php-soap centos安装方式: yum install php-soap 安装完成后重启 php-fpm 服务 service php-fpm restart ...
随机推荐
- json_encode转成带 花括号的{ } 和 中括号的[ ] 2种 形式 json数据
//提交多个出差人员 .命名为数组 name="apply_members[] " //php接收到数据 为 数组 $aa = array('0'=>11,'1'=> ...
- ATL opengl
#include <atlbase.h> #include <atlwin.h> #include <gl/glew.h> #include <gl/GL.h ...
- OpenCL( 一)
#include <CL/cl.h> #include <iostream> #include <string> #include <fstream> ...
- HDU 1017 A Mathematical Curiosity(枚举)
题目链接 Problem Description Given two integers n and m, count the number of pairs of integers (a,b) suc ...
- [kuangbin带你飞]专题四 最短路练习 POJ 1797 Heavy Transportation
求每条道路的最大承载量 和上一道题差不多 就是松弛的规则从最大值变成了最小值 /* *********************************************** Author :Su ...
- laravel常用拓展库
1.laravel-dompdf:pdf生成器 git地址:https://github.com/barryvdh/laravel-dompdf 2.
- IE8"开发人员工具"使用详解下(浏览器模式、文本模式、JavaScript调试、探查器)
来源: http://www.cnblogs.com/JustinYoung/archive/2009/04/03/kaifarenyuangongju2.html 在上一篇文章IE8“开发人员工具” ...
- Windows下QT4.8.4编译环境的搭建(转载http://blog.csdn.net/bestgonghuibin/article/details/38933141)
开始使用QT了,所以第一步就是把环境搭起来,这里小记一下,以免以后忘记. 1. 下载安装文件 要使用QT功能,那么必须要下载QT的源码,还必须要一个是用QT的编译环境,可以是VS2010,也可以是专用 ...
- Android获取网络数据进行GZIP解压
说明:现在很多网站都会在回传数据的时候进行GZIP压缩,我们可以在请求头中申明支持GZIP压缩.可以减轻网络传输压力,Xutils中已经实现. 下面是一个DEMO,便于理解. private void ...
- Apache+Tomcat服务器集群配置
在实际应用中,如果网站的访问量很大,为了提高访问速度,可以与多个Tomcat服务器与Apache服务器集成,让他们共同运行servlet/jsp 组件的任务,多个Tomcat服务器构成了一个集群(Cl ...