You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows Form中WebBrowser控件URL解码异常求助

解决WebBrowser控件路径特殊字符解码异常问题
  • 问题核心:WebUtility.UrlDecode默认采用UTF-8编码解析,但WebBrowser返回的本地路径编码为ISO-8859-1(Latin-1),编码不匹配导致特殊字符乱码。
  • 方案一(推荐):使用HttpUtility.UrlDecode并指定ISO-8859-1编码,需先引用System.Web程序集
' 添加System.Web程序集引用后使用
Dim encodedPath As String = WebBrowser1.Url.LocalPath
Dim decodedPath As String = HttpUtility.UrlDecode(encodedPath, Encoding.GetEncoding("ISO-8859-1"))
' decodedPath 会正确输出:\\path\\Dégâts des eaux
  • 方案二(无需额外引用):手动转换编码
Dim encodedPath As String = WebBrowser1.Url.LocalPath
' 提取编码部分并转为字节数组
Dim encodedSegments As String() = encodedPath.Split("%"c)
Dim bytes As New List(Of Byte)()
For i = 1 To encodedSegments.Length - 1
    bytes.Add(Convert.ToByte(encodedSegments(i).Substring(0, 2), 16))
    ' 拼接剩余非编码部分
    If encodedSegments(i).Length > 2 Then
        bytes.AddRange(Encoding.ASCII.GetBytes(encodedSegments(i).Substring(2)))
    End If
Next
' 解码并拼接前缀
Dim decodedPath As String = encodedSegments(0) & Encoding.GetEncoding("ISO-8859-1").GetString(bytes.ToArray())

内容的提问来源于stack exchange,提问作者Jimmy Dubray

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 02:12:05