我正在将数据发布到外部API(使用PHP,如果它是相关的).
我应该对我传递的POST变量进行URL编码吗?
或者我只需要对GET数据进行URL编码?
谢谢!
更新:这是我的PHP,如果它是相关的:
$fields = array(
'mediaupload'=>$file_field,
'username'=>urlencode($_POST["username"]),
'password'=>urlencode($_POST["password"]),
'latitude'=>urlencode($_POST["latitude"]),
'longitude'=>urlencode($_POST["longitude"]),
'datetime'=>urlencode($_POST["datetime"]),
'category'=>urlencode($_POST["category"]),
'metacategory'=>urlencode($_POST["metacategory"]),
'caption'=>($_POST["description"])
);
$fields_string = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
Run Code Online (Sandbox Code Playgroud) 在URL中,我应该使用%20
或编码空格+
吗?例如,在以下示例中,哪一个是正确的?
www.mydomain.com?type=xbox%20360
www.mydomain.com?type=xbox+360
Run Code Online (Sandbox Code Playgroud)
我们公司倾向于前者,但使用URLEncoder.encode(String, String)
带有"xbox 360"
(和"UTF-8"
)的Java方法返回后者.
那么,有什么区别?
我知道非标准的%uxxxx方案,但这似乎不是明智的选择,因为该方案已被W3C拒绝.
一些有趣的例子:
心中的人物.如果我在浏览器中输入:
http://www.google.com/search?q=?
Run Code Online (Sandbox Code Playgroud)
然后复制并粘贴它,我看到这个URL
http://www.google.com/search?q=%E2%99%A5
Run Code Online (Sandbox Code Playgroud)
这使得它看起来像Firefox(或Safari)正在这样做.
urllib.quote_plus(x.encode("latin-1"))
'%E2%99%A5'
Run Code Online (Sandbox Code Playgroud)
这是有道理的,除了不能用Latin-1编码的东西,比如三点字符.
…
Run Code Online (Sandbox Code Playgroud)
如果我输入URL
http://www.google.com/search?q=…
Run Code Online (Sandbox Code Playgroud)
进入我的浏览器然后复制粘贴,我明白了
http://www.google.com/search?q=%E2%80%A6
Run Code Online (Sandbox Code Playgroud)
背部.这似乎是做的结果
urllib.quote_plus(x.encode("utf-8"))
Run Code Online (Sandbox Code Playgroud)
这是有道理的,因为...不能用Latin-1编码.
但后来我不清楚浏览器是如何用UTF-8或Latin-1解码的.
因为这似乎含糊不清:
In [67]: u"…".encode('utf-8').decode('latin-1')
Out[67]: u'\xc3\xa2\xc2\x80\xc2\xa6'
Run Code Online (Sandbox Code Playgroud)
有效,所以我不知道浏览器是如何用UTF-8或Latin-1解码的.
使用我需要处理的特殊字符做什么是正确的?
如何编码查询参数以在Java中的URL上进行编码?我知道,这似乎是一个明显的问题.
我不确定有两个细微之处:
笔记:
java.net.URLEncoder.encode
似乎没有用,它似乎是为了提交表单的编码数据.例如,它+
代替空间编码空间%20
,并编码不必要的冒号.java.net.URI
不编码查询参数我正在创建一个搜索页面,您可以在其中键入搜索查询并将表单提交给search.php?query=your query
.什么PHP函数是最好的,我应该用于编码/解码搜索查询?
我相信这个问题的答案将是一个非常明显的字符编码问题......
我在命令行上使用curl来测试python应用程序中的一些端点.端点采用纬度和经度的url参数.没什么特别的.我输入命令:
curl -v -L http://localhost:5000/pulse/?lat=41.225&lon=-73.1
Run Code Online (Sandbox Code Playgroud)
服务器响应,具有详细的卷曲输出:
* Connected to localhost (127.0.0.1) port 5000 (#0)
> GET /pulse/?lat=41.225 HTTP/1.1
> User-Agent: curl/7.21.6 (i686-pc-linux-gnu) libcurl/7.21.6 OpenSSL/1.0.0e zlib/1.2.3.4 libidn/1.22 librtmp/2.3
> Host: localhost:5000
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 500 INTERNAL SERVER ERROR
< Content-Type: application/json
< Content-Length: 444
< Server: Werkzeug/0.8.1 Python/2.7.2+
< Date: Wed, 01 Feb 2012 17:06:29 GMT
<
{
"msg": "TypeError: float() argument must be a string or a number",
"flag": …
Run Code Online (Sandbox Code Playgroud) 如何将字符串中的空格转换为%20
?
这是我的尝试:
$str = "What happens here?";
echo urlencode($str);
Run Code Online (Sandbox Code Playgroud)
输出是"What+happens+here%3F"
,因此空格不表示为%20
.
我究竟做错了什么?
<a href="#/search?query={{address}}" ng-repeat="address in addresses">
{{address}}
</a>
Run Code Online (Sandbox Code Playgroud)
如果我理解正确的话,会生成非url编码的链接.编码的正确方法是#/search?query={{address}}
什么?
示例地址是1260 6th Ave, New York, NY
.
是否有内置的方法在Excel VBA中对字符串进行URL编码,还是需要手动滚动此功能?