C# 不添加WEB引用调用WSDL接口
在项目中添加WEB引用耦合度较高,更新时要更新引用,所以我建议不添加WEB引用调用WSDL接口,废话不多说,直接上代码
例如WSDL地址为:http://XXX.XX.XXX.XXX:9115/WsPortal/soap/ws?WSDL 需要调用execute方法 先查看描述文件参数
/// <summary>
/// 需要WebService支持Post调用
/// </summary>
public static XmlDocument QueryPostWebService(String URL, String MethodName, Hashtable Pars)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
SetWebRequest(request);
byte[] data = EncodePars(Pars);
WriteRequestData(request, data);
return ReadXmlResponse(request.GetResponse());
} /// <summary>
/// 需要WebService支持Get调用
/// </summary>
public static XmlDocument QueryGetWebService(String URL, String MethodName, Hashtable Pars)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName + "?" + ParsToString(Pars));
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
SetWebRequest(request);
return ReadXmlResponse(request.GetResponse());
} /// <summary>
/// 通用WebService调用(Soap),参数Pars为String类型的参数名、参数值
/// </summary>
public static XmlDocument QuerySoapWebService(String URL, String MethodName, Hashtable Pars)
{
if (_xmlNamespaces.ContainsKey(URL))
{
return QuerySoapWebService(URL, MethodName, Pars, _xmlNamespaces[URL].ToString());
}
else
{
return QuerySoapWebService(URL, MethodName, Pars, GetNamespace(URL));
}
} private static XmlDocument QuerySoapWebService(String URL, String MethodName, Hashtable Pars, string XmlNs)
{
_xmlNamespaces[URL] = XmlNs;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
request.Method = "POST";
request.ContentType = "text/xml; charset=utf-8";
request.Headers.Add("SOAPAction", "\"" + XmlNs + (XmlNs.EndsWith("/") ? "" : "/") + MethodName + "\"");
SetWebRequest(request);
byte[] data = EncodeParsToSoap(Pars, XmlNs, MethodName);
WriteRequestData(request, data);
XmlDocument doc = new XmlDocument(), doc2 = new XmlDocument();
doc = ReadXmlResponse(request.GetResponse()); XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
String RetXml = doc.SelectSingleNode("//soap:Body/*/*", mgr).InnerXml;
doc2.LoadXml("<root>" + RetXml + "</root>");
AddDelaration(doc2);
return doc2;
}
private static string GetNamespace(String URL)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL + "?WSDL");
SetWebRequest(request);
WebResponse response = request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
XmlDocument doc = new XmlDocument();
doc.LoadXml(sr.ReadToEnd());
sr.Close();
return doc.SelectSingleNode("//@targetNamespace").Value;
} private static byte[] EncodeParsToSoap(Hashtable Pars, String XmlNs, String MethodName)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"></soap:Envelope>");
AddDelaration(doc);
XmlElement soapBody = doc.CreateElement("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
XmlElement soapMethod = doc.CreateElement(MethodName);
soapMethod.SetAttribute("xmlns", XmlNs);
foreach (string k in Pars.Keys)
{
XmlElement soapPar = doc.CreateElement(k);
soapPar.InnerXml = ObjectToSoapXml(Pars[k]);
soapMethod.AppendChild(soapPar);
}
soapBody.AppendChild(soapMethod);
doc.DocumentElement.AppendChild(soapBody);
return Encoding.UTF8.GetBytes(doc.OuterXml);
}
private static string ObjectToSoapXml(object o)
{
XmlSerializer mySerializer = new XmlSerializer(o.GetType());
MemoryStream ms = new MemoryStream();
mySerializer.Serialize(ms, o);
XmlDocument doc = new XmlDocument();
doc.LoadXml(Encoding.UTF8.GetString(ms.ToArray()));
if (doc.DocumentElement != null)
{
return doc.DocumentElement.InnerXml;
}
else
{
return o.ToString();
}
} /// <summary>
/// 设置凭证与超时时间
/// </summary>
/// <param name="request"></param>
private static void SetWebRequest(HttpWebRequest request)
{
request.Credentials = CredentialCache.DefaultCredentials;
request.Timeout = ;
} private static void WriteRequestData(HttpWebRequest request, byte[] data)
{
request.ContentLength = data.Length;
Stream writer = request.GetRequestStream();
writer.Write(data, , data.Length);
writer.Close();
} private static byte[] EncodePars(Hashtable Pars)
{
return Encoding.UTF8.GetBytes(ParsToString(Pars));
} private static String ParsToString(Hashtable Pars)
{
StringBuilder sb = new StringBuilder();
foreach (string k in Pars.Keys)
{
if (sb.Length > )
{
sb.Append("&");
}
}
return sb.ToString();
} private static XmlDocument ReadXmlResponse(WebResponse response)
{
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
String retXml = sr.ReadToEnd();
sr.Close();
XmlDocument doc = new XmlDocument();
doc.LoadXml(retXml);
return doc;
} private static void AddDelaration(XmlDocument doc)
{
XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.InsertBefore(decl, doc.DocumentElement);
} private static Hashtable _xmlNamespaces = new Hashtable();
使用:
//以XML参数类型为例,其他太简单
string xml = request.ToXml("RequestParameter").ToString();
Hashtable ht = new Hashtable();
ht.Add("arg0", xml);
XmlDocument xd =QuerySoapWebService("地址","方法" , ht);
C# 不添加WEB引用调用WSDL接口的更多相关文章
- 添加web引用和添加服务引用有什么区别?
添加web引用和添加服务引用有什么区别,Add Service References 和 Add Web References 有啥区别?参考 http://social.microsoft.com/ ...
- .net 添加web引用和添加服务引用有什么区别?
添加web引用和添加服务引用有什么区别, Add Service References 和 Add Web References 有啥区别? 参考 http://social.microsoft.co ...
- WebService-.Net:添加web引用和添加服务引用有什么区别?
ylbtech-WebService-.Net:添加web引用和添加服务引用有什么区别? 1.返回顶部 1. 添加web引用和添加服务引用有什么区别,Add Service References 和 ...
- (转)添加服务引用和添加Web引用对比
在WindowsForm程序中添加服务引用和Web引用对比 为了验证书上有关Visual Studio 2010添加服务引用和Web引用的区别,进行实验. 一.建立一个Web服务程序项目新建项目,选择 ...
- 添加服务引用和添加Web引用对比
原文:添加服务引用和添加Web引用对比 在WindowsForm程序中添加服务引用和Web引用对比 为了验证书上有关Visual Studio 2010添加服务引用和Web引用的区别,进行实验. 一. ...
- php7-soap调用wsdl接口报错:Could not connect to host
由php5.6升级到php7.1以上版本,在用soap调用wsdl接口是报错:Could not connect to host 后来经过排查是centos服务器上装有2个版本的openssl造成的. ...
- C#.NET调用WSDL接口及方法
1.首先需要清楚WSDL的引用地址 如:http://XX.XX.4.146:8089/axis/services/getfileno?wsdl 上述地址的构造为 类名getfileno. 2.在.N ...
- C# 调用WSDL接口及方法
1.首先需要清楚WSDL的引用地址 如:http://XX.XX.4.146:8089/axis/services/getfileno?wsdl 上述地址的构造为 类名getfileno. 2.在.N ...
- C#/Java 调用WSDL接口及方法
一.C#利用vs里面自带的“添加web引用”功能: 1.首先需要清楚WSDL的引用地址 如:http://www.webxml.com.cn/Webservices/WeatherWebServic ...
随机推荐
- Sort Array By Parity LT905
Given an array A of non-negative integers, return an array consisting of all the even elements of A, ...
- python常用命令
安装sudo easy_install pip 列出已安装的包pip freeze or pip list 导出requirements.txtpip freeze > <目录>/r ...
- mysql练习题3
USE day44; -- 1 查出所有员工的名字,薪资,格式为 -- <名字:egon> <薪资:3000> SELECT '姓名:',name,'薪资:',salary f ...
- FMS4.5( Adobe Flash Media Server4.5)流媒体服务器搭建
下载FMS4.5 下载地址:http://pan.baidu.com/s/1pJLi5Ur(已更新) FMS是用于用户之间相互通讯的新平台.它集成了Flash多媒体交互的特性,又添加了实时音频和实时数 ...
- PHP中require(),include(),require_once()和include_once()有什么区别
引用文件的方法有两种:require 及 include.两种方式提供不同的使用弹性. require 的使用方法如 require("MyRequireFile.php"); . ...
- De Bruijn序列
最近文章中经常出现及De Bruijin 这个关键字,网上搜索了一下,记录下来. De Bruijn序列 (德布鲁因序列) 问题:能否构造一个长度为2的n次方的二进制环状串,使得二进制环状串中总共2的 ...
- java编程IO简单回顾和学习
java编程IO操作必不可少的,很久不玩IO,回顾一下,写了几个小程序,记录一下,方便查阅和学习. 1.给出一个整数数组,将其写入一个文件,再从文件中读出,并按整数大小逆序打印. package co ...
- Check the NativeLink log file
今天用Quartus ii16.1仿真Cyclone IV的IP核DDR2,总是报上面的错误 .网上都说是modelsim路径的问题, 但我确定不是.最后用QaurtusII 12.1可以通道仿真. ...
- git服务器使用
服务器版本:CentOS6.3 root用户密码:123456 服务器地址:192.168.1.125 搭建Git服务器参考:搭建Git服务器 使用git服务器首先要克隆仓库,即添加一个远程仓库,参考 ...
- 【ORA错误大全】 ORA-19527
在做主备切换的时候,需要将备库的联机日志文件清除(clear online redo logfile),为了加快switchover的速度,Oracle10g在将备库置于manged standby状 ...