引用 Microsoft Activex Server Pages Object Libaray
即可以使用此函数

解决方案 »

  1.   

    ASP中的Server.URLEncode在VC中的实现一. 事情的起因:
    从我的VC组件中试图用ServerXMLHTTP对象向服务器端的ASP页面发送一个QueryString,它的值为联系人的名字,这是中文字符,如:“http://localhost/tester/test.asp?username=郑昀”。希望ASP页面接收到的仍然是正常的中文字符,而不会是乱码。
    大家知道,ServerXMLHTTP对象在作HTTP操作之前,会先将URL转换为UTF8编码。而IIS接收时,会自动将这个UTF8编码的URL转换为Unicode编码,但是它有时会错误地丢掉奇数UTF8编码字符串的最后一个字节,所以得到就会是乱码。这个问题的描述可以参看《IIS是如何接收ServerXMLHTTP传过来的编码字符》。
    这样,对于我们的情况,我们希望能够在用SXH对象之前将URL字符串Encode一下。这样的做法在ASP中是通过Server.URLEncode来做的。
    下面我们就给出Visual C++中如何做到这一点的。(注意:这需要MFC。以后有空时,我们会给出STL实现的URLEncode)
    二. URLEncode的代码:
    inline BYTE toHex(const BYTE &x)
    {
    return x > 9 ? x + 55: x + 48;
    }
    CString URLEncode(CString sIn)
    {
    CString sOut;

    const int nLen = sIn.GetLength() + 1; register LPBYTE pOutTmp = NULL;
    LPBYTE pOutBuf = NULL;
    register LPBYTE pInTmp = NULL;
    LPBYTE pInBuf =(LPBYTE)sIn.GetBuffer(nLen);
    BYTE b = 0;

    //alloc out buffer
    pOutBuf = (LPBYTE)sOut.GetBuffer(nLen*3 - 2);//new BYTE [nLen  * 3]; if(pOutBuf)
    {
    pInTmp = pInBuf;
    pOutTmp = pOutBuf;

    // do encoding
    while (*pInTmp)
    {
    if(isalnum(*pInTmp))
    *pOutTmp++ = *pInTmp;
    else
    if(isspace(*pInTmp))
    *pOutTmp++ = '+';
    else
    {
    *pOutTmp++ = '%';
    *pOutTmp++ = toHex(*pInTmp>>4);
    *pOutTmp++ = toHex(*pInTmp%16);
    }
    pInTmp++;
    }
    *pOutTmp = '\0';
    //sOut=pOutBuf;
    //delete [] pOutBuf;
    sOut.ReleaseBuffer();
    }
    sIn.ReleaseBuffer();
    return sOut;
    }
    三.  测试代码:
    CString strUnEncodeLinkTo("Globalhelp.xml?username=郑昀");
    CString strLinkTo = URLEncode(strUnEncodeLinkTo);
    // strLinkTo的结果是:
    // "Globalhelp%2Exml%3Fusername%3D%D6%A3%EA%C0"四. 说明:
    这样,经URLEncode转换之后的URL,被IIS接收时,用QueryString(“username”)得到的就会是正确的中文字符了。
    请再看一下,下面这种情况:
    ASP的代码为:
    1 Dim strURL
    2 strURL = "郑昀" 
    3 strURL = Server.URLEncode(strURL)
    4 strURL = "http://localhost/tester/Receiver.asp?name=" & strURL
    5 xmlhttp.setOption(0)  = 936  
    6 xmlhttp.Open "POST",strURL,false
    7 xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    8 xmlhttp.send
    第三行代码把中文字符Encode了一下。所以strURL就变为了“%D6%A3%EA%C0”。
    第五行代码的意思是,设置SXH对象的SXH_OPTION_URL_CODEPAGE的值为936,即GB2312。(而这个选项的缺省值为CP_UTF8,这就是为什么会将你的Unicode的URL字符串转换为UTF8的原因。)
    现在我们把它设置为GB2312这种codepage。会出现什么结果呢?接收的ASP页面上是这么做的:
    1 Dim value
    2 value = Request.QueryString("name")
    这个value的值仍然会是“%D6%A3%EA%C0”,这就是设置codepage为GB2312的结果。如果将上面代码的第五行注释掉,那么这里的value就会是“郑昀”。
    所以说SXH.setOption是可以控制转换用的codepage的。
      

  2.   

    我用了你的代码,
          但报错
          
          --------------------Configuration:    test    -    Win32    Debug--------------------
          Compiling...
          testDlg.cpp
          F:\programme\myself\test\testDlg.cpp(99)    :    error    C2601:    'toHex'    :    local    function    definitions    are    illegal
          F:\programme\myself\test\testDlg.cpp(103)    :    error    C2601:    'URLEncode'    :    local    function    definitions    are    illegal
          Error    executing    cl.exe.
          
          test.exe    -    2    error(s),    0    warning(s)
          
          
          
          为什么?
      

  3.   

    这个错误的详细解释:
    Compiler Error C2601
    'function name' : local function definitions are illegalAn attempt was made to define a function within a function.The following is an example that will generate this error:int main()
    {
       int i = 0;   int funcname(int j)
       {
          j++;
          return j;
       }
       
       i = funcname(i);
       return 0;
    }If you move the function into the global space, outside the main() function, the program will compile and run.
    请你看一看是否代码有误?