我一直在收到像这样的垃圾文本
�\b\0\0\0\0\0\0�\a`I�%&/m�{J�J��t�\b�`$ؐ@������iG#
在此函数末尾的html
变量中。正如您所看到的,通过注释掉的代码,我尝试了两种插入Cookie的方法。
我是这样的HTTPwebRequest / Response方法的新手。但是从我在网上找到的所有内容中,我正确地设置了我的方法。如果可能,我会很乐意帮助。
此外,当使用Fiddler解码我的请求时,并非我的所有cookie都被发送。我的代码运行时,我只发送1 utma,1 utmb,1 utmc和1 utmz。但是,当我正常登录网站时,我会收到2个utma,1个utmb,2个utmc和2个utmz。
我觉得这是我连接问题的根源,但我不确定。
static void FillCookieJar()
{
Console.WriteLine("Filling cookie jar...\r\n");
try
{
string parameters = "SUPER LONG POST DATA found from TEXTVIEW in Fidler";
Uri target = new Uri("https://foo.bar.com/UserSignIn");
byte[] buffer = Encoding.ASCII.GetBytes(parameters);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(target);
//Cookie chipOne = new Cookie("__utma", "XXXXXXXXX.XXXXXXXXX.XXXXXXXXX.XXXXXXXXX.XXXXXXXXX.XX") { Domain = target.Host };
//Cookie chipTwo = new Cookie("__utma", "XXXXXXXXX.XXXXXXXXX.XXXXXXXXX.XXXXXXXXX.XXXXXXXXX.X") { Domain = target.Host };
//Cookie chipThree = new Cookie("__utmb", "XXXXXXXXX.X.XX.XXXXXXXXX") { Domain = target.Host };
//Cookie chipFour = new Cookie("__utmc", "XXXXXXXXX") { Domain = target.Host };
//Cookie chipFive = new Cookie("__utmc", "XXXXXXXXX") { Domain = target.Host };
//Cookie chipSix = new Cookie("__utmz", "XXXXXXXXX.XXXXXXXXX.X.X.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided)") { Domain = target.Host };
//Cookie chipSeven = new Cookie("__utmz", "XXXXXXXXX.XXXXXXXXX.XX.X.utmcsr=titlesource.com|utmccn=(referral)|utmcmd=referral|utmcct=/") { Domain = target.Host };
//cookieJar.Add(chipOne);
//cookieJar.Add(chipTwo);
//cookieJar.Add(chipThree);
//cookieJar.Add(chipFour);
//cookieJar.Add(chipFive);
//cookieJar.Add(chipSix);
//cookieJar.Add(chipSeven);
request.Headers.Add("__utma", "XXXXXXXXX.XXXXXXXXX.XXXXXXXXX.XXXXXXXXX.XXXXXXXXX.XX");
request.Headers.Add("__utma", "XXXXXXXXX.XXXXXXXXX.XXXXXXXXX.XXXXXXXXX.XXXXXXXXX.X");
request.Headers.Add("__utmb", "XXXXXXXXX.X.XX.XXXXXXXXX");
request.Headers.Add("__utmc", "XXXXXXXXX");
request.Headers.Add("__utmc", "XXXXXXXXX");
request.Headers.Add("__utmz", "XXXXXXXXX.XXXXXXXXX.X.X.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided)");
request.Headers.Add("__utmz", "XXXXXXXXX.XXXXXXXXX.XX.X.utmcsr=titlesource.com|utmccn=(referral)|utmcmd=referral|utmcct=/");
request.CookieContainer = cookieJar;
request.Method = WebRequestMethods.Http.Post;
request.KeepAlive = true;
request.Accept = "*/*";
request.Headers.Add("Cache-Control", "no-cache");
request.Headers.Add("Accept-Encoding: gzip,deflate,sdch");
request.Headers.Add("Accept-Language: en-US,en;q=0.8");
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36";
request.Headers.Add("X-Requested-With: XMLHttpRequest");
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
request.Referer = "https://foo.bar.com/UserSignIn";
request.Headers.Add("Origin", "https://foo.bar.com");
request.Headers.Add("X-MicrosoftAjax", "Delta=true");
request.ContentLength = buffer.Length;
Stream PostData = request.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream Answer = response.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
String html = _Answer.ReadToEnd();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine("Error FillingCookieJar");
}
}
答案 0 :(得分:5)
您需要解压缩gzip流以获取纯文本,这可能是UTF-8
而不是ASCII(在响应的charset
标题中查找Content-Type
属性)。
您可以使用AutomaticDecompression属性自动解压缩内容。
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(target);
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
您看到的网址是UrlEncoded
。您可以使用HttpUtility.UrlDecode将网址设为https://foo.bar.com/Vendor
。
string decoded = HttpUtility.UrlDecode(html);