zoukankan      html  css  js  c++  java
  • UE4笔记-http请求带中文字符串的使用问题记录(encodeURI/UriEncode)

    类似JavaScript 的 encodeURI功能...

    UE4使用IHttpRequest请求时 当Uri 路径中带中文字符时,需要进行百分比编码,否则无法正确解析Url路径和参数:

    FString temp = FGenericPlatformHttp::UrlEncode(queryStr);
    FString uri = FString::Printf(TEXT("http://www.yoursite.com?QueryString=%s"),*temp);

    Note:

    截至UE4.23,FGenericPlatformHttp::UrlEncode 函数不能解析整条http url.(因为UE4不会忽略’:‘,'/','@'以及'#' 等字符串)

    如果需要整条字符串一起处理,可以考虑自己复制扩展或修改一下源码:

    源码:

    GenericPlatformHttp.cpp中修改AllowedChars 字符串 添加 ‘#’,‘/’,‘:’,'@'等符号进行忽略.

    或直接复制扩展,如:

    static bool IsAllowedChar(UTF8CHAR LookupChar)
    {
        static char AllowedChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+=_.~:/#@?&";
        static bool bTableFilled = false;
        static bool AllowedTable[256] = { false };
    
        if (!bTableFilled)
        {
            for (int32 Idx = 0; Idx < ARRAY_COUNT(AllowedChars) - 1; ++Idx)    // -1 to avoid trailing 0
            {
                uint8 AllowedCharIdx = static_cast<uint8>(AllowedChars[Idx]);
                check(AllowedCharIdx < ARRAY_COUNT(AllowedTable));
                AllowedTable[AllowedCharIdx] = true;
            }
    
            bTableFilled = true;
        }
    
        return AllowedTable[LookupChar];
    }
    
    FString UCoreBPLibrary::UrlEncode( const FString &UnencodedString)
    {
        FTCHARToUTF8 Converter(*UnencodedString);    //url encoding must be encoded over each utf-8 byte
        const UTF8CHAR* UTF8Data = (UTF8CHAR*)Converter.Get();    //converter uses ANSI instead of UTF8CHAR - not sure why - but other code seems to just do this cast. In this case it really doesn't matter
        FString EncodedString = TEXT("");
    
        TCHAR Buffer[2] = { 0, 0 };
    
        for (int32 ByteIdx = 0, Length = Converter.Length(); ByteIdx < Length; ++ByteIdx)
        {
            UTF8CHAR ByteToEncode = UTF8Data[ByteIdx];
    
            if (IsAllowedChar(ByteToEncode))
            {
                Buffer[0] = ByteToEncode;
                FString TmpString = Buffer;
                EncodedString += TmpString;
            }
            else if (ByteToEncode != '')
            {
                EncodedString += TEXT("%");
                EncodedString += FString::Printf(TEXT("%.2X"), ByteToEncode);
            }
        }
        return EncodedString;
    }
  • 相关阅读:
    微信小程序传值
    tp查询中2个表格中字段,比较大小
    isNaN与parseInt/parseFloat
    编程技巧之表格驱动编程
    RGB
    矩形重叠检测。
    经验搜索排名---google已经做过类似的了(我想多了)
    有关编程语言的认识
    Nodepad++ 资料整理
    lower()
  • 原文地址:https://www.cnblogs.com/linqing/p/5321317.html
Copyright © 2011-2022 走看看