HttpWebRequest getResponse 不匹配 WireShark
HttpWebRequest getResponse not matching WireShark
在发出特定的 GET 请求时,html 响应的中心似乎缺少一大块 html。我似乎无法弄清楚它是如何被删除的。如果你 运行 示例代码并用 Wireshark 记录它。您会看到 Wireshark 的数据 html 比示例中的结果多。 (在两者中搜索 'french';您会注意到示例中缺少它,但它显示在 wireshark 中)。
static string ReadHTML(string urlAddress)
{
urlAddress = "http://www.ebay.com/sch/Dress-Shirts-/57991/i.html?_fln=1&_dmd=1&_ipg=200&_from=R40&_dcat=57991&LH_ItemCondition=3000&_nkw=&LH_Complete=1&LH_Sold=1";
HttpWebRequest request = WebRequest.Create(urlAddress) as HttpWebRequest;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1";
request.Host = "www.ebay.com";
request.Headers.Add("Accept-Encoding", "gzip, deflate, sdch");
request.Headers.Add("Accept-Language", "en-US,*");
//request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(new System.Net.Cookie("JSESSIONID", "989EDC0DF3234BF32A2544E4FA96C314") { Domain = request.Host });//JSESSIONID=989EDC0DF3234BF32A2544E4FA96C314
request.CookieContainer.Add(new System.Net.Cookie("npii", "btguid/4948662a1540a1c448711d0effdd49bb58fe33af^cguid/4948bab11540a56603d04eb0fd0d7c0c58fe33af^") { Domain = request.Host });//npii=btguid/4948662a1540a1c448711d0effdd49bb58fe33af^cguid/4948bab11540a56603d04eb0fd0d7c0c58fe33af^
request.CookieContainer.Add(new System.Net.Cookie("ebay", "%5Esbf%3D%2320000000000000000000210%5E") { Domain = request.Host });//ebay=%5Esbf%3D%2320000000000000000000210%5E
request.CookieContainer.Add(new System.Net.Cookie("ns1", "=BAQAAAVQr0QFlAAaAANgASVkAJ2pjNjl8NjAxXjE0NjE1MTgzNjIxNjleXjFeM3wyfDV8NHw3XjFeMl40XjNeMTJeMTJeMl4xXjFeMF4xXjBeMV42NDQyNDU5MDc17HliWNBjA7nLq7R3ubPYrU6hFOk*") { Domain = request.Host });
request.CookieContainer.Add(new System.Net.Cookie("dp1", "bu1p/QEBfX0BAX19AQA**5900276a^bl/US5ae15aea^") { Domain = request.Host });
request.CookieContainer.Add(new System.Net.Cookie("nonsession", "CgADLAAFXHvryMwDKACBghPVqNDk0ODY2MmExNTQwYTFjNDQ4NzExZDBlZmZkZDQ5YmI2iA27") { Domain = request.Host });
request.CookieContainer.Add(new System.Net.Cookie("s", "CgAD4ACBXIEVqNTBkYjAwODExNTQwYTU2MTc4NDcxNWIyZmZjMzkyNWIA7gCVVyBFajMGaHR0cDovL3d3dy5lYmF5LmNvbS9zY2gvRHJlc3MtU2hpcnRzLS81Nzk5MS9pLmh0bWw/X2Zsbj0xJl9kbWQ9MSZfaXBnPTIwMCZfZnJvbT1SNDAmX2RjYXQ9NTc5OTEmTEhfSXRlbUNvbmRpdGlvbj0zMDAwJl9ua3c9JkxIX0NvbXBsZXRlPTEmTEhfU29sZD0xGTda/A**") { Domain = request.Host });
if (request == null)
return "";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response == null)
return "";
if (response.StatusCode != HttpStatusCode.OK)
return "";
Stream s2 = response.GetResponseStream();
if (response.ContentEncoding.ToLower().Contains("gzip"))
s2 = new GZipStream(s2, CompressionMode.Decompress);
else if (response.ContentEncoding.ToLower().Contains("deflate"))
s2 = new DeflateStream(s2, CompressionMode.Decompress);
StreamReader sr = null;
if (response.CharacterSet == null)
{
sr = new StreamReader(s2);
}
else
{
sr = new StreamReader(s2, Encoding.GetEncoding(response.CharacterSet));
}
string data = sr.ReadToEnd();
response.Close();
return data;
}
您还可以通过 PostMan(chrome 的插件)模拟 GET 请求,并查看与 Wireshark 从请求中获得的完全相同的 html 响应,这与示例中的结果不同代码。
看起来 Visual Studio 中的 'Text Visualizer' 显示的响应不完整。数据一直都在那里。只是调试器错了。
现在我知道不能相信它了。
在发出特定的 GET 请求时,html 响应的中心似乎缺少一大块 html。我似乎无法弄清楚它是如何被删除的。如果你 运行 示例代码并用 Wireshark 记录它。您会看到 Wireshark 的数据 html 比示例中的结果多。 (在两者中搜索 'french';您会注意到示例中缺少它,但它显示在 wireshark 中)。
static string ReadHTML(string urlAddress)
{
urlAddress = "http://www.ebay.com/sch/Dress-Shirts-/57991/i.html?_fln=1&_dmd=1&_ipg=200&_from=R40&_dcat=57991&LH_ItemCondition=3000&_nkw=&LH_Complete=1&LH_Sold=1";
HttpWebRequest request = WebRequest.Create(urlAddress) as HttpWebRequest;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1";
request.Host = "www.ebay.com";
request.Headers.Add("Accept-Encoding", "gzip, deflate, sdch");
request.Headers.Add("Accept-Language", "en-US,*");
//request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(new System.Net.Cookie("JSESSIONID", "989EDC0DF3234BF32A2544E4FA96C314") { Domain = request.Host });//JSESSIONID=989EDC0DF3234BF32A2544E4FA96C314
request.CookieContainer.Add(new System.Net.Cookie("npii", "btguid/4948662a1540a1c448711d0effdd49bb58fe33af^cguid/4948bab11540a56603d04eb0fd0d7c0c58fe33af^") { Domain = request.Host });//npii=btguid/4948662a1540a1c448711d0effdd49bb58fe33af^cguid/4948bab11540a56603d04eb0fd0d7c0c58fe33af^
request.CookieContainer.Add(new System.Net.Cookie("ebay", "%5Esbf%3D%2320000000000000000000210%5E") { Domain = request.Host });//ebay=%5Esbf%3D%2320000000000000000000210%5E
request.CookieContainer.Add(new System.Net.Cookie("ns1", "=BAQAAAVQr0QFlAAaAANgASVkAJ2pjNjl8NjAxXjE0NjE1MTgzNjIxNjleXjFeM3wyfDV8NHw3XjFeMl40XjNeMTJeMTJeMl4xXjFeMF4xXjBeMV42NDQyNDU5MDc17HliWNBjA7nLq7R3ubPYrU6hFOk*") { Domain = request.Host });
request.CookieContainer.Add(new System.Net.Cookie("dp1", "bu1p/QEBfX0BAX19AQA**5900276a^bl/US5ae15aea^") { Domain = request.Host });
request.CookieContainer.Add(new System.Net.Cookie("nonsession", "CgADLAAFXHvryMwDKACBghPVqNDk0ODY2MmExNTQwYTFjNDQ4NzExZDBlZmZkZDQ5YmI2iA27") { Domain = request.Host });
request.CookieContainer.Add(new System.Net.Cookie("s", "CgAD4ACBXIEVqNTBkYjAwODExNTQwYTU2MTc4NDcxNWIyZmZjMzkyNWIA7gCVVyBFajMGaHR0cDovL3d3dy5lYmF5LmNvbS9zY2gvRHJlc3MtU2hpcnRzLS81Nzk5MS9pLmh0bWw/X2Zsbj0xJl9kbWQ9MSZfaXBnPTIwMCZfZnJvbT1SNDAmX2RjYXQ9NTc5OTEmTEhfSXRlbUNvbmRpdGlvbj0zMDAwJl9ua3c9JkxIX0NvbXBsZXRlPTEmTEhfU29sZD0xGTda/A**") { Domain = request.Host });
if (request == null)
return "";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response == null)
return "";
if (response.StatusCode != HttpStatusCode.OK)
return "";
Stream s2 = response.GetResponseStream();
if (response.ContentEncoding.ToLower().Contains("gzip"))
s2 = new GZipStream(s2, CompressionMode.Decompress);
else if (response.ContentEncoding.ToLower().Contains("deflate"))
s2 = new DeflateStream(s2, CompressionMode.Decompress);
StreamReader sr = null;
if (response.CharacterSet == null)
{
sr = new StreamReader(s2);
}
else
{
sr = new StreamReader(s2, Encoding.GetEncoding(response.CharacterSet));
}
string data = sr.ReadToEnd();
response.Close();
return data;
}
您还可以通过 PostMan(chrome 的插件)模拟 GET 请求,并查看与 Wireshark 从请求中获得的完全相同的 html 响应,这与示例中的结果不同代码。
看起来 Visual Studio 中的 'Text Visualizer' 显示的响应不完整。数据一直都在那里。只是调试器错了。
现在我知道不能相信它了。