Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP解析C#urltokencode字符串_C#_Php_Urlencode_Url Encoding - Fatal编程技术网

用PHP解析C#urltokencode字符串

用PHP解析C#urltokencode字符串,c#,php,urlencode,url-encoding,C#,Php,Urlencode,Url Encoding,我有以下c#代码,它使用URLTokencode将图像转换为base64编码字符串: byte[] imageArray = System.IO.File.ReadAllBytes(note_path); string base64ImageRepresentation = HttpServerUtility.UrlTokenEncode(imageArray); byte[] data = Encoding.UTF8.GetBytes($"id

我有以下c#代码,它使用URLTokencode将图像转换为base64编码字符串:

        byte[] imageArray = System.IO.File.ReadAllBytes(note_path);
        string base64ImageRepresentation = HttpServerUtility.UrlTokenEncode(imageArray);

        byte[] data = Encoding.UTF8.GetBytes($"id={id}&apikey={apikey}&friend_ids={friend_ids}&ttl={ttl}&note={base64ImageRepresentation}");

        Console.Write(sendRequest("upload-note", data));
        Console.ReadLine();
我将其发送到lamp服务器,通过php进行解析。显然,这不起作用,因为URLTokencode如何使用
-
表示
+
-
表示
/
,以及整数表示填充等于符号。我有以下代码来克服这个问题:

                $request->note = str_replace('-', '+', str_replace('_', '/', $request->note));
                $lastCharacter = substr($request->note, -1);
                if($lastCharacter == 1 || $lastCharacter == 2){
                    $request->note = substr($request->note, 0, -$lastCharacter);
                    if($lastCharacter == 1){
                        $request->note = $request->note . '=';
                    } else {
                        $request->note = $request->note . '==';
                    }
                }
我对这个过程的关注来自于后面的等号。我读到只有1或2个等号表示填充,但有没有可能出现没有填充的情况?如果这是可能的,那么字符串的最后一个字符可能是任意随机整数值,对吗?如果这是真的,那么我当前的php解码过程将不起作用,因为它假设最后一个字符总是1或2,并且该字符将表示所需的等号数


如果我的过程不起作用,有没有更好的方法用PHP解码c#UrlTokenEncode字符串?

来回答我的上述问题:

  • base64编码字符串的末尾将只有0、1或2个等号用于填充
  • 若并没有填充,那个么0将被追加到base64字符串
  • 我创建了以下函数,以将通过c#的URLTokencode方法创建的base64字符串转换回常规base64字符串,并将常规base64字符串转换为UrlTokenDecode期望的格式

    function convertToUrlTokenFormat($val){
    
        $padding = substr_count($val, '=');
        $val = str_replace('=', '', $val);
        $val .= $padding;
        $val = str_replace('+', '-', str_replace('/', '_', $val));
    
        return $val;
    
    }
    
    function convertFromUrlTokenFormat($val){
    
        $val = str_replace('-', '+', str_replace('_', '/', $val));
        $lastCharacter = substr($val, -1);
        $val = substr($val, 0, -1);
        switch($lastCharacter){
            case 1:
                $val = $val . "=";
                break;
            case 2:
                $val = $val . "==";
                break;
        }
    
        return $val;
    
    }