一个奇怪的问题,但让我们看看它得到了什么样的反应......
如果我编写控制台应用程序(VS 2013,.NET 4.5.1)并执行以下代码行:
Uri.EscapeUriString("[")
我明白了:
[
但是如果我Uri.EscapeUriString("[").Dump()在我的机器上的LINQPad中执行相同的操作(好吧,技术上),我会得到:
%5B
为了使事情进一步复杂化,根据这篇文章 Uri.EscapeUriString("[")确实应该返回%5B.该帖子写于2012年6月27日.
我想也许LINQPad引用的是比VS使用的更旧的DLL,但这意味着EscapeUriString最近已经发生了变化,我无法找到任何记录.有没有人对可能导致这种行为的原因有任何想法?
根据http://www.w3schools.com/tags/ref_urlencode.asp 提交请求时,它们是URL编码的,因此例如空间转换为%20.到现在为止还挺好.
我有问题!在表单中提交它会将其转换为%21.但是HttpUtility.UrlEncode(或其WebUtility合作伙伴)或 Uri.EscapeDataString所有人都将返回!这是预期的行为吗?我应该如何编码来自c#的输入,以便将其转换为正确的值?
我有一个奇怪的问题,urlencoding加号+作为针对API的请求的查询参数.API的文档说明:
日期必须采用W3C格式,例如'2016-10-24T13:33:23 + 02:00'.
到目前为止一直很好,所以我使用这个代码(minimalized)生成url,使用Spring的UriComponentBuilder:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssX");
ZonedDateTime dateTime = ZonedDateTime.now().minusDays(1);
String formated = dateTime.format(formatter);
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(baseUrl);
uriComponentsBuilder.queryParam("update", formated);
uriComponentsBuilder.build();
String url = uriComponentsBuilder.toUriString();
Run Code Online (Sandbox Code Playgroud)
未编码的查询将如下所示:
https://example.com?update=2017-01-05T12:40:44+01
Run Code Online (Sandbox Code Playgroud)
编码的字符串导致:
https://example.com?update=2017-01-05T12:40:44%2B01
Run Code Online (Sandbox Code Playgroud)
这是(恕我直言)一个正确编码的查询字符串.请参阅%2B替换查询字符串末尾的+in +01.
但是,现在,当我使用编码的url针对API发送请求时,我收到一条错误消息,指出无法处理请求.
但是,如果我在发送请求之前将其替换为%2Ba +,则可以:
url.replaceAll("%2B", "+");
Run Code Online (Sandbox Code Playgroud)
从我的理解,+标志是一个替代whitespace.因此,解码后服务器真正看到的URL必须是
https://example.com?update=2017-01-05T12:40:44 01
Run Code Online (Sandbox Code Playgroud)
我对这个假设是对的吗?
有什么我可以做的,除了联系API的所有者使其使用正确编码的查询,除了奇怪的非标准字符串替换?
更新:
根据规范RFC 3986(第3.4节),+查询参数中的符号不需要编码.
3.4.询问
查询组件包含非分层数据,与路径组件(第3.3节)中的数据一起用于标识
URI方案和命名权限
(如果有)范围内的资源.查询组件由第一个
问号("?")字符表示,并以数字符号("#")字符
或URI的末尾结束.伯纳斯 - 李等人.标准跟踪[第23页] RFC 3986 URI通用语法
2005年1月Run Code Online (Sandbox Code Playgroud)query = …
我正面临一个奇怪的问题,有时候我会从sendgrid获取网址
https://开头本地主机:81 /激活username=ats8@test.com&activationToken=EAAAAA
哪个工作正常.但有时我得到的网址编码如下,
" https:// localhost:81/Activation?username = ats8%40test.com&activationToken = EAAAAA "
我的ViewModel如下,
public class Verification
{
[DataType(DataType.EmailAddress)]
public string Username { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
[DataType(DataType.Password)]
[Compare("Password")]
public string ConfirmPassword { get; set; }
public string ActivationToken { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
方法如下,
public ActionResult Activation(string username, string activationToken)
{
var model = new Verification
{
Username = username,
ActivationToken = activationToken
};
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
在第二种情况下,activationToken为null.即使url被编码,我如何检测activationToken?
我正在尝试了解C#中满足新SMS提供程序要求的最佳编码.
我要发送的文字是:
BästeBjörn
提供商说它需要的编码文本是:
B%E4ste + BJ%F6rn
所以ä是%E4和ö是%F6
从这个答案,我得到了,为了这样的转换,我需要使用HttpUtility.HtmlAttributeEncode正常HttpUtility.UrlEncode将输出:
B%C3%a4ste + BJ%C3%b6rn
并在手机上输出奇怪的字符:/
由于几个字符没有转换,我尝试了这个:
private string specialEncoding(string text)
{
StringBuilder r = new StringBuilder();
foreach (char c in text.ToCharArray())
{
string e = System.Web.HttpUtility.UrlEncode(c.ToString());
if (e.StartsWith("%") && e.ToLower() != "%0a") // %0a == Linefeed
{
string attr = System.Web.HttpUtility.HtmlAttributeEncode(c.ToString());
r.Append(attr);
}
else
{
r.Append(e);
}
}
return r.ToString();
}
Run Code Online (Sandbox Code Playgroud)
详细,所以我可以断点并测试每个char,并发现:
System.Web.HttpUtility.HtmlAttributeEncode("ä")实际上等于ä...所以没有%E4输出... …
我有以下代码。当我检查变量 i 的值时,它是 16 个字节,但是当输出转换为 Base64 时,它是 24 个字节。
byte[] bytOut = ms.GetBuffer();
int i = 0;
for (i = 0; i < bytOut.Length; i++)
if (bytOut[i] == 0)
break;
// convert into Base64 so that the result can be used in xml
return System.Convert.ToBase64String(bytOut, 0, i);
Run Code Online (Sandbox Code Playgroud)
这是预期的吗?我正在尝试减少存储,这是我的问题之一。
我正在尝试通过以下方法打开网络浏览器。但是,当浏览器打开 url / 文件路径时,片段被损坏(从“#anchorName”到“%23anchorName”),这似乎没有得到处理。所以基本上,文件打开但不会跳转到文档中的适当位置。有谁知道如何打开文件并处理片段?对此的任何帮助将不胜感激。
打开的示例路径是“c:\MyFile.Html#middle”
// calls out to the registry to get the default browser
private static string GetDefaultBrowserPath()
{
string key = @"HTTP\shell\open\command";
using(RegistryKey registrykey = Registry.ClassesRoot.OpenSubKey(key, false))
{
return ((string)registrykey.GetValue(null, null)).Split('"')[1];
}
}
// creates a process and passes the url as an argument to the process
private static void Navigate(string url)
{
Process p = new Process();
p.StartInfo.FileName = GetDefaultBrowserPath();
p.StartInfo.Arguments = url;
p.Start();
}
Run Code Online (Sandbox Code Playgroud) 我有一个字符串,其中包含一个如下所示的网址:
http://www.test.com/images/tony的pics.jpg
我需要网址看起来像这样:
http://www.test.com/images/tony%27s%20pics.jpg
我将如何使用STRING表单中的URL以编程方式解决此问题.谢谢.
我想自动转换 UTF-8 字符â Ù á ? ?,a U a C G以便它们在 URL 中是可以接受的。
到目前为止,我有这个:
Encoding sourceEncoding = Encoding.GetEncoding(28591); // ISO-8859-1
byte[] asciiBytes = Encoding.Convert(sourceEncoding, Encoding.ASCII, sourceEncoding.GetBytes(<source text>));
String asciiString = Encoding.UTF8.GetString(asciiBytes);
Run Code Online (Sandbox Code Playgroud)
这种方法有两个问题:
有什么想法可以让我完成这项工作吗?