gpt4 book ai didi

java - 是否有 C# 的 HttpServerUtility.UrlTokenEncode 的 Java 等效项?

转载 作者:太空宇宙 更新时间:2023-11-03 15:27:41 24 4
gpt4 key购买 nike

如何使用 HttpServerUtility.UrlTokenDecode 在 Java 中编码将在 C# 中解码的字符串?

最佳答案

以下方法复制了 C# 功能。

public static String urlTokenEncode(String inputString) {
if (inputString == null) {
return null;
}
if (inputString.length() < 1) {
return null;
}

// Step 1: Do a Base64 encoding
String base64Str = new String(java.util.Base64.getEncoder().encode(inputString.getBytes()));

// Step 2: Transform the "+" to "-", and "/" to "_"
base64Str = base64Str.replace('+', '-').replace('/', '_');

// Step 3: Find how many padding chars are present at the end
int endPos = base64Str.lastIndexOf('=');
char paddingChars = (char)((int)'0' + base64Str.length() - endPos);

// Step 4: Replace padding chars with count of padding chars
char[] base64StrChars = base64Str.toCharArray();
base64StrChars[endPos] = paddingChars;

return new String(base64StrChars);
}

关于java - 是否有 C# 的 HttpServerUtility.UrlTokenEncode 的 Java 等效项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34613257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com