【问题标题】:HTTPS C# Post Problem (with Fiddler)HTTPS C# 发布问题(使用 Fiddler)
【发布时间】:2009-06-23 16:32:35
【问题描述】:

基本上我在下面有这段代码,最后一步 FinalStepGetReportData() 在返回时抛出异常 (HttpWebResponse)request.GetResponse(); fiddler 是否关闭?

当 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();
        }

【问题讨论】:

  • 您是否设置了 Fiddler 来解密 HTTPS 流量?
  • 您是否也在使用自签名证书测试 localhost?
  • 现在我已经完全卸载了 fiddler,因为我无法弄清楚问题
  • 关于自签名 - 不确定它需要的步骤?如果它有效,那么它适用于任何证书 - 远程或本地。
  • 您是从管理员帐户运行此程序吗?如果是这样,如果您以普通用户身份运行,它是否有效?如果没有,请尝试将站点添加到 IE 中的受信任区域。

标签: c# https httpwebrequest fiddler http-post


【解决方案1】:

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

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

【讨论】:

  • 当我意识到提琴手以某种方式修复它时,我遇到了同样的问题,我终于破解了。正是你所说的。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    在被黑客攻击的网站上发现了一篇有趣的文章,他在使用 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; 
    

    【讨论】:

    • 您好,谢谢您 - 似乎没有解决问题,我仍然会遇到同样的异常?
    • 在 phil haacks 网站上找到一篇文章,也许值得一试。
    • 感谢戴夫试一试 - 这简直让我发疯!
    猜你喜欢
    • 2013-10-02
    • 2011-08-02
    • 1970-01-01
    • 2011-08-03
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多