简体   繁体   English

如何在ac#应用程序中对Web服务的两次调用之间保持会话活动?

[英]How to keep session alive between two calls to a web service in a c# application?

This is quite straight forward. 这很直截了当。
I'm calling a web-service (.asmx, with session enabled) from ac# application. 我正在从ac#应用程序调用Web服务(.asmx,启用会话)。

I want each call to be with the same session key as the previous one (as opposed to creating a new session key each time). 我希望每个调用都使用与前一个相同的会话密钥(而不是每次都创建一个新的会话密钥)。

Here's my (nothing-out-of-the-ordinary) code: 这是我的(没有什么特别的)代码:

public string invokeMethod(string methodUrl)
{
 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(methodUrl);
 req.Method = "GET";
 req.ContentLength = 0;
 var result = new StringBuilder();
 using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
 {
   StreamReader sr = new StreamReader(res.GetResponseStream());
   result.Append(sr.ReadToEnd());
 }
 return result.ToString();
}  

Now I want to call it again, in the same session. 现在我想在同一个会话中再次调用它。

Added: Thanks to Waleed 's answer, I think I should be doing (after retrieving the session id) something like: 补充:感谢Waleed的回答,我想我应该这样做(在检索会话ID之后):
Added once more: This is how it's done: 再次添加:这就是它的完成方式:

if (!string.IsNullOrEmpty(currentSessionID))
{
  Cookie cookie = new Cookie("ASP.NET_SessionId", currentSessionID);
  cookie.Domain=myDomain;  //Will not work without this line!
  req.CookieContainer.Add(cookie);  
}

Thanks again. 再次感谢。

If I understand the question correctly, I believe you'll need to get the session cookie from the response (the first time you call the web service) and add it to the request in the subsequent calls. 如果我正确理解了这个问题,我相信您需要从响应中获取会话cookie(第一次调用Web服务)并在后续调用中将其添加到请求中。

Edit: 编辑:

Using CookieContainer is the correct way but be aware that it's null by default so you'll have to to assign a CookieContainer object to the property before making the request (see the example code in the MSDN's article). 使用CookieContainer是正确的方法,但请注意它默认为null,因此您必须在发出请求之前将CookieContainer对象分配给属性(请参阅MSDN文章中的示例代码)。

As to the cookie name, the default name for ASP.NET session cookie is ASP.NET_SessionId, but the name can be different as it can be changed in web.config, or they might not be using ASP.NET sessions (their own implementation for example), so you'll have to check the cookies you get from the domain of that application (You can use an HTTP sniffer like Fiddler, or more easily if you're using FireFox or Chrome, you can check the names and contents of the cookies you get from that domain in the options dialog box). 至于cookie名称,ASP.NET会话cookie的默认名称是ASP.NET_SessionId,但名称可以不同,因为它可以在web.config中更改,或者它们可能没有使用ASP.NET会话(它们自己的实现)例如),因此您必须检查从该应用程序的域中获取的cookie(您可以使用像Fiddler这样的HTTP嗅探器,或者如果您使用的是FireFox或Chrome,则可以更轻松地检查名称和内容)您在选项对话框中从该域获得的cookie)。

There is a good discussion of using ASP.NET session state with web services on MSDN here: 在MSDN上使用ASP.NET会话状态和Web服务有一个很好的讨论:

http://msdn.microsoft.com/en-us/library/aa480509.aspx http://msdn.microsoft.com/en-us/library/aa480509.aspx

From first call, look after ASP.NET_SessionId value in response, this is session Id by default. 从第一次调用开始,在响应中查看ASP.NET_SessionId值,默认情况下这是会话ID。

Then add it to next call (see below): 然后将其添加到下一个电话(见下文):

http://support.microsoft.com/?kbid=899918 http://support.microsoft.com/?kbid=899918

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM