[英]How to check a string is html encoded or not?
I am working with my web API.我正在使用我的 web API。 Some user send parameter string as HTML encoded I want to identify the coming parameters are HTML encoded or plain text.
一些用户发送参数字符串为 HTML 编码我想识别即将到来的参数是 HTML 编码或纯文本。
My Web API method is我的 Web API 方法是
public HttpResponseMessage Get(string Accesskey, string AccessPwd)
{
// code logic
}
i am getting Accesskey as Html Encoded like我将 Accesskey 设为 Html 编码如下
Accesskey="AbIfZVn4y514sJbt%2BCCMJy6lsmsMi6uOWyhUhynDSf1MEqZ%2FWJnwUuCFO8zMFa4jCRWQrXHKdBaHd9CCdDNfoKDnPg1W9No16JQbL2DBEGg%3D";
i want output like我想要 output 喜欢
Accesskey="AbIfZVn4y514sJbt+CCMJy6lsmsMi6uOWyhUhynDSf1MEqZ/WJnwUuCFO8zMFa4jCRWQrXHKdBaHd9CCdDNfoKDnPg1W9No16JQbL2DBEGg="
A technique should be to force URL decoding and check if result is the same as source string. 一种技术应该是强制URL解码并检查结果是否与源字符串相同。
using System;
using System.Web;
var incomingStrings = new[] { "not encoded", "encodéd" };
foreach (var incomingString in incomingStrings)
{
Console.WriteLine(IsUrlEncoded(incomingString));
}
private bool IsUrlEncoded(string source)
{
return source != HttpUtility.UrlDecode(source);
}
You can just use 你可以用
console.log('yourtext');
It will show as original in console. 它将在控制台中显示为原始。
I got this problem as well.我也遇到了这个问题。 My text value is not always encoded HTML, it can be just encoded text.
我的文本值并不总是编码 HTML,它可以只是编码文本。 When I use a method like
当我使用类似的方法时
HttpUtility.HtmlDecode(value)
HttpUtility.HtmlDecode(值)
it always return the same text, not decoded.它总是返回相同的文本,而不是解码。 So it is not work for me.
所以它不适合我。
To resolve it, I used a rough way by checking if the string is typically an encoded text.为了解决这个问题,我使用了一种粗略的方法来检查字符串是否通常是编码文本。
if (!string.IsNullOrEmpty(value) && value.Length % 4 == 0 && !value.Contains(" ") && !value.Contains("\t") && !value.Contains("\r") && !value.Contains("\n"))
{
// it's encoded text value
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.