在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?
然后在同一个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?
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultCredentials;
wc.DownloadFile(url2,"C:\\haha.xml");
res.Close();
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"; CookieContainer cc = new CookieContainer();
//Log In
HttpWebRequest req1 = (HttpWebRequest)WebRequest.Create(url1);
req1.CookieContainer = cc;
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(); //Download File
HttpWebRequest req2 = (HttpWebRequest)WebRequest.Create(url2);
req2.CookieContainer = cc;
req2.Method = "POST";
req2.ContentType = "application/x-www-form-urlencoded";
req2.ContentLength = 0; HttpWebResponse res = (HttpWebResponse)req2.GetResponse();
StreamReader sr2 = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
string backstr2 = sr2.ReadToEnd(); //将backstr2的内容写入本地文件
……………………………………