如何在asp.net中保持两个Url之间的会话网
我有两个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
否则,您可以通过使用 Firebug 来手动添加 cookie 值来解决问题:)
Otherwise you can solve the problem by adding the values manually by using Firebug for cookies :)
您需要记住第一个请求中的“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 toclient.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