關於C#調用WebServices的方法

關於C#調用WebServices的方法

https://www.cnblogs.com/CreateFree/p/8423315.html

 

第一種:最常見的就是直接在項目的引用中右擊添加服務引用

此方法用法如下:

首先需要添加引用;然後需要實例化,實例化完了之後就可以通過實例化的對象調用webservices的方法;

 

這個方法以前常用,可惜這次不行了;

第二種:不用通過引用,直接調用webservices

此方法又分爲了幾種:

https://www.cnblogs.com/weicanpeng/p/5755987.html  這是原網址。

(1)使用HttpWebRequest 向WebService發送POST請求,並將請求頭:ContentType = "application/json;charset=utf-8",參數以JSON方式發送給WebService

複製代碼

複製代碼

        /// <summary>
        /// 需要WebService支持Post調用
        /// </summary>
        public static string PostWebServiceByJson(String URL, String MethodName, Hashtable Pars)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName);
            request.Method = "POST";
            request.ContentType = "application/json;charset=utf-8";
            request.Credentials = CredentialCache.DefaultCredentials;
            request.Timeout = 10000;
            byte[] data = Encoding.UTF8.GetBytes(HashtableToJson(Pars));
            request.ContentLength = data.Length;
            Stream writer = request.GetRequestStream();
            writer.Write(data, 0, data.Length);
            writer.Close();
            
            StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.UTF8);
            String retXml = sr.ReadToEnd();
            sr.Close();
            return retXml;
        }

複製代碼

複製代碼

調用方法如下:

複製代碼

複製代碼

            Hashtable ht = new Hashtable();
            ht.Add("LoginName", "Admin");
            ht.Add("Password", "Password");
            ht.Add("AppKey", "123");

            HttpHelper.PostWebServiceByJson("http://localhost/OpenApi/MobileService.asmx", "Login", ht);

複製代碼

複製代碼

注意以上調用WebService方法,需要在WebService中將以下代碼中的[System.Web.Script.Services.ScriptService]註釋去掉

 // 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消註釋以下行。 
  [System.Web.Script.Services.ScriptService]

 

 (2)根據WebService文檔對應方法說明(例如參數提交方式,請求)發送POST或Get請求

SOAP 1.1

以下是 SOAP 1.2 請求和響應示例。所顯示的佔位符需替換爲實際值。

POST /OpenApi/MobileService.asmx HTTP/1.1
Host: gps.szwearable.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/Login"

<?xml version="1.0" encoding="utf-8"?>
<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:Body>
    <Login xmlns="http://tempuri.org/">
      <LoginName>string</LoginName>
      <Password>string</Password>
      <AppKey>string</AppKey>
    </Login>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<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:Body>
    <LoginResponse xmlns="http://tempuri.org/">
      <LoginResult>boolean</LoginResult>
    </LoginResponse>
  </soap:Body>
</soap:Envelope>

 以上Soap1.1示例相應代碼如下

複製代碼

複製代碼

 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.Add("SOAPAction", "\"" + XmlNs + (XmlNs.EndsWith("/") ? "" : "/") + MethodName + "\"");
            // 憑證
            request.Credentials = CredentialCache.DefaultCredentials;
            //超時時間
            request.Timeout = 10000;
            byte[] data = HashtableToSoap(Pars, XmlNs, MethodName);
            request.ContentLength = data.Length;
            Stream writer = request.GetRequestStream();
            writer.Write(data, 0, data.Length);
            writer.Close();
            var response = request.GetResponse();
            XmlDocument doc = new XmlDocument();
            StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            String retXml = sr.ReadToEnd();
            sr.Close();
            doc.LoadXml(retXml);
            XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
            mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
            String xmlStr = doc.SelectSingleNode("//soap:Body/*/*", mgr).InnerXml;
           
            return xmlStr;
        }
        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();
            }
        }
        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);
        }

複製代碼

複製代碼

 調用方法如下

複製代碼

複製代碼

  Hashtable ht = new Hashtable();
            ht.Add("LoginName", "Admin");
            ht.Add("Password", "Password");
            ht.Add("AppKey", "123");

          
 var data = HttpHelper.SoapV1_1WebService("http://localhost/OpenApi/MobileService.asmx", "Login", ht, "http://tempuri.org/");

複製代碼

複製代碼

我是用SOAP1.1這個方法成功調用的;

此方法可以多加使用,當然有更好的方法時可以考慮使用更好的方法。

SOAP 1.2

以下是 SOAP 1.2 請求和響應示例。所顯示的佔位符需替換爲實際值。

