• c#动态调用Webservices


    方式一:

    Hashtable ht = new Hashtable();
                ht.Add("a", "testhelloworld");
                XmlDocument xx = WebServicesHelper.QuerySoapWebService("http://e-learning.huawei.com/elab_mgmt/WorkflowSchemeTaskSerivce.asmx", "ATesting", ht);
                string ss = xx.OuterXml;
    /// <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;
            }

    方式二:

    通过SOAPUI直接取URL

    string postData2="<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"><soapenv:Header/><soapenv:Body><tem:ATesting><!--Optional:--><tem:a>?</tem:a></tem:ATesting></soapenv:Body></soapenv:Envelope>";
                HttpHelper.GetResponseFormUrlAsync("http://www.xxx.com/testingservices.asmx?wsdl", postData2, "text/xml", true, new AsyncCallback(responseCallback));
    }
            static void responseCallback(IAsyncResult ar)
            {
                HttpWebRequest req = ar.AsyncState as HttpWebRequest;
                if (req == null)
                    return;
                try
                {
                    HttpWebResponse response = req.EndGetResponse(ar) as HttpWebResponse;
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        response.Close();
                        LogHelper.Error("定时任务", "异步执行失败," + req.RequestUri.ToString() + "
    Response状态代码为
    " + response.StatusCode.ToString());
                        return;
                    }
                    Stream responseStream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                    string ResponseStr = reader.ReadToEnd();
                    responseStream.Close();
                    LogHelper.Warn("定时任务", req.RequestUri.ToString() + "
    " + ResponseStr);
                }
                catch (Exception e)
                {
                    LogHelper.Fatal("定时任务", req.RequestUri.ToString() + "
    执行失败", e);
                }
            }
    View Code
  • 相关阅读:
    开发思路总结
    电脑蓝屏代码丢失,Eclipse这个功能帮我找回了代码
    书籍
    向日葵无法在控制端用键盘输入的原因
    js 多submit 不使其执行其他submit form
    js获取url参数值
    C# 正则表达式 去除HTML标签 C#后台
    读取txt文件 循环操作每行数据 添加到数据库
    Ubuntu中安装XAMPP出错的解决方法(转发,备查)
    常用正则表达式
  • 原文地址:https://www.cnblogs.com/8090sns/p/3282562.html
Copyright © 2020-2023  润新知