我现在正在失去绝对的头脑。我已经完成了大量的HttpWebRequest类型项目,通常不会遇到调试会话无法处理的问题。
我正在使用Fiddler / Chrome网络工具来嗅探网络请求。我正在与Lotus-Domino服务器(.nsf扩展名)进行交互。
该网站有一个初始页面,我也登录了登录凭据。它反过来使用AllowAutoRedirect执行302跟随。这个页面是在Chrome和Fiddler中设置cookie的地方。但是,它没有使用Headers这样做..它在体内使用meta标签,如下所示:
<META HTTP-EQUIV="Set-Cookie" CONTENT="sh=Y; path=/; domain=website.com ">
这些cookie不会被响应选中,也永远不会被添加到CookieContainer中。我尝试在重定向后将它们添加到CookieContainer中,但没有运气。以下请求均失败并返回初始登录页面。
var cookieJar = new CookieContainer();
var postData = "username=test&password=letmein";
var url = "https://www.example.com/names.nsf?Login";
var request = (HttpWebRequest)WebRequest.Create(url);
request.KeepAlive = true;
request.Headers["Cache-Control"] = "max-age:0";
request.Headers["Origin"] = "https://www.example.com";
request.Headers["Upgrade-Insecure-Requests"] = "1";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.ContentType = "application/x-www-form-urlencoded";
request.Headers["DNT"] = "1";
request.Referer = "https://www.example.com/";
request.Headers["Accept-Encoding"] = "gzip, deflate, br";
request.Headers["Accept-Language"] = "en-US,en;q=0.8";
request.CookieContainer = cookieJar;
request.AllowAutoRedirect = true;
request.MaximumAutomaticRedirections = 20;
request.Method = "POST";
var buffer = Encoding.ASCII.GetBytes(postData);
request.ContentLength = buffer.Length;
var requestStream = request.GetRequestStream();
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Close();
cookieJar.Add(new Uri("http://www.example.com"), new Cookie("sp", "Y", "/", "www.example.com"));
cookieJar.Add(new Uri("http://www.example.com"), new Cookie("coa", "Y", "/", "www.example.com"));
cookieJar.Add(new Uri("http://www.example.com"), new Cookie("co", "2", "/", "www.example.com"));
cookieJar.Add(new Uri("http://www.example.com"), new Cookie("so", "Y", "/", "www.example.com"));
cookieJar.Add(new Uri("http://www.example.com"), new Cookie("dt", "Y", "/", "www.example.com"));
cookieJar.Add(new Uri("http://www.example.com"), new Cookie("vf", "Y", "/", "www.example.com"));
cookieJar.Add(new Uri("http://www.example.com"), new Cookie("ar", "0", "/", "www.example.com"));
cookieJar.Add(new Uri("http://www.example.com"), new Cookie("bo", "Y", "/", "www.example.com"));
cookieJar.Add(new Uri("http://www.example.com"), new Cookie("sh", "Y", "/", "www.example.com"));
cookieJar.Add(new Uri("http://www.example.com"), new Cookie("cm", "Y", "/", "www.example.com"));
cookieJar.Add(new Uri("http://www.example.com"), new Cookie("em", "", "/", "www.example.com"));
cookieJar.Add(new Uri("http://www.example.com"), new Cookie("er", "Y", "/", "www.example.com"));
cookieJar.Add(new Uri("http://www.example.com"), new Cookie("browser", "Netscape", "/", "www.example.com"));
cookieJar.Add(new Uri("http://www.example.com"), new Cookie("dl", "Y", "/", "www.example.com"));
cookieJar.Add(new Uri("http://www.example.com"), new Cookie("os", "Win", "/", "www.example.com"));
var response = (HttpWebResponse)request.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{
var sResponse = sr.ReadToEnd();
}
// this is the redirected URL, so the redirect did happen
Console.WriteLine(response.ResponseUri);
// it does a self-post with a hidden form value
postData = "__Click=0";
url = "https://www.example.com/domreg.nsf/SetCookies?OpenForm&Seq=1";
request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = cookieJar;
request.AllowAutoRedirect = true;
request.MaximumAutomaticRedirections = 20;
request.Method = "POST";
request.KeepAlive = true;
request.Headers["Cache-Control"] = "max-age:0";
request.Headers["Origin"] = "https://www.example.com";
request.Headers["Upgrade-Insecure-Requests"] = "1";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.ContentType = "application/x-www-form-urlencoded";
request.Headers["DNT"] = "1";
request.Referer = "https://www.example.com/";
request.Headers["Accept-Encoding"] = "gzip, deflate, br";
request.Headers["Accept-Language"] = "en-US,en;q=0.8";
buffer = Encoding.ASCII.GetBytes(postData);
request.ContentLength = buffer.Length;
requestStream = request.GetRequestStream();
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Close();
response = (HttpWebResponse)request.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{
// back to the login page
var sResponse = sr.ReadToEnd();
}