POST /OpenApi/MobileService.asmx HTTP/1.1
Host: gps.szwearable.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<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:Body>
    <Login xmlns="http://tempuri.org/">
      <LoginName>string</LoginName>
      <Password>string</Password>
      <AppKey>string</AppKey>
    </Login>
  </soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<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:Body>
    <LoginResponse xmlns="http://tempuri.org/">
      <LoginResult>boolean</LoginResult>
    </LoginResponse>
  </soap12:Body>
</soap12:Envelope>
以上Soap1.2示例相應代碼如下

 

複製代碼

複製代碼

        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.Credentials = CredentialCache.DefaultCredentials;
            //超時時間
            request.Timeout = 10000;
            byte[] data = HashtableToSoap12(Pars, XmlNs, MethodName);
            request.ContentLength = data.Length;
            Stream writer = request.GetRequestStream();
            writer.Write(data, 0, data.Length);
            writer.Close();
            var response = request.GetResponse();
            XmlDocument doc = new XmlDocument();
            StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            String retXml = sr.ReadToEnd();
            sr.Close();
            doc.LoadXml(retXml);
            XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
            mgr.AddNamespace("soap12", "http://www.w3.org/2003/05/soap-envelope");
            String xmlStr = doc.SelectSingleNode("//soap12:Body/*/*", mgr).InnerXml;

            return xmlStr;
        }

        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);
        }
        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();
            }
        }

複製代碼

複製代碼

 調用方法如下

複製代碼

複製代碼

       Hashtable ht = new Hashtable();
            ht.Add("LoginName", "Admin");
            ht.Add("Password", "Password");
            ht.Add("AppKey", "123");
       var data = HttpHelper.SoapV1_2WebService("http://localhost/OpenApi/MobileService.asmx", "Login", ht, "http://tempuri.org/");

複製代碼

複製代碼

 

HTTP GET

以下是 HTTP GET 請求和響應示例。所顯示的佔位符需替換爲實際值。

GET /OpenApi/MobileService.asmx/Login?LoginName=string&Password=string&AppKey=string HTTP/1.1
Host: gps.szwearable.com
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<boolean xmlns="http://tempuri.org/">boolean</boolean>

 以上GET示例相應代碼如下

複製代碼

複製代碼

 /// <summary>
        /// 需要WebService支持Get調用
        /// </summary>
        public static string WebServiceGet(String URL, String MethodName, Hashtable Pars)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName + "?" + HashtableToPostData(Pars));
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";
            // 憑證
            request.Credentials = CredentialCache.DefaultCredentials;
            //超時時間
            request.Timeout = 10000;
            var response = request.GetResponse();
            var stream = response.GetResponseStream();
            StreamReader sr = new StreamReader(stream, Encoding.UTF8);
            String retXml = sr.ReadToEnd();
            sr.Close();
            return retXml;
        }
        private static String HashtableToPostData(Hashtable ht)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string k in ht.Keys)
            {
                if (sb.Length > 0)
                {
                    sb.Append("&");
                }
                sb.Append(HttpUtility.UrlEncode(k) + "=" + HttpUtility.UrlEncode(ht[k].ToString()));
            }
            return sb.ToString();
        }

複製代碼

複製代碼

 調用方法如下

複製代碼

複製代碼

Hashtable ht = new Hashtable();
            ht.Add("LoginName", "Admin");
            ht.Add("Password", "Password");
            ht.Add("AppKey", "123");

var data = HttpHelper.WebServiceGet("http://localhost/OpenApi/MobileService.asmx", "Login", ht);

複製代碼

複製代碼

 

HTTP POST

以下是 HTTP POST 請求和響應示例。所顯示的佔位符需替換爲實際值。

POST /OpenApi/MobileService.asmx/Login HTTP/1.1
Host: gps.szwearable.com
Content-Type: application/x-www-form-urlencoded
Content-Length: length

LoginName=string&Password=string&AppKey=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<boolean xmlns="http://tempuri.org/">boolean</boolean>

 以上POST示例相應代碼如下

複製代碼

複製代碼

         /// <summary>
        /// 需要WebService支持Post調用
        /// </summary>
        public static string PostWebService(String URL, String MethodName, Hashtable ht)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            // 憑證
            request.Credentials = CredentialCache.DefaultCredentials;
            //超時時間
            request.Timeout = 10000;
            var PostStr = HashtableToPostData(ht);
            byte[] data = System.Text.Encoding.UTF8.GetBytes(PostStr);
            request.ContentLength = data.Length;
            Stream writer = request.GetRequestStream();
            writer.Write(data, 0, data.Length);
            writer.Close();
            var response = request.GetResponse();
            var stream = response.GetResponseStream();
            StreamReader sr = new StreamReader(stream, Encoding.UTF8);
            String retXml = sr.ReadToEnd();
            sr.Close();
            return retXml;
        }

        private static String HashtableToPostData(Hashtable ht)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string k in ht.Keys)
            {
                if (sb.Length > 0)
                {
                    sb.Append("&");
                }
                sb.Append(HttpUtility.UrlEncode(k) + "=" + HttpUtility.UrlEncode(ht[k].ToString()));
            }
            return sb.ToString();
        }

