用C# Console程序访问Web站点时,如何设置Session信息?
在IE中输入一个认证URL,比如:https://login.WebSite.com.tr/authenticate4outprg.aspx?userid=eZhang&password=XXXYYY。
然后在同一个IE窗口中输入https://fosn.WebSite.com.tr/prg_daily_summary_xml.asp?e=1来下载文件。
我用下面代码试图完成上述动作:
string url1 = "https://login.WebSite.com.tr/authenticate4outprg.aspx?userid=eZhang&password=XXXYYY";
string url2 = "https://fosn.WebSite.com.tr/prg_daily_summary_xml.asp?e=1";
//Log In
HttpWebRequest req1 = (HttpWebRequest)WebRequest.Create(url1);
req1.Method = "POST";
req1.ContentType = "application/x-www-form-urlencoded";
req1.ContentLength = 0;
HttpWebResponse res = (HttpWebResponse)req1.GetResponse();
StreamReader sr1 = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
string backstr1 = sr1.ReadToEnd();
Console.WriteLine(backstr1);
sr1.Close();
res.Close();
//Down Load ASN File
WebClient wc = new WebClient();
wc.DownloadFile(url2,"C:\\haha.xml");
res.Close();
但是,上面代码的两次请求不是同一个Session,认证信息带不到下载页面,故无法下载。哪位知道如何把session信息带到下一个request?