How to keep session when you dynamically call WebService?

I follow the method introduced on the Internet to dynamically call WebService, but if the WebService method Contains the action of operating the Session, and the Session cannot be maintained between the previous and subsequent actions. I have been confused with this, not only how to solve it, I hope all teachers can give me advice. The following is the method of dynamically invoking WebService: public object InvokeWebservice(string url, string @namespace, string classname, string methodname, object[] args) {try {System.Net.WebClient wc = new System.Net.WebClient(); System .IO.Stream stream = wc.OpenRead(url + "?WSDL"); System.Web.Services.Description.ServiceDescription sd = System.Web.Services.Description.ServiceDescription.Read(stream); System.Web.Services. Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter(); sdi.AddServiceDescription(sd, "", ""); System.CodeDom.CodeNamespace cn = new System.CodeDom.CodeNamespace(@namespace); System .CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit(); ccu.Namespaces.Add(cn); sdi.Import(cn, ccu); Microsoft.CSharp.CSharpCodeProvider csc = new Microsoft.CSharp.CSharpCodeProvider(); S ystem.CodeDom.Compiler.ICodeCompiler icc = csc.CreateCompiler(); System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.CompilerParameters(); cplist.GenerateExecutable = false; cplist.GenerateInies = trued; cplist.GenerateInies = true; .Add("System.dll"); cplist.ReferencedAssemblies.Add("System.XML.dll"); cplist.ReferencedAssemblies.Add("System.Web.Services.dll"); cplist.ReferencedAssemblies.Add("System .Data.dll"); System.CodeDom.Compiler.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);} catch (Exception ex) {throw ex;}} The following are the two methods in WebService: [ WebMethod(EnableSession=true)] public bool Authenticate(string uid, string pwd) {if (uid == pwd) {Session["authentification"] = "1"; return true;} else {return false;}} [WebMethod (EnableSession = true)] public bool Test() {if (Session["authentification"] != null && Session["authentification"].ToString() == "1") {return true;} else {return false; } }
Thank you in advance, I understand the principle, but for my dynamic call to the Webservice method above, where should I get the cookie content returned in the http header? Where should I send it? Please give me some pointers again, I am grateful. Please pay attention to the comment I added public partial class _Default: System.Web.UI.Page {protected void Page_Load(object sender, EventArgs e) {Response.Write("Test web service, direct method
"); CookieContainer container = new CookieContainer(); localhost.Service1 service1 = new localhost.Service1(); service1.CookieContainer = container; Response.Write(service1.Authenticate("raymond", "raymond")); service1.CookieContainer = container;/ /If it is not the following statement, it is still False Response.Write(service1.Test()); Response.Write("Test web service, CodeDom method
"); Response.Write(InvokeWebservice("http: //localhost/MyTestWebService/Service1.asmx", "MyTestWebService", "Service1", "Authenticate", new object[] {"raymond", "raymond" },container)); Response.Write(InvokeWebservice("http: //localhost/MyTestWebService/Service1.asmx", "MyTestWebService", "Service1", "Test", new object[] {},container));} public object InvokeWebservice(string url, string @namespace, s tring classname, string methodname, object[] args,CookieContainer container) {try {System.Net.WebClient wc = new System.Net.WebClient(); System.IO.Stream stream = wc.OpenRead(url + "?WSDL" ); System.Web.Services.Description.ServiceDescription sd = System.Web.Services.Description.ServiceDescription.Read(stream); System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter( ); sdi.AddServiceDescription(sd, "", ""); System.CodeDom.CodeNamespace cn = new System.CodeDom.CodeNamespace(@namespace); System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit(); ccu .Namespaces.Add(cn); sdi.Import(cn, ccu); Microsoft.CSharp.CSharpCodeProvider csc = new Microsoft.CSharp.CSharpCodeProvider(); System.CodeDom.Compiler.ICodeCompiler icc = csc.CreateCompi ler(); System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.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"); System.CodeDom. Compiler.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); //Set CookieContainer 1987raymond add PropertyInfo property = t .GetProperty("CookieContainer"); property.SetValue(obj, container, null); System.Reflection.MethodInfo mi = t.GetMethod(methodname); return mi.Invoke(obj, args);} catch (Exception ex) { throw ex;}}} The execution result test web service, the direct method TrueTrue test web service, the CodeDom method TrueTrue

Original link

Leave a Comment

Your email address will not be published.