HttpWebRequest getResponse与WireShark不匹配

时间:2016-04-26 06:46:16

标签: c# httpwebrequest wireshark getresponse

在发出特定的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响应,该响应与示例代码中的结果不同。

1 个答案:

答案 0 :(得分:0)

看起来Visual Studio中的“Text Visualizer”显示的是不完整的响应。数据始终存在。只是调试器错了。

现在我知道不要相信它。