我试图与此代码发送POST查询:POST请求没有通过
def open(self, url, params):
self.__opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
c = self.__opener.open(
urllib2.Request(
url,
urllib.urlencode(params),
{"User-agent": "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"}
)
)
Class.open('http://example.com', {'username': 'test'});
但是服务器对我说该用户名字段为空。
urllib.Request('http://example.com?username=test');
它完美的工作。如何解决它?
这不是Python 3的没有'urllib2'在Python 3。而且,虽然有一个'urllib',它只是一个与它的模块封装,而不是一个名为'Request'的类。所以,首先告诉我们你使用的是哪个版本。 – abarnert
@abarnert,对不起。我试图在Python 2.7.5中解释它。 – qwerty
好的,下一个问题。在第一个版本中,您传递了'username = test'(www-form-encoded)作为正文。在第二个版本中(我假设你真的在那里使用'urllib2.Request',因为没有'urllib.Request'这样的东西?),你将它传递给查询字符串。虽然_many_ web服务会将这两者视为同一件事,但并不要求他们这样做。 – abarnert