python3.65 urllib.request.Request()增加header报错 5C

coding:utf-8

import urllib.request
import urllib.parse

url = 'http://192.168.**.**:9080/api/transactions'
header = {
'Content-Type': 'application/json'
}
values ={
"currentToken": {
"simplifiedName": "ETH",
"address": "0x5bcd4d0508bc86c48760d0805962261d260d7a88"
},
"txid": ""
}
data = urllib.parse.urlencode(values)
data = data.encode(encoding='UTF-8')
request = urllib.request.Request(url, data, header)
#request = urllib.request.Request(url, data)
print("111")
html = urllib.request.urlopen(request)
print("222")
html =html.read().decode('utf-8')
print(html)

#返回结果 报错
D:\tool\Python36\python.exe D:/Users/Administrator/PycharmProjects/coinPlatform/test/test6.py
111
Traceback (most recent call last):
File "D:/Users/Administrator/PycharmProjects/coinPlatform/test/test6.py", line 21, in
html = urllib.request.urlopen(request)
File "D:\tool\Python36\lib\urllib\request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "D:\tool\Python36\lib\urllib\request.py", line 532, in open
response = meth(req, response)
File "D:\tool\Python36\lib\urllib\request.py", line 642, in http_response
'http', request, response, code, msg, hdrs)
File "D:\tool\Python36\lib\urllib\request.py", line 570, in error
return self._call_chain(*args)
File "D:\tool\Python36\lib\urllib\request.py", line 504, in _call_chain
result = func(*args)
File "D:\tool\Python36\lib\urllib\request.py", line 650, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found

Process finished with exit code 1

coding:utf-8

import urllib.request
import urllib.parse

url = 'http://192.168.**.**:9080/api/transactions'
header = {
'Content-Type': 'application/json'
}
values ={
"currentToken": {
"simplifiedName": "ETH",
"address": "0x5bcd4d0508bc86c48760d0805962261d260d7a88"
},
"txid": ""
}
data = urllib.parse.urlencode(values)
data = data.encode(encoding='UTF-8')
#request = urllib.request.Request(url, data, header)
request = urllib.request.Request(url, data)
print("111")
html = urllib.request.urlopen(request)
print("222")
html =html.read().decode('utf-8')
print(html)

返回结果 成功 但没得到想要的结果
D:\tool\Python36\python.exe D:/Users/Administrator/PycharmProjects/coinPlatform/test/test6.py
111
222
{"code":0,"message":"success","data":{"currentToken":{},"transactions":[]}}

Process finished with exit code 0

4个回答

这个要看你的服务器是怎么返回的数据,是不是你提交的数据有不符合的地方,所以索然返回成功了,但是没有达到效果

改成headers=header,试试!

18.05.08 18:16
这个要看你的服务器是怎么返回的数据,是不是你提交的数据有不符合的地方,所以索然返回成功了,但是没有达到效果

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问