个人采用python代码实现:
解决方法:
#私有方法,封装的目的是为了捕获异常
def __sendRequest(self, req, desc):
try :
context = ssl._create_unverified_context()
res = urllib2.urlopen(req, context = context) ##发送请求,接受反馈的信息
#HTTPError是URLError的子类
except urllib2.URLError, e:
if hasattr(e, 'code'):
# Only HTTPError has code attribute.
print ('The server couldn\'t fulfill the request. %s failed, error code:%s, reason:%s' % (desc, e.code, e.reason))
elif hasattr(e, 'reason'):
# HTTPError and URLError all have reason attribute.
print ('We failed to reach a server. %s failed, reason:%s' % (desc, e.reason))
return False, None
else:
# everything is fine
res = res.read() # 读取反馈的内容
print res
return True, res
记住:
如果有HEADER头域的Accept字段值,需设置为:"Accept":"*/*"