3

エスケープされた文字を含む文字列から新しい Uri をインスタンス化したいと思います。

Dim sUri as String = "http://foo.bar/path/to/file_with%2fpercent?query=params"
Dim uUri as New Uri(sUri)
Console.WriteLine(uUri)

>>>>

http://foo.bar/path/to/file_with/percent?query=params

どうすれば維持でき%2fますか?

4

1 に答える 1

0

たとえば、次のコードを使用します(注意:絶対URLに対してのみ機能します!):

Sub DownloadFileToDisc (downloadUrl As String)
    Dim MyWebClient As New System.Net.WebClient()
    Dim downloadUri As New UriWithCorrectDataEncoding(downloadUrl)
    MyWebClient.DownloadFile(downloadUri, localFilePath)
End Sub

''' <summary>
''' Original behaviour of System.Uri.ToString returns unescaped characters in query parameters - a behaviour which corrupts URLs like http://domain.com/?forwardurl=module%3dInvoice%26action%3dCreatePDF4Mail%26return_module%3dInvoice into http://domain.com/?forwardurl=module=Invoice&amp;action=CreatePDF4Mail&amp;return_module=Invoice
''' </summary>
''' <remarks></remarks>
Private Class UriWithCorrectDataEncoding
    Inherits Uri

    Public Sub New(url As String)
        MyBase.New(url)
    End Sub

    Public Overrides Function ToString() As String
        Return MyBase.AbsoluteUri
    End Function

End Class
于 2012-11-23T09:42:33.660 に答える