一:post请求
1,包含 import urllib2,urllib
2,data={}
data["serviceUserName"] = "TJ4A"
data["servicePwd"] = "VEo0QQ=="
data["appToken"] = req1.get('appToken')
data["appCode"] = "JKDDDZLBZPT"
url="http://111.30.115.50:3018/VixtelRest?subInterface=queryUserByApptoken"
url_values = json.dumps(data,separators=(',',':'),ensure_ascii=False) #格式化json,并将json中的空格去掉。
3,headers = {'Content-Type': 'application/json'} #指定请求参数的格式
requestwe = urllib2.Request(url=url, headers=headers, data=url_values) # 发送请求同时传data表单
response = urllib2.urlopen(requestwe) #接受反馈的信息
result_str = json.loads(response.read()) #读取反馈的内容
二:get请求
import urllib2
import urllib
data = {}
data['name'] = 'WHY'
data['location'] = 'SDU'
data['language'] = 'Python'
url_values = urllib.urlencode(data)
print url_values
name=Somebody+Here&language=Python&location=Northampton
url = 'http://www.example.com/example.cgi'
full_url = url + '?' + url_values
data = urllib2.open(full_url)