65.9K
CodeProject is changing. Read more.
Home

How to Fix Missing the ContentType Bug in Internet Explorer 10 on Windows 8 When using XDomainRequest and POST Requests

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.83/5 (3 votes)

Dec 5, 2013

CPOL
viewsIcon

15881

Fixing missing ContentType-Bug in Internet Explorer 10 on Windows 8 when using XDomainRequest and POST requests

Introduction

If you use the XDomainRequest control from Internet Explorer 10 on a Windows 8 machine, the Request-Headers don't contain the Content-Type-Header. On Internet Explorer 10 on a Windows 7 machine, it works fine.

When the Content-Type-Header is missing, ASP.NET doesn't parse the InputStream (the Post-Data from the Request) and provides an empty Request.Form collection.

Sample-Request

POST http://server/url.aspx HTTP/1.1
Accept: */*
Origin: http://remoteserver
Accept-Language: de-DE
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; WOW64; Trident/6.0; 
.NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729)
Host: server
Content-Length: 22
DNT: 1
Connection: Keep-Alive
Pragma: no-cache

para1=true&param2=test

but would be correct:

POST http://server/url.aspx HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Accept: */*
Origin: http://remoteserver
Accept-Language: de-DE
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; WOW64; Trident/6.0; 
.NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729)
Host: server
Content-Length: 22
DNT: 1
Connection: Keep-Alive
Pragma: no-cache

para1=true&param2=test 

How to Fix this Problem

To fix this problem, you can make an HttpModule or change the global.asax to perform an additional request but with Content-Type.

sub Application_BeginRequest
    Dim strResponse As String = String.Empty
    Dim oStreamReader As System.IO.StreamReader = Nothing

    Detect if retry is needed
    If String.IsNullOrEmpty(Request.ContentType) _
      and Request.InputStream.Length > 0 _
      and Request.HttpMethod = "POST" _
      Then
        oStreamReader = New System.IO.StreamReader(Request.InputStream)
        strResponse = Server.UrlDecode(oStreamReader.ReadToEnd())
        
        Dim dataStream = Encoding.UTF8.GetBytes(strResponse)
        Dim webRequest = System.Net.WebRequest.Create(request.Url)
        webRequest.Method = Request.HttpMethod
        webRequest.ContentType = "application/x-www-form-urlencoded"
        webRequest.ContentLength = dataStream.Length
        webRequest.ImpersonationLevel = 
            System.Security.Principal.TokenImpersonationLevel.Delegation
        webRequest.UseDefaultCredentials = true
        Dim newStream = webRequest.GetRequestStream()
        newStream.Write(dataStream,0,dataStream.Length)
        newStream.Close()
        Dim webResponse = webRequest.GetResponse()
    
        if webResponse isnot nothing then
            Response.Write( (new System.IO.StreamReader
                (webResponse.GetResponseStream())).ReadToEnd() )
        end if
    
        response.end()
    End If
end sub

Points of Interest

This is a dirty solution, but it works fine for me.

History

  • 05.12.2013: Posting.