1

基本上我在下面有这段代码,最后一步 FinalStepGetReportData() 在返回时抛出异常 (HttpWebResponse)request.GetResponse(); 如果提琴手被关闭?

除了最后一步之外,当 Fiddler 关闭时,所有其他操作都有效。当我在最后一步打开 Fiddler 时有效吗?

有任何想法吗?我正用头撞墙。

谢谢


public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy
    {
        public TrustAllCertificatePolicy() { }
        public bool CheckValidationResult(ServicePoint sp,
            X509Certificate cert,
            WebRequest req,
            int problem)
        {
            return true;
        }
    }

        public void Download()
        {
            string cookiesInRawFormat = null;
            NameValueCollection headers = null;
            HttpWebResponse response = null;

            request = (HttpWebRequest)HttpWebRequest.Create(Config.ReportUrl);
            System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
            request.Accept = "image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
            request.KeepAlive = true;
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)";

            headers = new NameValueCollection()
            {
                { "Accept-Language", "en-us,en;q=0.5" }
            };

            request.Headers.Add(headers);
            request.CookieContainer = cookies;
            response = (HttpWebResponse) request.GetResponse();
            cookiesInRawFormat = response.Headers["Set-Cookie"];
            if (!String.IsNullOrEmpty(cookiesInRawFormat))
            {
                ParseCookiesFromResponseHeader(cookiesInRawFormat);
            }
            string location = response.ResponseUri.AbsolutePath;

            using (response = SignIn(location))
            {
                                using (Stream dataStream = FinalStepGetReportData(response, location).GetResponseStream())
                {
                    // Open the stream using a StreamReader for easy access.
                    using (StreamReader reader = new StreamReader(dataStream))//, Encoding.UTF8))
                    {
                        // Read the content.
                        string responseFromServer = reader.ReadToEnd();
                        // Display the content.
                        Console.WriteLine(responseFromServer);
                    }
                }
            }
        }

        private HttpWebResponse SignIn(string referrerLocation)
        {
            request = (HttpWebRequest)HttpWebRequest.Create(Config.SignInUrl);
            request.Accept = "image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
            request.KeepAlive = true;
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Referer = referrerLocation;
            System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();

            NameValueCollection headers = new NameValueCollection()
            {
                { "Accept-Language", "en-us,en;q=0.5" },
                { "Cache-Control", "no-cache" }
            };

            request.Method = HttpMethod.POST.ToString();
            request.Headers.Add(headers);
            request.CookieContainer = cookies;
            request.AllowAutoRedirect = false;

            PostDataBuilder builder = new PostDataBuilder() { 
                { "action", "sign-in" },
                { "disableCorpSignUp", String.Empty },
                { "email", Config.UserName },
                { "metadata1", "Firefox 3.0.10 Windows" },
                { "metadata2", "Mozilla Default Plug-in Java(TM) Platform SE 6 U12 QuickTime Plug-in 7.6 Windows Genuine Advantage 19000912007 Microsoft Office system Shockwave Flash 10012iTunes Application Detector Silverlight Plug-In 20401150Windows Presentation Foundation RealPlayer(tm) G2 LiveConnect-Enabled Plug-In (32-bit)  RealPlayer Version Plugin 601269Java(TM) Platform SE 6 U12 16012||1280-1024-971-32-*-*-*" },
                { "metadata3", "timezone: -2 execution time: 3" },
                { "metadataf1", String.Empty },
                { "mode", "1" },
                { "pageAction", "****THE HTML PAGE*****" },
                { "password", Config.Password },
                { "path", "****THE HTML PAGE*****" },
                { "protocol", "https" },
                { "query", String.Empty },
                { "redirectProtocol", String.Empty },
                { "useRedirectOnSuccess", "0" },
                { "x", "134" },
                { "y", "15" } };

            byte[] postDataBytes = builder.Build(Encoding.UTF8);
            request.ContentLength = postDataBytes.Length;

            using (Stream stream = request.GetRequestStream())
            {
                stream.Write(postDataBytes, 0, postDataBytes.Length);
            }

            return (HttpWebResponse)request.GetResponse();
        }


        private HttpWebResponse FinalStepGetReportData(HttpWebResponse response, string referer)
        {
            request = (HttpWebRequest)HttpWebRequest.Create(response.Headers["Location"]);
            request.Accept = "image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)";
            request.ContentType = "text/xml; encoding='utf-8'";
            request.Method = HttpMethod.GET.ToString();
            request.Referer = referer;
            request.KeepAlive = true;
            request.ProtocolVersion = HttpVersion.Version10;
            request.Headers.Add(HttpRequestHeader.Cookie, response.GetResponseHeader("Set-Cookie"));
            System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();

            NameValueCollection headers = new NameValueCollection()
            {
                {"Accept-Language", "en-us,en;q=0.5" },
                {"Cache-Control", "no-cache" },
                {"Accept-Encoding", "deflate"},
            };

            request.Headers.Add(headers);
            return (HttpWebResponse)request.GetResponse();
        }
4

3 回答 3

1

你得到的确切例外是什么?这里的一个常见问题是在阅读完响应后忘记调用 .Close 。

旧版本的 Fiddler 默认会关闭客户端连接,这解决了一些客户端没有正确关闭响应流本身的问题。

于 2009-06-26T04:19:49.173 回答
0

我有完全相同的问题,这是因为我没有像你提到的那样关闭它就留下了一个回复。

于 2009-10-14T20:29:30.420 回答
-1

在被黑客攻击的站点上发现了一篇有趣的文章,在该文章中他遇到了使用 HTTP 1.1 发布表单数据的问题。值得一试...

http://haacked.com/archive/2004/05/15/http-web-request-expect-100-continue.aspx

//try this or the other solutions given in the comments
request.ServicePoint.Expect100Continue = false; 
于 2009-06-23T16:44:46.337 回答