using System;using System.Collections;using System.IO;using System.Net;using System.Net.Http;using System.Text;using System.Text.RegularExpressions;using System.Xml;using System.Xml.Serialization;namespace ConsoleApp1{ class Program { static void Main(string[] args) { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); //HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.baidu.com/"); //request.Method = "GET"; //request.ContentType = "text/xml; charset=utf-8"; //request.Accept = "text/html, application/xhtml xml, */*"; //var resp = (HttpWebResponse)request.GetResponseAsync().Result; //StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.UTF8); //String retXml = sr.ReadToEnd(); Hashtable ht = new Hashtable(); ht.Add("theCityName", "北京"); bool flag = true; while (flag) { Console.WriteLine("请输入1或2"); var str = Console.ReadLine(); if (str == "1") { try { var datav1 = HttpHelper.SoapV1_1WebService("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx ", "getWeatherbyCityName", ht, "http://WebXml.com.cn/"); Console.WriteLine(datav1.Substring(0,10)); } catch (Exception ex) { Console.WriteLine("v1异常了"); } } else if (str == "2") { try { var datav2 = HttpHelper.SoapV1_2WebService("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx ", "getWeatherbyCityName", ht, "http://WebXml.com.cn/"); Console.WriteLine(datav2.Substring(0, 15)); } catch (Exception ex) { Console.WriteLine("v2异常了"); } } else { flag = false; } } //var datav2 = HttpHelper.SoapV1_2WebService("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx", "getWeatherbyCityName", ht, "http://WebXml.com.cn/"); } } public class HttpHelper { public static string SoapV1_1WebService(String URL, String MethodName, Hashtable Pars, string XmlNs) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL); request.Method = "POST"; request.ContentType = "text/xml; charset=utf-8"; request.Headers["SOAPAction"] = "\"" XmlNs (XmlNs.EndsWith("/") ? "" : "/") MethodName "\""; request.Headers["User-Agent"] = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"; // 凭证 //request.Credentials = CredentialCache.DefaultCredentials; //超时时间 request.ContinueTimeout = 10000; byte[] data = HashtableToSoap(Pars, XmlNs, MethodName); Stream writer = request.GetRequestStreamAsync().Result; writer.Write(data, 0, data.Length); writer.Dispose(); var response = (HttpWebResponse)request.GetResponseAsync().Result; XmlDocument doc = new XmlDocument(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); String retXml = sr.ReadToEnd(); sr.Dispose(); doc.LoadXml(retXml); return doc.InnerText; } private static string ObjectToSoapXml(object o) { //XmlSerializer mySerializer = new XmlSerializer(o.GetType()); //MemoryStream ms = new MemoryStream(); //mySerializer.Serialize(ms, o); //XmlDocument doc = new XmlDocument(); //var xml = Encoding.UTF8.GetString(ms.ToArray()); //StringBuilder sbXml = new StringBuilder(); //sbXml.AppendLine("<?xml version=\"1.0\" encoding=\"utf - 8\"?>"); //var tempArr = xml.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); //for (int i = 1; i <tempArr.Length; i ) //{ // sbXml.AppendLine(tempArr[i]); //} //doc.LoadXml(sbXml.ToString()); XmlSerializer mySerializer = new XmlSerializer(o.GetType()); MemoryStream ms = new MemoryStream(); var streamWriter = new StreamWriter(ms, System.Text.Encoding.UTF8); mySerializer.Serialize(streamWriter, o); XmlDocument doc = new XmlDocument(); ms.Seek(0, SeekOrigin.Begin); var streamReader = new StreamReader(ms, System.Text.Encoding.UTF8); var xml = streamReader.ReadToEnd(); doc.LoadXml(xml); if (doc.DocumentElement != null) { return doc.DocumentElement.InnerXml; } else { return o.ToString(); } } private static byte[] HashtableToSoap(Hashtable ht, 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>"); XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "utf-8", null); doc.InsertBefore(decl, doc.DocumentElement); 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 ht.Keys) { XmlElement soapPar = doc.CreateElement(k); soapPar.InnerXml = ObjectToSoapXml(ht[k]); soapMethod.AppendChild(soapPar); } soapBody.AppendChild(soapMethod); doc.DocumentElement.AppendChild(soapBody); return Encoding.UTF8.GetBytes(doc.OuterXml); } public static string SoapV1_2WebService(String URL, String MethodName, Hashtable Pars, string XmlNs) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL); request.Method = "POST"; request.ContentType = "application/soap xml; charset=utf-8"; request.Headers["User-Agent"] = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"; // 凭证 request.Credentials = CredentialCache.DefaultCredentials; //超时时间 request.ContinueTimeout = 10000; byte[] data = HashtableToSoap12(Pars, XmlNs, MethodName); Stream writer = request.GetRequestStreamAsync().Result; writer.Write(data, 0, data.Length); writer.Dispose(); var response = (HttpWebResponse)request.GetResponseAsync().Result; XmlDocument doc = new XmlDocument(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); String retXml = sr.ReadToEnd(); sr.Dispose(); doc.LoadXml(retXml); return doc.InnerText; } private static byte[] HashtableToSoap12(Hashtable ht, String XmlNs, String MethodName) { XmlDocument doc = new XmlDocument(); doc.LoadXml("<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"></soap12:Envelope>"); XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "utf-8", null); doc.InsertBefore(decl, doc.DocumentElement); XmlElement soapBody = doc.CreateElement("soap12", "Body", "http://www.w3.org/2003/05/soap-envelope"); XmlElement soapMethod = doc.CreateElement(MethodName); soapMethod.SetAttribute("xmlns", XmlNs); foreach (string k in ht.Keys) { XmlElement soapPar = doc.CreateElement(k); soapPar.InnerXml = ObjectToSoapXml(ht[k]); soapMethod.AppendChild(soapPar); } soapBody.AppendChild(soapMethod); doc.DocumentElement.AppendChild(soapBody); return Encoding.UTF8.GetBytes(doc.OuterXml); } }}
下载Net Core调用WebService示例Demo用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论