HttpUtil.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # coding=utf-8
  2. import sys
  3. if (sys.version_info.major == 3):
  4. from urllib import parse
  5. import urllib.request as urllib2
  6. else:
  7. import urllib2
  8. class HttpUtil(object):
  9. #post方法获取数据 application/json
  10. @staticmethod
  11. def post(url, postData): #
  12. req = urllib2.Request(url, data=postData.encode('utf-8'),
  13. headers={'Content-Type': 'application/json;charset=UTF-8'})
  14. res = urllib2.urlopen(req, timeout=60).read().decode("utf-8")
  15. return res
  16. #post方法获取数据 application/x-www-form-urlencoded
  17. @staticmethod
  18. def postText(url, postData):
  19. req = urllib2.Request(url, data=postData.encode('utf-8'),
  20. headers={'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'})
  21. res = urllib2.urlopen(req, timeout=60).read().decode("utf-8")
  22. return res
  23. #get方法获取数据 text/xml
  24. @staticmethod
  25. def get(url, getData, isquote):
  26. if isquote:
  27. getData = parse.quote(getData)
  28. req = urllib2.Request((url + getData), headers={'Content-Type': 'text/xml;charset=UTF-8'})
  29. res = urllib2.urlopen(req, timeout=60)
  30. req = res.read()
  31. return req.decode()
  32. # if __name__ == '__main__':
  33. # httputil = HttpUtil()
  34. # httputil.postText()
  35. # HttpUtil.postText()