Click here to Skip to main content
15,882,152 members
Articles / Web Development / ASP.NET
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.83/5 (3 votes)
16 Dec 2013CPOL 15.6K   4  
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.

VB
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --