Problem notes using ASP.NET_SessionId in ASP.NET WebService

  • 2020-05-12 02:29:26
  • OfStack

Today, I was helping colleagues to solve the problem of making Web References call to WebService: Method1 found that the server Set-Cookie: ASP.NET_SessionId =**** saved the session state of ASP.NET when the first webservice method Method1 was called. Then when I called webservice's second method, Method2, I found that I could not correctly return the result of the server-side processing

That is to say, webservices cannot normally respond to the server when http post is running. It feels like the problem is with cookie (Set-Cookie on the server when Method1 is called), but it just doesn't work. Then test webservices browser side access, 1 cut is normal. Blame yourself for knowing so little about WebService that when you know the problem, you can't solve it.

This is often the case with technical problems. The more you try to solve it, the harder it is to solve it quickly. So I went home from work, took a shower, ate, and tried to solve the problem. I suddenly thought of long long ago wrote an article 1, is http caught in the article, the article addresses here: http: / / www cnblogs. com ryanding/archive 2011/01/17/1936392. html. It mainly solves the problem through HttpWebRequest and HttpWebResponse. And then I thought, well, this is going to fix this problem. But it's a little too complicated. Remember that HttpWebRequest has an CookieContainer object that solves the cookie problem. As a result, they desperately look for relevant properties on WebServcie's Web References instantiated objects. Finally, I solved it perfectly. The code is as follows:
 
ConsoleApplication1.WebReference.YourWebServiceName proxy= new ConsoleApplication1.WebReference.YourWebServiceName(); 
System.Net.CookieContainer Cookies = new System.Net.CookieContainer(); 
proxy.CookieContainer = Cookies; 

The code executes the above information before executing Method1, because this thing just started Set-Cookie...
Then in the implementation of Method21 cut all OK.
This indicates that proxy.CookieContainer stores the client ASP.NET_SessionId. In this way, ASP.NET_SessionId will be passed to the server each time it is called through the webservice method.
Note: this WEBSERVICES is provided by the customer. We cannot modify its code arbitrarily.
I think many people have encountered similar problems. This article will be convenient later.

Related articles: