# coding=utf-8 import sys if (sys.version_info.major == 3): from urllib import parse import urllib.request as urllib2 else: import urllib2 class HttpUtil(object): #post方法获取数据 application/json @staticmethod def post(url, postData): # req = urllib2.Request(url, data=postData.encode('utf-8'), headers={'Content-Type': 'application/json;charset=UTF-8'}) res = urllib2.urlopen(req, timeout=60).read().decode("utf-8") return res #post方法获取数据 application/x-www-form-urlencoded @staticmethod def postText(url, postData): req = urllib2.Request(url, data=postData.encode('utf-8'), headers={'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}) res = urllib2.urlopen(req, timeout=60).read().decode("utf-8") return res #get方法获取数据 text/xml @staticmethod def get(url, getData, isquote): if isquote: getData = parse.quote(getData) req = urllib2.Request((url + getData), headers={'Content-Type': 'text/xml;charset=UTF-8'}) res = urllib2.urlopen(req, timeout=60) req = res.read() return req.decode() # if __name__ == '__main__': # httputil = HttpUtil() # httputil.postText() # HttpUtil.postText()