【问题标题】:Why is a Base64 string displayed as empty in Message Box?为什么 Base64 字符串在 Message Box 中显示为空?
【发布时间】:2019-05-28 07:24:23
【问题描述】:

我必须在提交表单之前将一些HTML源代码编码为base64格式,然后在后面的代码中将其解码回原始代码。下面是 MsgBox 的测试代码:

MsgBox(HttpContext.Current.Request.Form("encodedSourceCode"))
MsgBox(Convert.ToString(HttpContext.Current.Request.Form("encodedSourceCode").GetType()))
Dim b = Convert.FromBase64String(HttpContext.Current.Request.Form("encodedSourceCode"))
Dim html = System.Text.Encoding.UTF8.GetString(b)
MsgBox(html)

我在客户端脚本中为encodedSourceCode 添加了一个alert()

结果是:

第一个 MsgBox:空

第二个 MsgBox:“System.String”

Last MsgBox:原始 HTML 源代码

JS 警告对话框显示 base64 字符串,由一堆数字和字母组成。

简而言之,一切都很好,除了第一个 MsgBox,它应该是 base64 编码的字符串,但结果是空的。为什么?正常吗?

其实这并不重要,因为即使是最终结果(解码后)似乎也没有问题,但我只是好奇为什么中间结果没有显示为应有的结果。

【问题讨论】:

    标签: asp.net vb.net base64 msgbox


    【解决方案1】:

    我想,如果没有“可包装”字符,字符串似乎太长了。 MsgBox 删掉“最后一句话”,什么也不显示。
    这可能证实了这一点:

    dim test = HttpContext.Current.Request.Form("encodedSourceCode")
    MsgBox(test) ' empty
    test = test.Substring(0, 20)
    MsgBox(test) ' shows the first 20 characters
    

    在 LinqPad 中测试,我得到了大约 43.000 个字符的限制:

    MsgBox("".PadLeft(43000, "a"))
    MsgBox("".PadLeft(44000, "a"))
    MsgBox("".PadLeft(43000, "a") & " " & "".PadLeft(1000, "a"))
    

    第一个:显示文本。
    第二:显示空框,长度 = 44.000
    第三:显示文字,虽然总长度是44.001,但是可以在空格处换行。

    【讨论】:

    • 最后一个MsgBox的源代码也很长,但是在框内可以显示很多带有缩进的行。
    • 查看我的编辑。正如我所说,base64 字符串没有 non-word 字符,这似乎是重点。
    【解决方案2】:

    它绝对与 base64 字符串无关,因为它们是简单的字符串。证明如下:

        Dim myString = "Hello world, this is just an ɇxâmpŀƏ ʬith some non-ansi characters..."
        Dim myEncoding As Encoding = Encoding.UTF8
        MsgBox(myString)
        Dim myBase64 = Convert.ToBase64String(myEncoding.GetBytes(myString))
        MsgBox(myBase64)
        Dim myStringAgain = myEncoding.GetString(Convert.FromBase64String(myBase64))
        MsgBox(myStringAgain)
        MsgBox(If(StringComparer.Ordinal.Equals(myString, myStringAgain), "same", "different"))
    

    线

    MsgBox(Convert.ToString(HttpContext.Current.Request.Form("encodedSourceCode").GetType()))
    

    导致“System.String”,因为您将类型的名称转换为字符串(请参阅xxx.GetType())。

    【讨论】:

    • 这不是重点。我测试并提到它只是为了表明HttpContext.Current.Request.Form("encodedSourceCode")是一个简单的字符串,应该能够在MsgBox中显示。主要是为什么HttpContext.Current.Request.Form("encodedSourceCode")在解码后可以返回预期结果时为空MsgBox
    • 键“encodedSourceCode”中的一些隐藏差异?它肯定应该显示字符串。
    • 我上面引用的五行代码在我的代码中都是连续的。中间什么都没有。而且所有的结果都是直接从HTTP Request中获取数据的,所以应该是一样的。
    猜你喜欢
    • 1970-01-01
    • 2018-12-11
    • 2021-01-11
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    相关资源
    最近更新 更多