http://www.mareenoire.info/u012534831/article/details/51357111
前言:
目前我们项目组还在采用webservice这种http方式,并且某些网站服务提供的对外接口还在采用webservice方式,因此就总结了一下写了这篇文章。
以soap1.2的请求为例,在例子里我们传进去用户名和密码给服务,服务返回一个xml数据。
首先我们来开一下soap1.2的request,
//wsdl,例:OrderApp.asmx
POST /******App.asmx HTTP/1.1
//这儿填写服务地址
Host: 100.100.100.100
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
string
string
POST /******App.asmx HTTP/1.1
//这儿填写服务地址
Host: 100.100.100.100
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
接下来,我们在代码里拼接请求体:
/**
* arg1为第一个参数键,arg2为第一个参数值,arg3为第二个参数键,arg4为第二个参数值,
*method为方法名,xmlns为命名空间 */
public void initSoap(String arg1,String arg2,String arg3,String arg4,String method,String xmlns) {
String soapRequestData = ""
+ "
+ " xmlns:xsd=\"http://www.mareenoire.info/2001/XMLSchema\""
+ " xmlns:soap12=\"http://www.mareenoire.info/2003/05/soap-envelope\">"
+ ""
+ "<"+method+""+"xmlns="+"\""+xmlns+"\""
// + ""
+ "<"+arg1+">"+arg2+""+arg1+">"
+ "<"+arg3+">"+arg4+""+arg3+">"
// + ""+"YQPIS0670"+" "
// + ""+"YQPIS0670"+" "
+ " "
+ " "
+ " ";
}
* arg1为第一个参数键,arg2为第一个参数值,arg3为第二个参数键,arg4为第二个参数值,
*method为方法名,xmlns为命名空间 */
public void initSoap(String arg1,String arg2,String arg3,String arg4,String method,String xmlns) {
String soapRequestData = ""
+ "
+ " xmlns:soap12=\"http://www.mareenoire.info/2003/05/soap-envelope\">"
+ "
+ "<"+method+""+"xmlns="+"\""+xmlns+"\""
// + "
+ "<"+arg1+">"+arg2+""+arg1+">"
+ "<"+arg3+">"+arg4+""+arg3+">"
// + "
// + "
+ "
+ "
+ " ";
}
第二步,开启线程并执行访问
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
PostMethod postMethod = new PostMethod(
"服务地址,即上面request中的host+端口号+post");
// 然后把Soap请求数据添加到PostMethod中
byte[] b = null;
try {
b = soapRequestData.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream is = new ByteArrayInputStream(b, , b.length);
RequestEntity re = new InputStreamRequestEntity(is,
b.length, "application/soap+xml; charset=utf-8");
postMethod.setRequestEntity(re);
// 最后生成一个HttpClient对象,并发出postMethod请求
HttpClient httpClient = new HttpClient();
try {
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode == ) {
Log.d("soapRequestData", "调用成功!");
StringBuffer buffer = new StringBuffer();
// 解析器 工厂类
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
//返回流式数据
InputStream soapResponseData = postMethod
.getResponseBodyAsStream();
Document dm = db.parse(soapResponseData);
// element和node是同一概念
// 不同的是element提供更多方法
if (dm.getElementsByTagName("Root").item()
.getFirstChild() != null) {
// j是Root即根节点下面节点个数
for (int j = ; j < dm .getElementsByTagName("Root").item()
.getChildNodes().getLength(); j++) {
String result3 = dm.getElementsByTagName("Root") .item().getChildNodes().item(j).getTextContent();
buffer.append(result3);
}
}
} else {
Log.d("soapRequestData", "调用失败!错误码:" + statusCode);
}
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
1
登录成功
YQPIS0670
祁宏涛
2010-11-04
...
当前会员
友情会籍
12ee640d-a037-497e-966e-91fc2186c8b4
175f0624-29d1-4b88-9d97-d72ebb1e6a1c
男
中国
@Override
public void run() {
// TODO Auto-generated method stub
PostMethod postMethod = new PostMethod(
"服务地址,即上面request中的host+端口号+post");
// 然后把Soap请求数据添加到PostMethod中
byte[] b = null;
try {
b = soapRequestData.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream is = new ByteArrayInputStream(b, , b.length);
RequestEntity re = new InputStreamRequestEntity(is,
b.length, "application/soap+xml; charset=utf-8");
postMethod.setRequestEntity(re);
// 最后生成一个HttpClient对象,并发出postMethod请求
HttpClient httpClient = new HttpClient();
try {
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode == ) {
Log.d("soapRequestData", "调用成功!");
StringBuffer buffer = new StringBuffer();
// 解析器 工厂类
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
//返回流式数据
InputStream soapResponseData = postMethod
.getResponseBodyAsStream();
Document dm = db.parse(soapResponseData);
// element和node是同一概念
// 不同的是element提供更多方法
if (dm.getElementsByTagName("Root").item()
.getFirstChild() != null) {
// j是Root即根节点下面节点个数
for (int j = ; j < dm .getElementsByTagName("Root").item()
.getChildNodes().getLength(); j++) {
String result3 = dm.getElementsByTagName("Root") .item().getChildNodes().item(j).getTextContent();
buffer.append(result3);
}
}
} else {
Log.d("soapRequestData", "调用失败!错误码:" + statusCode);
}
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
大家可以看下我的xml数据:
如果有多层节点,可以自己修改dm.getElementsByTagName(“Root”) .item(0).getChildNodes().item(j).getTextContent()
为dm.getElementsByTagName(“Root”) .item(0).getChildNodes().item(j).getChildNodes().item(k).getTextContent();即为3层节点的属性值。
相关推荐
- 如何在线更改营业地址-公司地址变
- 广州公司地址变更办理费(营业执照
- arduino开发esp32教程
- 为什么vs2010安装qt后不显
- Photoshop将古典人物转变
- c vs2010教程pdf_vs
- 为什么Windows 10慢速版
- Windows 7个性化配置,关
- 数据采集程序通用架构(C++
- 张杰个人信息介绍,张杰个人信息
- 麦克米尼是什么?
- 接入internet的电脑必须装
- stm32防止程序被读取(stm
- 如何在vs2010中恢复项目配置
- stm32flash读保护(st
- mysql中double wri
- Python Monkey pa
- Applescript 将打开的
- applescript 元素、类
- 诚邀参与 |“谁在重构产业链”产
- 注册分公司要多少钱——自己注册公
- 如何合法避税-合理避税的意见和建
- 公司如何合法避税-合理避税的方法
- usb转rs232串口接线检测标
- stm32烧录软件下载(stm3
- 学习不好能成为黑客吗?
- 山东德州网络安全知识
- 黑客加持代码学习
- 海外VPS主机哪家最好?如何选择
- 趋势之上,SD-WAN市场机遇即
Copyright© 黑海资讯