简体   繁体   English

urlencode给出HTTP错误403:禁止

[英]urlencode gives HTTP Error 403: FORBIDDEN

callurl = "http://vgintnh116:8001/master_data/"
params = urllib.urlencode({'res': 'arovit', 'qfields': 'prod' })
f = urllib2.urlopen(callurl, params)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/u/vgtools2/python-2.6.5/lib/python2.6/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/u/vgtools2/python-2.6.5/lib/python2.6/urllib2.py", line 397, in open
    response = meth(req, response)
  File "/u/vgtools2/python-2.6.5/lib/python2.6/urllib2.py", line 510, in http_response
    'http', request, response, code, msg, hdrs)
  File "/u/vgtools2/python-2.6.5/lib/python2.6/urllib2.py", line 435, in error
    return self._call_chain(*args)
  File "/u/vgtools2/python-2.6.5/lib/python2.6/urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "/u/vgtools2/python-2.6.5/lib/python2.6/urllib2.py", line 518, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: FORBIDDEN


But it works with - 
callurl = "http://vgintnh116:8001/master_data/res=arovit&qfields=prod"
f = urllib2.urlopen(callurl)

Please help. 请帮忙。 I want to use urlencode to avoid handling spaces and extra characters. 我想使用urlencode以避免处理空格和多余的字符。

If you pass the second argument ( data ), request will be POST instead of GET. 如果您传递第二个参数( data ),则请求将是POST而不是GET。

Also, dictionaries in Python does not have order. 另外,Python中的字典没有顺序。 To guarantee the order, you should use sequence. 为了保证顺序,您应该使用顺序。

callurl = "http://vgintnh116:8001/master_data/"
params = urllib.urlencode([('res', 'arovit'), ('qfields', 'prod')])
f = urllib2.urlopen(callurl + params)

From urllib2 documentation : urllib2文档中

the HTTP request will be a POST instead of a GET when the data parameter is provided 提供data参数时,HTTP请求将是POST而不是GET

In your working example, you are making a GET request. 在您的工作示例中,您正在执行GET请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM