http_build_query()根据某些RFC标准,有没有一种方法可以使用URL编码?
为什么我不想对所有内容进行URL编码:我正在查询Ebay API ..他们诚实地坚持参数名称不是URL编码,就括号中的逗号而言.例如,DomainName(0)是一个参数,如果这些parens被编码,则查询失败.
我想发送一个URI作为查询/矩阵参数的值.在我将它附加到现有URI之前,我需要根据RFC 2396对其进行编码.例如,给定输入:
http://google.com/resource?key=value1 & value2
我期待输出:
http%3a%2f%2fgoogle.com%2fresource%3fkey%3dvalue1%2520%26%2520value2
既java.net.URLEncoder不会也java.net.URI不会产生正确的输出.URLEncoder用于HTML格式编码,与RFC 2396不同.URI没有一次编码单个值的机制,因此无法知道value1和value2是同一个键的一部分.
我的地图是:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with params
new { controller = "Home", action = "Index", id = "" } // Param defaults
);
Run Code Online (Sandbox Code Playgroud)
如果我使用URL http://localhost:5000/Home/About/100%2f200,则没有匹配的路由.我将URL更改为http://localhost:5000/Home/About/100然后再次匹配路由.
有没有简单的方法来处理包含斜杠的参数?其他转义值(空格%20)似乎有效.
编辑:
编码Base64对我有用.它使URL变得丑陋,但现在还可以.
public class UrlEncoder
{
public string URLDecode(string decode)
{
if (decode == null) return null;
if (decode.StartsWith("="))
{
return FromBase64(decode.TrimStart('='));
}
else
{
return HttpUtility.UrlDecode( decode) ;
}
}
public string UrlEncode(string encode)
{
if (encode == null) return null;
string encoded = …Run Code Online (Sandbox Code Playgroud) 下面的URL链接将打开一个新的Google邮件窗口.我遇到的问题是Google用空格替换了电子邮件正文中的所有加号(+).看起来它只发生在+符号上.有关如何解决这个问题的任何建议?(我正在使用ASP.NET网页)
https://mail.google.com/mail?view=cm&tf=0&to=someemail@somedomain.com&su=some subject&body =你好+那里你好
(在正文电子邮件中,"你好+你好那里"将显示为"你好你好那里")
非常奇怪的错误.我使用gide http://developers.facebook.com/docs/authentication/.所以我创建了对fb的请求并传递了redirect_uri.我在localhost上使用测试站点.所以,如果我通过
redirect_uri = http://localhost/test_blog/index.php
它工作正常,但如果我通过
redirect_uri = http://localhost/test_blog/index.php?r = site/oauth2
它不想要工作.我尝试使用
redirect_uri =.urlencode(' http://localhost/test_blog/index.php?r = site/oauth2)
但不行.我尝试解释.我成功获得代码,但当我访问https://graph.facebook.com/me?access_token时,我收到错误'错误验证验证码'.我检查过,错误是在?r = site/oauth2但我需要传递一些参数可以有人帮助我吗?我读了http://forum.developers.facebook.net/viewtopic.php?id=70855的帖子,但对我来说没什么用
我正在编写一个Web应用程序并学习如何urlencode html链接...
这里的所有urlencode问题(见下面的标签)都是"如何...?" 的问题.
我的问题不是"如何?" 但为什么?".
甚至维基百科文章也只讨论它的机制:
http://en.wikipedia.org/wiki/Urlencode,
但不是为什么我应该在我的应用程序中使用urlencode.
什么是安全使用(或者更确切地说,不使用)进行urlencode的含义是什么?
如何使用进行urlencode失败被利用?
使用未编码的网址会出现什么样的错误或失败?
我问,因为即使没有urlencode,我的应用程序开发网站的链接,如下所示按预期工作:
http://myapp/my%20test/ée/ràé
我为什么要使用urlencode?
或另一种说法:
我什么时候应该使用urlencode?在什么样的情况下?
在Groovy中有一种URLEncode吗?
我找不到任何字符串?String URL编码实用程序.
示例:dehydrogenase (NADP+)?dehydrogenase%20(NADP%2b)
(+而不是%20也可以接受,因为一些实现这样做)
如果我有一个像这样的对象:
d = {'a':1, 'en': 'hello'}
Run Code Online (Sandbox Code Playgroud)
...然后我可以把它传递给urllib.urlencode,没问题:
percent_escaped = urlencode(d)
print percent_escaped
Run Code Online (Sandbox Code Playgroud)
但是如果我尝试传递一个值为type的对象unicode,游戏结束:
d2 = {'a':1, 'en': 'hello', 'pt': u'olá'}
percent_escaped = urlencode(d2)
print percent_escaped # This fails with a UnicodeEncodingError
Run Code Online (Sandbox Code Playgroud)
所以我的问题是关于准备传递给对象的可靠方法urlencode.
我想出了这个函数,我只是遍历对象并编码string或unicode类型的值:
def encode_object(object):
for k,v in object.items():
if type(v) in (str, unicode):
object[k] = v.encode('utf-8')
return object
Run Code Online (Sandbox Code Playgroud)
这似乎有效:
d2 = {'a':1, 'en': 'hello', 'pt': u'olá'}
percent_escaped = urlencode(encode_object(d2))
print percent_escaped
Run Code Online (Sandbox Code Playgroud)
那个输出a=1&en=hello&pt=%C3%B3la,准备好传递给POST电话或其他什么.
但我的encode_object功能对我来说真的很不稳定.首先,它不处理嵌套对象.
另一方面,如果声明,我会很紧张.我还应该考虑其他任何类型吗?
并且正在将这些type()东西与本地对象进行比较,就像这个好习惯一样?
type(v) in …Run Code Online (Sandbox Code Playgroud) 我在使用某些字符编码时遇到了一些麻烦,导致我网站上的搜索表单出现问题.其中一个可能的字段值中包含一个&符号.选择此选项并提交搜索时,&符编码为:%2526
使用页面底部的分页链接并导航到结果列表中的第二页时,&符编码为: %26
最后......在尝试导航到列表中的第3页时,&符号被更改回:&这会打破表单,因为它假设有一个实际上不存在的变量.
为什么编码会改变?我怎样才能解决这个问题?谢谢你的帮助!