複製代碼

複製代碼

 調用方法如下

複製代碼

  Hashtable ht = new Hashtable();
            ht.Add("LoginName", "Admin");
            ht.Add("Password", "Password");
            ht.Add("AppKey", "123");
  var data = HttpHelper.PostWebService("http://localhost/OpenApi/MobileService.asmx", "Login", ht);

複製代碼

WebService支持Post和Get方法

在Web.config,那麼只需要修改或添加這麼一段

<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>

 

 


 

還有一個說是動態添加,可惜我還是沒有用成功

附上此動態調用代碼

複製代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;
using System.Web.Services.Description;
using System.CodeDom;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Xml.Serialization;
using System.Text;

namespace DeliveryGT
{
    public class AddWebServices
    {
        /// < summary>             
        /// 動態調用web服務           
        /// < /summary>            
        /// < param name="url">WSDL服務地址< /param>   
        /// < param name="methodname">方法名< /param>             
        /// < param name="args">參數< /param>             
        /// < returns>< /returns>            
        public static object InvokeWebService(string url, string methodname, object[] args)
        {
            return AddWebServices.InvokeWebService(url, null, methodname, args);
        }
        /// < summary>            
        /// 動態調用web服務   
        /// < /summary>            
        /// < param name="url">WSDL服務地址< /param>  
        /// < param name="classname">類名< /param>    
        /// < param name="methodname">方法名< /param>    
        /// < param name="args">參數< /param>   
        /// < returns>< /returns>  
        public static object InvokeWebService(string url, string classname, string methodname, object[] args)
        {
            //string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";
            string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";
            if ((classname == null) || (classname == ""))
            {
                classname = AddWebServices.GetWsClassName(url);
            }
            try
            {                   //獲取WSDL     
                WebClient wc = new WebClient();
                Stream stream = wc.OpenRead(url + "?WSDL");
                ServiceDescription sd = ServiceDescription.Read(stream);
                ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
                sdi.AddServiceDescription(sd, "", "");
                CodeNamespace cn = new CodeNamespace(@namespace);
                //生成客戶端代理類代碼            
                CodeCompileUnit ccu = new CodeCompileUnit();
                ccu.Namespaces.Add(cn);
                sdi.Import(cn, ccu);
                CSharpCodeProvider icc = new CSharpCodeProvider();
                //設定編譯參數                   
                CompilerParameters cplist = new CompilerParameters();
                cplist.GenerateExecutable = false;
                cplist.GenerateInMemory = true;
                cplist.ReferencedAssemblies.Add("System.dll");
                cplist.ReferencedAssemblies.Add("System.XML.dll");
                cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
                cplist.ReferencedAssemblies.Add("System.Data.dll");
                //編譯代理類                   
                CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
                if (true == cr.Errors.HasErrors)
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
                    {
                        sb.Append(ce.ToString());
                        sb.Append(System.Environment.NewLine);
                    }
                    throw new Exception(sb.ToString());
                }
                //生成代理實例,並調用方法     
                System.Reflection.Assembly assembly = cr.CompiledAssembly;
                Type t = assembly.GetType(@namespace + "." + classname, true, true);
                object obj = Activator.CreateInstance(t);
                System.Reflection.MethodInfo mi = t.GetMethod(methodname);
                return mi.Invoke(obj, args);
                // PropertyInfo propertyInfo = type.GetProperty(propertyname);       
                //return propertyInfo.GetValue(obj, null);   
            }
            catch (Exception ex)
            {
                throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
            }
        }
        private static string GetWsClassName(string wsUrl)
        {
            string[] parts = wsUrl.Split('/');
            string[] pps = parts[parts.Length - 1].Split('.');
            return pps[0];
        }


        //上面那種方法的調用方式  
        //string url = "http://webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx";  
        //string[] args = new string[2];  
        //args[0] = "k123";  
        //args[1] = "";  
        //object result = WSHelper.InvokeWebService(url, "getDetailInfoByTrainCode", args);  
        //DataSet ds = (DataSet)result;  
        //this.GridView1.DataSource = ds;  
        //this.GridView1.DataBind();  
    }
}

複製代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章