1

基本的に私はこのコードを以下に持っています & 最後のステップ FinalStepGetReportData() は return (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

あなたが得る正確な例外は何ですか?ここでの一般的な問題は、呼び出しを忘れることです。読み終わったら、応答を閉じます。

古いバージョンのFiddlerは、デフォルトでクライアント接続を閉じていました。これにより、応答ストリーム自体を適切に閉じなかったクライアントに関するいくつかの問題が解決されました。

于 2009-06-26T04:19:49.173 に答える
0

私はまったく同じ問題を抱えています。あなたが言ったように、それを閉じずに1つの応答を残したからです。

于 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 に答える