如何在asp.net中保持两个Url之间的会话网

发布于 2024-11-16 16:34:58 字数 646 浏览 3 评论 0原文

我有两个 URl。如果我打开第一个网址,它将允许我们进行身份验证。第二个 URL 将打开 XML 数据形式的 Web 内容。我需要读取该数据...但是当我执行第一个 URL 时,它工作正常,身份验证成功,但我立即尝试打开第二个 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)
{           
}

I have two URl's . If I open first url it will allow us authentication. Second URL will open web content as XML data. I need to read that data... But when I excute first URL its working fine Authentication is SUCCESS, But immediately I try to open second URL its saying Authentication failed . How to maintain session from first URL to second URL...

My Code :

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)
{           
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

知足的幸福 2024-11-23 16:34:58
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; ");
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;
    }
}

Otherwise you can solve the problem by adding the values manually by using Firebug for cookies :)

webClient.Headers.Add("Cookie", "PHPSESSID=xxxxxxx; mosesuser=xxxxxxx; ");
扭转时空 2024-11-23 16:34:58

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

基本上,在第一个请求之后(可能在 DownloadString() 之后,您需要在 client.ResponseHeaders 中找到标头,然后需要将其添加到 client.Headers 编辑:似乎

上面是不可能的,但您可以修改底层的 WebRequest 实例,请参阅这个问题:如何让 WebClient 使用 Cookie?

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

You will need to remember the "Set-Cookie" response header from the first request and send it in your second request.

Basically, after the first request (probably after DownloadString() you would need to find the header in client.ResponseHeaders, and then you would need to add it to client.Headers somehow.

EDIT: Seems like the above isn't possible, but you can modify the underlying WebRequest instance, see this question: How can I get the WebClient to use Cookies?

or this: http://couldbedone.blogspot.com/2007/08/webclient-handling-cookies.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文