import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
public class WebService {
private String responseInfo;
private InputStream responseInputStream;
// 命名空间
private String namespace;
// 超时设置
private int timeOut;
/**
* @param SOAPAction 等于url+方法
* @param soapNameSpace 等于url 如果<soap:body>的子元素中包含xmlns=""
* 属性则可以为空 例如soapNameSpace值为xmlns="http://WebXml.com.cn/"
* 例如http://WebXml.com.cn/qqCheckOnline
* http://WebXml.com.cn=URL /qqCheckOnline=Method
* @param serviceURL post提交地址
* @param SoapHeaderXml <soap:head>标签子节点 内容为xml元素 为空时将不添加请求头
* @param SaopBodyXmlString <soap:Body>标签子节点 xml元素
* 例如 <qqCheckOnline xmlns="http://WebXml.com.cn/"><qqCode>2248263994</qqCode></qqCheckOnline>
* 完整代码示例:
* WebService webService=new WebService();
String bodyInfo="<qqCheckOnline xmlns=\"http://WebXml.com.cn/\"><qqCode>2248263994</qqCode></qqCheckOnline>";
webService.requestSoap("http://WebXml.com.cn/qqCheckOnline",null, "http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx",null,bodyInfo);
String responseJsonString=webService.responseString();
返回值:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<qqCheckOnlineResponse xmlns="http://WebXml.com.cn/">
<qqCheckOnlineResult>Y</qqCheckOnlineResult>
</qqCheckOnlineResponse>
</soap:Body>
</soap:Envelope>
* @throws Exception
*/
public void requestSoap(String SOAPAction,String soapNameSpace,String serviceURL,String SoapHeaderXml,String SaopBodyXmlString) throws Exception{
this.namespace=soapNameSpace;
URLHelp urlHelp=new URLHelp();
urlHelp.setTimeOut(timeOut);
urlHelp.setUrl(serviceURL);
try {
createSoapEnvelope();
if(SoapHeaderXml!=null){
createSoapHeader(SoapHeaderXml);
}
createSoapBody(SaopBodyXmlString);
String param="<?xml version=\"1.0\" encoding=\"utf-8\"?>"+XmlUtil.parseXml(soap_Envelope);
urlHelp.setParams(param);
responseInfo=urlHelp.soap(SOAPAction);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* soap返回读取数值获得流的操作,与responseInputStream联用.
* */
public void requestSoapGetInputStream(String SOAPAction,String soapNameSpace,String serviceURL,String SoapHeaderXml,String SaopBodyXmlString) throws Exception{
this.namespace=soapNameSpace;
URLHelp urlHelp=new URLHelp();
urlHelp.setTimeOut(timeOut);
urlHelp.setUrl(serviceURL);
try {
createSoapEnvelope();
if(SoapHeaderXml!=null){
createSoapHeader(SoapHeaderXml);
}
createSoapBody(SaopBodyXmlString);
String param="<?xml version=\"1.0\" encoding=\"utf-8\"?>"+XmlUtil.parseXml(soap_Envelope);
urlHelp.setParams(param);
responseInputStream=urlHelp.soapGetInputStream(SOAPAction);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param SOAPAction 等于url+方法
* @param soapNameSpace 等于url 如果<soap:body>的子元素中包含xmlns=""
* 属性则可以为空 例如 xmlns="http://WebXml.com.cn/"
* 例如http://WebXml.com.cn/qqCheckOnline
* http://WebXml.com.cn=URL /qqCheckOnline=Method
* @param serviceURL post提交地址
* @param SoapHeaderXml[] <soap:head>标签子节点 内容为xml元素 soap:Header标签属性请浏览官网
* @param SaopBodyXmlString[] <soap:Body>标签子节点 xml元素
* 例如 <qqCheckOnline xmlns="http://WebXml.com.cn/"><qqCode>2248263994</qqCode></qqCheckOnline>
* */
public void requestSoap(String SOAPAction, String soapNameSpace, String serviceURL
, String[] SoapHeaderXml,String[] SaopBodyXmlString) throws Exception{
this.namespace=soapNameSpace;
URLHelp urlHelp=new URLHelp();
urlHelp.setTimeOut(timeOut);
urlHelp.setUrl(serviceURL);
try {
createSoapEnvelope();
if(SoapHeaderXml!=null){
for (String headerXml : SoapHeaderXml) {
createSoapHeader(headerXml);
}
}
if(SaopBodyXmlString!=null){
for (String BodyXml : SaopBodyXmlString) {
createSoapBody(BodyXml);
}
}
String param="<?xml version=\"1.0\" encoding=\"utf-8\"?>"+XmlUtil.parseXml(soap_Envelope);
urlHelp.setParams(param);
responseInfo=urlHelp.soap(SOAPAction);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 通过SOAP/http请求返回的值,字符串类型。值为:标准xml内容
* */
public String responseString(){
return responseInfo;
}
/**
* 通过SOAP/http请求返回的值,字符串类型。值为:标准xml内容
* */
public InputStream responseInputStream(){
return responseInputStream;
}
/**
* 通过SOAP请求返回的值,转换为XmlNode。Xml节点模型
* */
public XmlNode responseSoapXmlNode() throws ParserConfigurationException
, SAXException, IOException{
return XmlUtil.parseNode(responseInfo);
}
/**
* 通过SOAP请求返回的值,值为标准json格式字符串。
* */
public String responseSoapJsonString() throws ParserConfigurationException
, SAXException, IOException{
return XmlUtil.toJson(XmlUtil.parseNode(responseInfo));
}
/**
* http请求
* @param url 连接地址
* @param params 参数数组。例如[name="张三",age="18"]
*
* */
public void request(String url,String...params) throws IOException{
URLHelp urlHelp=new URLHelp();
urlHelp.setUrl(url);
urlHelp.setParams(params);
responseInfo=urlHelp.doPost();
}
/**请求超时设置**/
public void setTimeOut(int timeOut){
this.timeOut=timeOut;
}
/**
* http请求
* @param url 连接地址
* @param param 参数请拼接成get入参方式
* */
public void request(String url,String param) throws IOException{
URLHelp urlHelp=new URLHelp();
urlHelp.setUrl(url);
urlHelp.setParams(param);
responseInfo=urlHelp.doPost();
}
private XmlNode soap_Envelope;
/**
* 创建soap:Envelope标签
* */
private void createSoapEnvelope(){
// saop的根节点
if(soap_Envelope==null){
soap_Envelope=new XmlNode();
soap_Envelope.setChild(new ArrayList<XmlNode>());
soap_Envelope.setAttribute(new HashMap<String, String>());
soap_Envelope.setQname("soap:Envelope");
}
// 必要属性
Map<String, String> map=mapIsNull(soap_Envelope.getAttribute());
map.put("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
map.put("xmlns:soap", "http://schemas.xmlsoap.org/soap/envelope/");
map.put("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
}
private XmlNode soap_Heander;
/**
* 创建soap:Header标签
* @param soap_Header_Child
* 为空时或者不调用时不创建
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
* */
private void createSoapHeader(String soap_Header_Child)
throws ParserConfigurationException, SAXException, IOException{
// saop的头部
if(soap_Heander==null){
soap_Heander=new XmlNode();
soap_Heander.setAttribute(new HashMap<String, String>());
soap_Heander.setChild(new ArrayList<XmlNode>());
// saop根节点的子节点Header
List<XmlNode> soap_Envelope_Header=listIsNull(soap_Envelope.getChild());
soap_Envelope_Header.add(soap_Heander);
soap_Envelope.setChild(soap_Envelope_Header);
soap_Heander.setQname("soap:Header");
}
// header头部包括的内容
if(soap_Header_Child!=null){
List<XmlNode> soap_Header= new ArrayList<XmlNode>();
soap_Header.add(XmlUtil.parseNode(soap_Header_Child));
soap_Heander.setChild(soap_Header);
}
}
private XmlNode soap_Body;
/**
* @param SaopBodyXmlString为xml字符串
* 创建soap:body标签
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
* */
private void createSoapBody(String SaopBodyXmlString)
throws ParserConfigurationException, SAXException, IOException{
// saop的body
if(soap_Body==null){
soap_Body=new XmlNode();
soap_Body.setAttribute(new HashMap<String, String>());
soap_Body.setChild(new ArrayList<XmlNode>());
List<XmlNode> soap_Envelope_Body=listIsNull(soap_Envelope.getChild());
soap_Envelope_Body.add(soap_Body);
soap_Body.setQname("soap:Body");
Map<String, String> map=mapIsNull(soap_Body.getAttribute());
if(namespace!=null){
String[] importNamespace=this.namespace.split("=");
map.put(importNamespace[0], importNamespace[1]);
}
}
soap_Body.getChild().add(XmlUtil.parseNode(SaopBodyXmlString));
}
// 判断map是否为空
private Map<String,String> mapIsNull(Map<String,String> checkMap){
if(checkMap==null){
checkMap=new HashMap<String, String>();
}
return checkMap;
}
// 判断list是否为空
private List<XmlNode> listIsNull(List<XmlNode> checkList){
if(checkList==null){
checkList=new ArrayList<XmlNode>();
}
return checkList;
}
}