繁体   English   中英

如何在ASP中维护两个URL之间的会话。 净

[英]How maintain session beetween two Url in asp. Net

我有两个URl。 如果我打开第一个网址,它将允许我们进行身份验证。 第二个URL将打开Web内容作为XML数据。 我需要读取这些数据...但是,当我执行第一个URL时,它的工作正常身份验证是SUCCESS,但是我立即尝试打开第二个URL,它说身份验证失败。 如何维护从第一个URL到第二个URL的会话...

我的代码:

string url1 = "http://172.xx.xx.xx:xxxx/cms?login&username=santhu&password=welcom0e";
string url = "http://172.xx.xx.xx:xxxx//cms?status=ProcessStatus";
string result = null;
string result1 = null;
try
{
  WebClient client = new WebClient();
  result = client.DownloadString(url1);

  TextBox1.Text = result.ToString();
  result1 = client.DownloadString(url);
  TextBox2.Text = result1.ToString();
}
catch (Exception ex)
{           
}
private class CookieAwareWebClient : WebClient
{
    public CookieAwareWebClient(): this(new CookieContainer())
    {
    }
    public CookieAwareWebClient(CookieContainer c)
    {
        this.CookieContainer = c;
    }
    public CookieContainer CookieContainer { get; set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).CookieContainer = this.CookieContainer;
        }
        return request;
    }
}

否则,您可以通过使用Firebug cookie手动添加值来解决问题:)

webClient.Headers.Add("Cookie", "PHPSESSID=xxxxxxx; mosesuser=xxxxxxx; ");

您将需要记住第一个请求中的“ Set-Cookie”响应标头,并在第二个请求中将其发送。

基本上,在第一个请求之后(可能在DownloadString()之后),您需要在client.ResponseHeaders找到标头,然后需要以某种方式将其添加到client.Headers

编辑:似乎上述不可能,但您可以修改基础的WebRequest实例,请参见以下问题: 如何让WebClient使用Cookies?

或这样: http : //couldbedone.blogspot.com/2007/08/webclient-handling-cookies.html

暂无
暂无

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

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