gpt4 book ai didi

.net - 保留转义字符的新 Uri

转载 作者:行者123 更新时间:2023-12-04 20:02:27 26 4
gpt4 key购买 nike

我想从包含转义字符的字符串中实例化一个新的 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

最佳答案

使用例如以下代码(注意:仅适用于绝对 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

关于.net - 保留转义字符的新 Uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10084175/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com