Skip to main content

Microsoft Silverlight

Unanswered Question HttpWebRequest does not escape uriRSS Feed

(0)

Eloff
Eloff

Member

Member

139 points

114 Posts

HttpWebRequest does not escape uri

Take an escaped (url encoded) uri, pass it into System.Uri.

>>> from System import Uri
>>> u = Uri('http://foo.com/?equals=%3d')
>>> u.ToString()
'http://foo.com/?equals=='

It gets unescaped, and this seems normal .NET behavior. But HttpWebRequest actually will try to fetch the unescpaed uri (which won't work correctly in this case and many others.)

A temporary workaround is to double UrlEncode the uri before passing it to System.Uri. But I think this must indicate a bug in HttpWebRequest.

Keep up the good work, -Dan

Allen Chen – MSFT
Allen Ch...

Star

Star

13856 points

1,800 Posts

Re: HttpWebRequest does not escape uri

Hi:

  Have you tried this overload method to initialize the uri?

Uri(string uriString,bool dontEscape)

Regards

Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

mscherotter
mscherotter

Member

Member

119 points

21 Posts

Microsoft

Re: HttpWebRequest does not escape uri

Allen,

You suggested an overload that isn' t in the SL2 System.Uri class.  In .Net, that API is no obsoled: http://msdn.microsoft.com/en-us/library/9zh9wcb3.aspx

I have run into this problem as well as the web service that I am working with expects escaped information in the URI.

Michael

Michael S. Scherotter
Media Experience Evangelist
Microsoft Corp.

silverlightX64
silverli...

Member

Member

41 points

25 Posts

Re: HttpWebRequest does not escape uri

This really drives me crazy. I have to pass some variables in the querystring and it’s not working every time, even if I double escape the values.
Is there a solution?

ssawchenko
ssawchenko

Member

Member

342 points

176 Posts

Re: HttpWebRequest does not escape uri

 Was a solution ever found for this?  I have bumped into a similar issue.

mscherotter
mscherotter

Member

Member

119 points

21 Posts

Microsoft

Re: HttpWebRequest does not escape uri

double-encoding or triple-encoding was my solution.

Michael

Michael S. Scherotter
Media Experience Evangelist
Microsoft Corp.

silverlightX64
silverli...

Member

Member

41 points

25 Posts

Re: HttpWebRequest does not escape uri

My solution was more a workaround, sending the parameters in a xml string with the request.

        Dim _Page As Page = App.Current.RootVisual

        Dim _Request As New HttpHelper(_Page.GetApplicationURL & "Test.aspx", Me.Dispatcher)
        _Request.Method = "POST"
        _Request.ContentType = "text/xml"

        _Request.RequestText = "<?xml version='1.0'?>" & _
          "<Daten>" & _
          "<ID>" + ReplaceSign(Path) + "</ID>" & _
          "</Daten>"

        AddHandler _Request.RequestReady, AddressOf Me.TestCallBack
        _Request.Execute()

     Private Function ReplaceSign(ByVal FeldWert As String) As String
        FeldWert = FeldWert.Replace("&", "&amp;")
        FeldWert = FeldWert.Replace("<", "&lt;")
        FeldWert = FeldWert.Replace(">", "&gt;")

        Return FeldWert
    End Function

 On the server I read the input with this code:

        Dim objXML As XmlDocument = New XmlDocument()
        objXML.Load(Request.InputStream)

        Response.write(objXML.SelectSingleNode("Daten/ID").InnerText)

vijaydevatha
vijaydev...

Member

Member

111 points

25 Posts

Microsoft

Re: HttpWebRequest does not escape uri

I'm not able to reproduce this issue, and the fact that so many of you seem to be hitting this is making me wonder if I understand the problem to begin with.

I tried issuing a GET to each of the following URLs using HttpWebRequest (I was testing eith IE7 on Vista, using SL2.0 RTW bits):

In each case, I found that the Uris were sent across the wire as-is; i.e., HttpWebRequest does no encoding/decoding/mangling of any kind. "%3d" was sent as "%3d", not as "=". Is that not what you're seeing or expecting?

Thanks!

Vijay Devatha
Silverlight team (Microsoft)
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities