views:

2091

answers:

4

Hi :)

Basically I have this code below & the last step FinalStepGetReportData() throws an exception on return (HttpWebResponse)request.GetResponse(); if fiddler is switched off?

All else works when Fiddler is OFF except this last step. When I turn Fiddler on the final step works ?

Any ideas? I am banging my head against the wall.

Thanks


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

Found an interesting article on haacked site where he ran into problems posting form data using HTTP 1.1. Worth a try...

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;
David Liddle
hi thanks for this - doesnt seem to solve the prob I stll get the same exception ?
Found an article on phil haacks site, maybe worth giving it a try.
David Liddle
thanks dave will give it a go - this is just driving me insane !
+1  A: 

What's the exact exception that you get? A common problem here is forgettting to call .Close on the response when you're done reading it.

Older versions of Fiddler would close client connections by default, which resolved some problems with clients that didn't properly close response streams themselves.

EricLaw -MSFT-
A: 

Did you ever resolve this? I'm having the same issue... My theory is that fiddler is reformatting the request in some manner so the server accepts it. Is there any way to view the original XML before it gets to fiddler?

A: 

I have the exact same problem and It was because I left one response without closing it like you mentioned.