python 发送get,post请求原创
金蝶云社区-温侯
温侯
14人赞赏了该文章 2,615次浏览 未经作者许可,禁止转载编辑于2021年02月01日 16:54:54

import clr
clr.AddReference("System")
clr.AddReference("System.Web.Extensions")
clr.AddReference("Kingdee.BOS.Core")
clr.AddReference("Kingdee.BOS")
clr.AddReference("Newtonsoft.Json")
import sys
from System import *
from System.Collections.Generic import *
from System.Threading import *
from System.IO import *
from System.Net import *
from System.Text import *
from System.Security.Cryptography import *
from System.Web.Script.Serialization import *
from System.Collections.Generic import Dictionary
from Newtonsoft.Json.Linq import *
reload(sys)
sys.setdefaultencoding('utf-8')

def ButtonClick(e):    
    #发送get请求
    #webRequest=get("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=XXX&corpsecret=XXX")
    #this.View.ShowMessage(webRequest)
    
    #解析json
    #JObject = JObject.Parse(webRequest)
    #this.View.ShowMessage(JObject['access_token'].ToString())
    
    #发送post请求
    #webRequest=post('https://www.anyan.com/anyan/12223.php','choose=asdfga')
    #this.View.ShowMessage(webRequest)
    
    #发送post请求
    #webRequest=post('https://www.anyan.com/anyan/22222.php','choose={"id":"123"}')
    #this.View.ShowMessage(webRequest)    
    
def get(url):    
    webRequest = WebRequest.Create(url)
    webRequest.Method = "GET";
    webResponse = webRequest.GetResponse();    
    stream = webResponse.GetResponseStream();
    streamReader =StreamReader(stream, Encoding.GetEncoding("utf-8"));
    result = streamReader.ReadToEnd();    
    return result
    
def post(url,postdata):        
    webRequest = HttpWebRequest.Create(url);
    webRequest.Method = "POST";
    webRequest.ContentType = "application/x-www-form-urlencoded";    
    data=Encoding.ASCII.GetBytes(postdata);
    webRequest.ContentLength = data.Length;
    webRequest.GetRequestStream().Write(data, 0, data.Length);
    webRequest.GetRequestStream().Flush();
    webRequest.GetRequestStream().Close();
    webResponse  =webRequest.GetResponse();
    streamReader =StreamReader( webResponse .GetResponseStream(),Encoding.GetEncoding("utf-8") );
    result=streamReader.ReadToEnd()
    return result

图标赞 14
14人点赞
还没有人点赞,快来当第一个点赞的人吧!
图标打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!