1

我想向网站发送发布请求。下面的python代码工作正常。

# -*- encoding=utf-8 -*-
import urllib, urllib2

url = "http://xxx.com/login.asp"
req = urllib2.urlopen(url, urllib.urlencode({"name":u"汉字".encode('GB2312'),"id":u"12345"}))
print req.read()

但是 bash 代码返回错误的页面。这是编码问题还是其他问题?

url="http://xxx.com/login.asp"
curl --data-urlencode "name=汉字&id=12345" $url
4

1 回答 1

1
In [4]: urllib.urlencode({"name":"汉字".decode('utf-8').encode('GB2312'),"id":u"12345"})
Out[4]: 'name=%BA%BA%D7%D6&id=12345'

根据 curl 手册页,

--data-urlencode <data>
      ...
      The <data> part can be passed to
      curl using one of the following syntaxes:
      ...
      name=content
      This will make curl URL-encode the content part and pass that on.
      Note that the name part is expected to be URL-encoded already.

由于 curl 将对内容进行 URL 编码,因此我们需要向它传递一个尚未进行 URL 编码的字符串:

In [7]: urllib.unquote(urllib.urlencode({"name":"汉字".decode('utf-8').encode('GB2312'),"id":u"12345"}))
Out[7]: 'name=\xba\xba\xd7\xd6&id=12345'

因此,尝试

url="http://xxx.com/login.asp"
curl --data-urlencode 'name=\xba\xba\xd7\xd6&id=12345' $url
于 2013-04-10T07:01:21.077 回答