久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

動(dòng)態(tài)WebService方法

 行動(dòng)如風(fēng)的蝸牛 2017-12-13

  調(diào)用Webservice的方法一般是通過右擊項(xiàng)目--》添加服務(wù)引用--》輸入Webservice地址--》前往--》確定,,這樣可以順利調(diào)用服,,但是需要注意的一點(diǎn)是:如果上面的方法是在非啟動(dòng)項(xiàng)項(xiàng)目(比如某個(gè)類庫)中添加的,在該項(xiàng)目下會(huì)自動(dòng)生成一個(gè)app.config文件,,而在主配置文件web.config中并沒有自動(dòng)添加上該webservice的標(biāo)記,,這樣運(yùn)行會(huì)出現(xiàn)錯(cuò)誤,說找不到配置信息等等……所有還需要把a(bǔ)pp.config中的<system.serviceModel>……</system.serviceModel>這段配置添加到web.config的<configuration>……</configuration>標(biāo)記中,,這樣運(yùn)行就不會(huì)出問題了,。如果以后服務(wù)地址發(fā)生了變化,也只需要修改web.config中的地址就行了,。

  如果你覺得上面的方法含麻煩的話,,你可以選擇下面的方法:動(dòng)態(tài)WebService方法。需要寫一個(gè)底層解析Webservice服務(wù)地址的方法,,然后調(diào)用就可以,,很方便。服務(wù)地址你可以配置到web.config中,,也可以保存到數(shù)據(jù)庫中,,隨你了……

  下面通過一個(gè)判斷騰訊QQ在線狀態(tài)的例子說明一下動(dòng)態(tài)WebService的方法。

      騰訊QQ在線狀態(tài)WEB 服務(wù):http://webservice./webservices/qqOnlineWebService.asmx

  方法:qqCheckOnline 獲得騰訊QQ在線狀態(tài)

  輸入?yún)?shù):QQ號(hào)碼 String,,默認(rèn)QQ號(hào)碼:8698053,。返回?cái)?shù)據(jù):String,Y = 在線,;N = 離線,;E = QQ號(hào)碼錯(cuò)誤;A = 商業(yè)用戶驗(yàn)證失??;V = 免費(fèi)用戶超過數(shù)量

復(fù)制代碼
using System;
using System.Collections;
using System.Reflection;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Services.Description;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Net;
using System.Web;

namespace Kayang.WebService
{
    /// <summary>
    /// WebServiceProxy 的摘要說明。
    /// </summary>
    public sealed class WebServiceProxy
    {
        private static Hashtable _assenblyCache = null;  //緩存提高效率
        public WebServiceProxy()
        {
        }
        //url:服務(wù)地址
     //methodname:方法名字
//args:方法的參數(shù) public static object InvokeWebservice(string url, string nsClassName, string methodname, params object[] args) { return InvokeWebservice(url, nsClassName, methodname, 100000, args); } public static object InvokeWebservice(string url, string nsClassName, string methodname, int timeout, params object[] args) { if (args.Length == 1 && args[0] is ArrayList) { args = (args[0] as ArrayList).ToArray(); } try { int li = nsClassName.LastIndexOf('.'); string @namespace = (li == -1 ? "" : nsClassName.Substring(0, li)); Assembly assembly; if (_assenblyCache == null) { _assenblyCache = new Hashtable(); } if (_assenblyCache.ContainsKey(url.ToUpper())) { assembly = (Assembly)_assenblyCache[url.ToUpper()]; } else { System.Net.WebClient wc = new System.Net.WebClient(); System.IO.Stream stream = wc.OpenRead(url + "?WSDL"); //Configuration.SoapEnvelopeProcessingElement se = new Configuration.SoapEnvelopeProcessingElement(); //se.ReadTimeout = 15000; 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); Microsoft.CSharp.CSharpCodeProvider csc = new Microsoft.CSharp.CSharpCodeProvider(); ICodeCompiler icc = csc.CreateCompiler(); 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 (CompilerError ce in cr.Errors) { sb.Append(ce.ToString()); sb.Append(System.Environment.NewLine); } throw new Exception(sb.ToString()); } assembly = cr.CompiledAssembly; _assenblyCache[url.ToUpper()] = assembly; } Type t = null; if (String.IsNullOrEmpty(nsClassName)) { t = assembly.GetTypes()[0]; } else { t = assembly.GetType(nsClassName, true, true); } MethodInfo mi = null; if (String.IsNullOrEmpty(methodname)) { mi = t.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)[0]; } else { mi = t.GetMethod(methodname); } SoapHttpClientProtocol obj = Activator.CreateInstance(t) as SoapHttpClientProtocol; SetCookie(url, obj); //obj.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; obj.Timeout = timeout; return mi.Invoke(obj, args); } catch (Exception ex) { throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace)); } } /// <summary> /// 傳入Cookie,,使對(duì)方可以使用當(dāng)前Session /// By 黃正 2009-12-6 /// </summary> /// <param name="url"></param> /// <param name="obj"></param> private static void SetCookie(string url, SoapHttpClientProtocol obj) { HttpContext ctx = HttpContext.Current; if (ctx != null) { CookieContainer cc = new CookieContainer(); foreach (string cookieName in ctx.Request.Cookies.AllKeys) { cc.SetCookies(new Uri(url), cookieName + "=" + ctx.Request.Cookies[cookieName].Value); } //req.Headers.Add(HttpRequestHeader.Cookie, Request.Headers["Cookie"]); obj.CookieContainer = cc; } } } }
復(fù)制代碼

調(diào)用:

string url = "http://webservice./webservices/qqOnlineWebService.asmx";
string @namespace="";
string methodname = "qqCheckOnline";//需要調(diào)用的webservice中的方法
string Invoke = "123456";//QQ號(hào)碼
string result = WebService.WebServiceProxy.InvokeWebservice(url, @namespace, methodname, Invoke).ToString();


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購買等信息,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約