是否有一个Java相当于C#的HttpServerUtility.UrlTokenDecode? [英] Is there a Java equivalent for C#'s HttpServerUtility.UrlTokenDecode?
本文介绍了是否有一个Java相当于C#的HttpServerUtility.UrlTokenDecode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何在Java中的解码用在C#编码的字符串HttpServerUtility.UrlTokenEncode? P>
解决方案
我试着用 org.apache.commons.codec.binary.Base64
(该构造函数接受一个参数,说明编码/解码是否安全网址),但原来它没有实现同。UrlTokenEncode /解码
我结束了在C#实现迁移到Java的:
公共静态的byte [] UrlTokenDecode(字符串输入){
如果(输入== NULL)
返回新的字节[0];
INT LEN = input.length();
如果(LEN< 1)
返回新的字节[0];
////////////////////////////////////////// /////////////////////////
//第1步:计算填充字符数追加到该字符串。
//填充字符要追加的号被存储在字符串的最后一个字符。
INT numPadChars =(int)的input.charAt(LEN - 1) - (INT)0;
如果(numPadChars℃,|| numPadChars→10)
返回NULL;
////////////////////////////////////// /////////////////////////////
//第2步:创建数组存储的字符(不包括最后一个字符)
//和填充字符
的char [] = base64Chars新的char [len个 - 1 + numPadChars];
////////////////////////////////////// //////////////////
//第3步:在字符复制。变换 - 到+和*为/
为(INT ITER = 0; ITER< LEN - 1; ITER ++){$ B $ B焦炭C = input.charAt( ITER);
开关(三){
的情况下 - :
base64Chars [ITER] ='+';
中断;
案_:
base64Chars [国际热核实验堆] ='/';
中断;
默认:
base64Chars [国际热核实验堆] = C;
中断;
}
}
//////////////////////////////// ////////////////////////
//第4步:添加填充字符
为(INT ITER = LEN - 1; ITER < base64Chars.length; ITER ++){$ b $ b base64Chars [国际热核实验堆] ='=';
}
//执行实际的转换
字符串assembledString = String.copyValueOf(base64Chars);
返回Base64.decodeBase64(assembledString);
}
How do I decode in Java a string that was encoded in C# using HttpServerUtility.UrlTokenEncode?
解决方案
I tried using org.apache.commons.codec.binary.Base64
(The ctor accepts a parameter stating whether the encoding/decoding is url-safe) but turns out it is not implemented the same as UrlTokenEncode/Decode.
I ended up migrating the C# implementation to Java:
public static byte[] UrlTokenDecode(String input) {
if (input == null)
return new byte[0];
int len = input.length();
if (len < 1)
return new byte[0];
///////////////////////////////////////////////////////////////////
// Step 1: Calculate the number of padding chars to append to this string.
// The number of padding chars to append is stored in the last char of the string.
int numPadChars = (int)input.charAt(len - 1) - (int)'0';
if (numPadChars < 0 || numPadChars > 10)
return null;
///////////////////////////////////////////////////////////////////
// Step 2: Create array to store the chars (not including the last char)
// and the padding chars
char[] base64Chars = new char[len - 1 + numPadChars];
////////////////////////////////////////////////////////
// Step 3: Copy in the chars. Transform the "-" to "+", and "*" to "/"
for (int iter = 0; iter < len - 1; iter++) {
char c = input.charAt(iter);
switch (c) {
case '-':
base64Chars[iter] = '+';
break;
case '_':
base64Chars[iter] = '/';
break;
default:
base64Chars[iter] = c;
break;
}
}
////////////////////////////////////////////////////////
// Step 4: Add padding chars
for (int iter = len - 1; iter < base64Chars.length; iter++) {
base64Chars[iter] = '=';
}
// Do the actual conversion
String assembledString = String.copyValueOf(base64Chars);
return Base64.decodeBase64(assembledString);
}
这篇关于是否有一个Java相当于C#的HttpServerUtility.UrlTokenDecode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文