Page view counter
WebRequest, Firefox, AllowReadStreamBuffering and custom headers Subscribe to this thread
Last post 09-17-2008 5:03 PM by cagdasgerede. 3 replies.
Sort Posts:
06-30-2008 11:21 PM
WebRequest, Firefox, AllowReadStreamBuffering and custom headers
This is either a Silverlight bug or a specific Firefox constraint.

->The following works fine in IE.

On the server side I have a HTTP handler that just writes a string to the Response stream (within a loop). On the client side, I set AllowReadStreamBuffering to false. If I set a custom header, it seems to "disable" the AllowReadStreamBuffering since the BeginGetResponse's Callback parameter is just called after my httphandler is done (same as if I set to true).

HTTP Handler code:

    public void ProcessRequest(HttpContext context)
    {
        context.Response.Write("Start write test"+'\n');
        
        //4kb chunk for IE for test purpose... related to another possible bug in IE
        //string bigfile = File.ReadAllText("c:\\bigfile.txt");
        //context.Response.Write(bigfile);
        //context.Response.Flush();

        int nbLoop = 20;
        while (nbLoop > 0)
        {
            Thread.Sleep(2000);

            context.Response.Write("Write test : " + nbLoop + '\n');
            context.Response.Flush();
            nbLoop--;
        }

        context.ApplicationInstance.CompleteRequest();
    }

Silverlight code: (called on a button's Click event)

    private void btnGo_Click(object sender, RoutedEventArgs e)
    {
        string serverUri = HtmlPage.Document.DocumentUri.AbsoluteUri;
        serverUri = serverUri.Substring(0, serverUri.LastIndexOf("/"));
        Uri uri = new Uri(String.Format("{0}/test.test", serverUri));

        HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest;
        req.AllowReadStreamBuffering = false;

        //uncomment the following to reproduce the bug
        //req.Headers["custom-header"] = "custom";

        req.BeginGetResponse(arResponse =>
        {
            //with AllowReadStreamBuffering=false, it should step here immediately
            using (HttpWebResponse svrResp = req.EndGetResponse(arResponse) as HttpWebResponse)
            {
                using (StreamReader sr = new StreamReader(svrResp.GetResponseStream()))
                {
                    int bsize = 256;
                    Char[] buffer = new Char[bsize];
                    int nbByte = sr.Read(buffer, 0, bsize);
                    while (nbByte > 0)
                    {
                        string resultingData = new String(buffer, 0, nbByte);
                        Debug.WriteLine(resultingData);
                        nbByte = sr.Read(buffer, 0, bsize);
                    }
                }
            }
        },null);
    }


I don't know if someone could confirm this? (I can email the complete VS solution if someone is interested)

Thanks

plafond444

Loading...
Joined on 05-02-2007
Bermuda
Posts 21
09-17-2008 2:29 PM
Re: WebRequest, Firefox, AllowReadStreamBuffering and custom headers
I tried your code. I got the following exception "Error HRESULT E_FAIL has been returned from a call to a COM component".

When I set the request method to POST with
request.Method = "POST", I didn't get any exceptions and in Firefox, my silverlight app was able to receive every byte sent.

When I set the request method to POST with
request.Method = "GET", I again got an exception, due to, I believe, setting a header. So I commented out:
request.Headers["custom-header"]="value";
After that, it again worked as expected.


Do you think you are getting an exception when you invoke BeginGetResponse? Please let me know what you think.
cagdasgerede

Loading...
Joined on 09-10-2008
Posts 22
09-17-2008 2:56 PM
Re: WebRequest, Firefox, AllowReadStreamBuffering and custom headers

Ok, I'll undust it and see if I get the same result...

When you set request.Method to POST, can you see the "custom" header (name and value) on the server side?

plafond444

Loading...
Joined on 05-02-2007
Bermuda
Posts 21
09-17-2008 5:03 PM
Re: WebRequest, Firefox, AllowReadStreamBuffering and custom headers
Yes. I can read the custom header and the value; the name of the header is HTTP_[name of the header where it is capitalized and - replaced by _] - if your header name is xyz_-abc, then it becomes HTTP_XYZ__ABC.
cagdasgerede

Loading...
Joined on 09-10-2008
Posts 22
Microsoft Communities