HttpWebRequest:如何获取会话ID [英] HttpWebRequest: how get the session id

查看:89
本文介绍了HttpWebRequest:如何获取会话ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用网站的Web服务与外部服务器进行通信.外部服务器要求一个会话ID.

we are using a web service for a web site to communicate with an external server. External server ask for a session id.

我们的以下代码询问外部服务器:

Our following code ask external server:

HttpWebRequest webRequest = WebRequest.Create(ExtUrl) as HttpWebRequest;
webRequest.Credentials = new NetworkCredential(ExtAccountToUse, ExtPassword);
HttpWebResponse webResponse;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";                
StreamWriter writer = new StreamWriter(webRequest.GetRequestStream());
 writer.Write(xmlOutput);
writer.Close();               
webResponse = webRequest.GetResponse() as HttpWebResponse;  

是否可以获取会话ID以发送到外部服务器?

Is it possible to get a session id to send to external server ?

感谢您的时间

推荐答案

这取决于要将请求发送到的服务器的类型.例如,如果您有一个IIS托管站点,则它期望会话ID Cookie(名称为 ASP.NET_SessionId )(或在请求字符串上).如果在另一端有Java servlet引擎,则它需要一个名为 JSESSIONID 的cookie(或请求路径参数 jsessionid ).

That depends on the type of server you are sending the request to. For example, if you have an IIS hosted site, it expects a session id inside a cookie named ASP.NET_SessionId (or on the request string). If you have a Java servlet engine on the other side, it expects a cookie called JSESSIONID (or a request path parameter jsessionid).

这取决于.但是,在HttpWebRequest中设置cookie并不困难.您可以使用属性 CookieContainer :

So it depends. However, setting cookies inside a HttpWebRequest is not difficult. You can use the property CookieContainer:

CookieContainer cookies = new CookieContainer();
cookies.Add(new Cookie("ASP.NET_SessionId", sessionId));
request.CookieContainer = cookies;

您存储在cookie中的会话标识符应该具有特定的格式,这又取决于另一端的服务器类型.默认情况下,在ASP.NET中,类 SessionIDManager 用于生成和验证会话ID.此类很难重用,因为它需要 HttpContext .但是,您可以使用Reflector检查它如何生成会话ID.

The session identifier you store inside the cookie should have a particular format and again, this depends on the server type at the other end. In ASP.NET by default the class SessionIDManager is used to produce and validate session ids. This class is hard to reuse because it requires an HttpContext. However, you could check with Reflector how it generates a session id.

这篇关于HttpWebRequest:如何获取会话ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