我需要对某些句点进行URL编码,因为我必须传递一些文档路径,就像这样
http://example.com/test.aspx?document=test.docx
Run Code Online (Sandbox Code Playgroud)
因此test.docx导致我出现非法字符错误.所以我需要改变它
. --> %2E
Run Code Online (Sandbox Code Playgroud)
我试图使用Server.UrlEncode
string b = Server.UrlEncode("http://example.com/test.aspx?document=test.docx");
Run Code Online (Sandbox Code Playgroud)
但我明白了
"http%3a%2f%2fexample.com%2ftest.aspx%3fdocument%3dtest.docx"
Run Code Online (Sandbox Code Playgroud)
那么我是否必须像字符串替换一样使用并手动执行并用该代码替换所有句点?
我最近(从这些 问题)了解到,在某些时候,建议在href参数中编码&符号.也就是说,而不是写:
<a href="somepage.html?x=1&y=2">...</a>
Run Code Online (Sandbox Code Playgroud)
应该写:
<a href="somepage.html?x=1&y=2">...</a>
Run Code Online (Sandbox Code Playgroud)
显然,前一个例子不起作用,但浏览器错误恢复意味着它.
我们现在已经过了严苛的XHTML要求时代.这是XHTML严格处理的要求,还是我作为Web开发人员应该注意的事实?
String百分比转义方法的签名是:
func addingPercentEncoding(withAllowedCharacters: CharacterSet)
-> String?
Run Code Online (Sandbox Code Playgroud)
(这是stringByAddingPercentEncodingWithAllowedCharacters在Swift 2中.)
为什么这个方法返回一个可选的?
文档说该方法返回nil"如果转换不可能",但不清楚在什么情况下转义转换可能会失败:
使用UTF-8转义字符,这是一个完整的Unicode编码.任何有效的Unicode字符都可以使用UTF-8进行编码,因此可以进行转义.
我想也许该方法对允许的字符集和用于转义的字符之间的不良交互应用了某种形式的检查,但事实并非如此:无论允许的字符集是否包含"%",该方法都会成功,如果允许的char集为空,也会成功.
就目前而言,非可选的返回值似乎是强制进行无意义的错误检查.
根据RFC1738,可以在URL中使用未编码的星号(*)":
因此,只有字母数字,特殊字符"$ -_.+!*'(),"和用于其保留目的的保留字符可以在URL中未编码使用.
然而,w3.org的命名和寻址材料表明,星号"保留用于在特定方案中具有特殊的重要性",并暗示它应该被编码.
此外,根据RFC3986,URL是一个URI:
术语"统一资源定位符"(URL)指的是URI的子集,其除了标识资源之外,还通过描述其主要访问机制(例如,其网络"位置")来提供定位资源的手段.
它还指定星号是"sub-delim",它是"保留集"的一部分,并且:
URI生成应用程序应对与保留集中的字符对应的数据八位字节进行百分比编码,除非URI方案明确允许这些字符表示该组件中的数据.
它还明确指定它更新RFC1738.
我将所有这些都理解为要求将星号编码在URL中,除非它们用于URI方案定义的特殊用途.
是RFC1738的HTTP URI方案标准基准?它是否以某种方式免除星号编码,或者由于RFC3986而在这方面是否过时?
维基百科说,"当没有保留目的时,他不需要对字符进行百分比编码." RFC1738是否删除了星号的保留用途?
各种资源和工具似乎在这个问题上分开了.
PHP urlencode和rawurlencode- 后者声称遵循RFC3986 - 对星号进行编码.
然而,JavaScript的escape和encodeURIComponent 不编码的星号.
并且Java URLEncoder 不对星号进行编码:
特殊字符"."," - ","*"和"_"保持不变.
流行的在线 工具(谷歌搜索"在线网址编码器"的前两个结果)也不编码星号.该URL编码和解码工具明确指出,"[T]他保留的字符只有在特定情况下进行编码." 它继续列出星号和&符作为保留字符.它编码&符号,但不编码星号.
Stack Exchange社区中的其他类似问题似乎有陈旧,不完整或难以令人信服的答案:
如何测试字符串是否为URL编码?
以下哪种方法更好?
function is_urlEncoded($string){
$test_string = $string;
while(urldecode($test_string) != $test_string){
$test_string = urldecode($test_string);
}
return (urlencode($test_string) == $string)?True:False;
}
$t = "Hello World > how are you?";
if(is_urlEncoded($sreq)){
print "Was Encoded.\n";
}else{
print "Not Encoded.\n";
print "Should be ".urlencode($sreq)."\n";
}
Run Code Online (Sandbox Code Playgroud)
上面的代码可以工作,但不是在字符串经过双重编码的情况下,如下例所示:
$t = "Hello%2BWorld%2B%253E%2Bhow%2Bare%2Byou%253F";$t = "Hello+World%2B%253E%2Bhow%2Bare%2Byou%253F";我有这个锚链接:
<a href="/question/tag/1/1/?list_id={{title}}">{{title}}</a>
Run Code Online (Sandbox Code Playgroud)
有时,这个标题有一些内容+(添加运算符),如:"Django + Python"
但是当它直接放在锚链接上时,交付的网址将是:
http://127.0.0.1:8080/question/tag/1/1/?list_id=Django + Python
Run Code Online (Sandbox Code Playgroud)
这将最终导致检索问题,因为url解码器认为list_id GET = DjangoPython.
那么,有谁知道如何避免这个问题?请注意,我不想将锚链接更改为输入按钮.
被%3B区别对待,以%3b在网址是什么?
我正在使用jQuery的$ .ajax方法向REST服务发送和检索数据.我提供给$ .ajax方法的一些URL需要空格和其他特殊字符进行编码.
问题在于Chrome,Safari(Webkit)和Internet Explorer浏览器.Firefox POST是一个URL,它被编码但其他浏览器POST到一个未编码的URL.
举个例子:
$.ajax ({
url: "http://localhost:8080/rest/123/Product Line A/[Product Type B]",
type: "POST",
dataType: "json",
data: { ... },
success: function(...){},
error: function(...){}
})
Run Code Online (Sandbox Code Playgroud)
Firefox以下列格式发布URL:
http://localhost:8080/rest/123/Product%20Line%20A/%5BProduct%20Type%20B%5D
Run Code Online (Sandbox Code Playgroud)
Chrome,Safari和IE按以下格式发送网址:
http://localhost:8080/rest/123/Product Line A/[Product Type B]
Run Code Online (Sandbox Code Playgroud)
REST服务接受编码(Firefox)格式 - 有没有一种方法可以使所有浏览器保持一致?
提前致谢!
我想使用node.js发送http请求.我做:
http = require('http');
var options = {
host: 'www.mainsms.ru',
path: '/api/mainsms/message/send?project='+project+'&sender='+sender+'&message='+message+'&recipients='+from+'&sign='+sign
};
http.get(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
}).on('error', function(e) {
console.log('ERROR: ' + e.message);
});
Run Code Online (Sandbox Code Playgroud)
我path这样的时候:
/api/mainsms/message/send?project=geoMessage&sender=gis&message=tester_response&recipients=79089145***&sign=2c4135e0f84d2c535846db17b1cec3c6
Run Code Online (Sandbox Code Playgroud)
这是工作.但是当message参数包含任何空格时,例如tester response所有空格.在控制台中我看到http使用这个网址:
/api/mainsms/message/send?project=geoMessage&sender=gis&message=tester
Run Code Online (Sandbox Code Playgroud)
如何发送空间.或者我只是不能在网址中使用空格?