none
vb inet 提交数据,存储在sqlserver中,是乱码 RRS feed

  • 问题

  •  

     

    '字符串urlencode函数
    Public Function URLEncode(strInput As String) As String
        Dim strOutput As String
        Dim intAscii As Integer
        Dim i As Integer
        For i = 1 To Len(strInput)
        intAscii = Asc(Mid(strInput, i, 1))
        If ((intAscii < 58) And (intAscii > 47)) Or _
        ((intAscii < 91) And (intAscii > 64)) Or _
        ((intAscii < 123) And (intAscii > 96)) Then
        strOutput = strOutput & Chr$(intAscii)
        Else
        strOutput = strOutput & _
        IIf(intAscii < 16, "%0", "%") & _
        Trim$(Hex$(intAscii))
        End If
        Next
        URLEncode = strOutput
    End Function

     

    用这个函数将数据转换后,提交到utf-8的网页中,再由网页接受,存储到数据库中,数据库中,汉字都变成了乱码

     

    我该怎么解决?

     

     

    想在读数据的时候做转化,但是用adodb.stream在网页中读数据库的时候,设置了charset="utf-8",依然解决不了这个问题

    2008年11月15日 14:18

答案

  • 转陈Byte数组,直接传这个数组

    Code Snippet

     

    Private Const CP_UTF8 = 65001   ' default to UTF-8 code page

     

    Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long

     

    Public Function EncodeToBytes(ByVal sData As String) As Byte()

        Dim aRetn() As Byte
        Dim nSize As Long
       
        nSize = WideCharToMultiByte(CP_UTF8, 0, StrPtr(sData), -1, 0, 0, 0, 0)
        ReDim aRetn(0 To nSize - 1) As Byte
        WideCharToMultiByte CP_UTF8, 0, StrPtr(sData), -1, VarPtr(aRetn(0)), nSize, 0, 0
       
        EncodeToBytes = aRetn
    End Function

     

     

    2008年11月16日 6:24
    版主

全部回复

  • 你这个函数就是进行编码的,不要这个函数就可以了

    2008年11月16日 6:17
    版主
  • 转陈Byte数组,直接传这个数组

    Code Snippet

     

    Private Const CP_UTF8 = 65001   ' default to UTF-8 code page

     

    Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long

     

    Public Function EncodeToBytes(ByVal sData As String) As Byte()

        Dim aRetn() As Byte
        Dim nSize As Long
       
        nSize = WideCharToMultiByte(CP_UTF8, 0, StrPtr(sData), -1, 0, 0, 0, 0)
        ReDim aRetn(0 To nSize - 1) As Byte
        WideCharToMultiByte CP_UTF8, 0, StrPtr(sData), -1, VarPtr(aRetn(0)), nSize, 0, 0
       
        EncodeToBytes = aRetn
    End Function

     

     

    2008年11月16日 6:24
    版主
  •  

    感谢两位大版主:但是问题还是没解决!

     

    现在将源代码写在下面,请各位版主再次给予指导!期待你们的指导!!!

     

    'xml格式存字符串发送

    Text2  = Text1.text

     

     XmlMsg = "<?xml version=""1.0"" encoding=""utf-8"" ?>" & vbNewLine
        XmlMsg = XmlMsg & "<message>" & vbNewLine
        XmlMsg = XmlMsg & "<msgtype>chatmsg</msgtype>" & vbNewLine
        XmlMsg = XmlMsg & "<mastername>" & Pub_Mastername & "</mastername>" & vbNewLine
        XmlMsg = XmlMsg & "<description><![CDATA[ " & Text2 & " ]]></description>" & vbNewLine
        XmlMsg = XmlMsg & "</message>" & vbNewLine
       
        Postdata = "touserid=" & Touserid & "&content=" & URLEncode(XmlMsg)
        Call Post(Postdata)

     

    'post函数如下

    Private Sub Post(ByVal Postdata As String)
        On Error Resume Next
        Doing = Frm_main.Inet3.StillExecuting
        Do While Doing
            Doing = Frm_main.Inet3.StillExecuting
            DoEvents
        Loop
        'url=http://127.0.0.1/x.asp  x.asp为utf-8编码
        Frm_main.Inet3.Execute Url, "Post", Postdata, "Content-Type: application/x-www-form-urlencoded"

    End Sub

     

    '字符串urlencode函数
    Public Function URLEncode(strInput As String) As String
        Dim strOutput As String
        Dim intAscii As Integer
        Dim i As Integer
        For i = 1 To Len(strInput)
        intAscii = Asc(Mid(strInput, i, 1))
        If ((intAscii < 58) And (intAscii > 47)) Or _
        ((intAscii < 91) And (intAscii > 64)) Or _
        ((intAscii < 123) And (intAscii > 96)) Then
        strOutput = strOutput & Chr$(intAscii)
        Else
        strOutput = strOutput & _
        IIf(intAscii < 16, "%0", "%") & _
        Trim$(Hex$(intAscii))
        End If
        Next
        URLEncode = strOutput
    End Function

     

    如果不用urlencode编码,我的空格字符等等,发送不过去。

     

     

    我把带有汉字的发给x.asp,x.asp存储到数据库中汉字你好变成了 “ 4E3AC3” 这个样子“特殊符号4E3 特殊符号AC3”,特殊符号,粘贴不到这里

     

     

    其他都很正常:如下:

     

    <?xmlversion="1.0"encoding="utf-8"?><message><msgtype>chatmsg</msgtype><mastername>yezhuan</mastername><description><![CDATA[ 4E3AC3]]></description></message>

     

     

     

     

    2008年11月16日 8:32
  • 谢谢,已经解决了

    2008年11月16日 10:33