二开案例.python.python发送http请求原创
7人赞赏了该文章
430次浏览
编辑于2024年04月18日 10:02:06
# 引入clr运行库 import clr # 添加对cloud插件开发的常用组件的引用 clr.AddReference('System') clr.AddReference('System.Core') clr.AddReference('System.Data') clr.AddReference("System.Web.Extensions") clr.AddReference('Kingdee.BOS') clr.AddReference('Kingdee.BOS.DataEntity') clr.AddReference('Kingdee.BOS.Core') clr.AddReference('Kingdee.BOS.App') clr.AddReference('Kingdee.BOS.App.Core') clr.AddReference('Kingdee.BOS.ServiceHelper') clr.AddReference('Kingdee.BOS.ServiceFacade.Common') clr.AddReference('Newtonsoft.Json') # 导入cloud基础库中的常用实体对象(分命名空间导入,不会递归导入) from Kingdee.BOS import * from Kingdee.BOS.Core import * from Kingdee.BOS.Core.DependencyRules import * from Kingdee.BOS.Core.Bill import * from Kingdee.BOS.Core.DynamicForm.PlugIn import * from Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel import * from Kingdee.BOS.Core.DynamicForm.PlugIn.Args import * from Kingdee.BOS.Core.Metadata.FormElement import * from System import * from System.Data import * from System.Threading import * from System.Text import * from System.Collections.Generic import * from System.Linq import * from System.IO import * from System.Net import * from System.Security.Cryptography import * from System.Web.Script.Serialization import * from System.Collections.Generic import Dictionary from Kingdee.BOS.App.Data import * from Kingdee.BOS.ServiceHelper import * from Kingdee.BOS.Core.DynamicForm import * from Kingdee.BOS.Core.Metadata.EntityElement import * from Kingdee.BOS.Core.Metadata.FieldElement import * from Kingdee.BOS.Orm.DataEntity import * from Kingdee.BOS.ServiceFacade import * from Newtonsoft.Json import * from Newtonsoft.Json.Linq import * j = JsonSerializerProxy(Encoding.UTF8, False) ly_param = { "url": "http://192.168.0.132:8085/ly/testapi" } # 单据体按钮点击事件;其余地方的点击事件类似,这里就不一一例举 def EntryBarItemClick(e): key = e.BarItemKey.ToUpperInvariant(); # 菜单标识大写 if (key == "PYIV_tbButton".ToUpperInvariant()): # 对象转json字符串 json_str = j.Serialize(param) # 第一个请求地址、第二个具体的json参数 result = post(ly_param["url"], json_str) # this.View.ShowMessage(result) if result is None: this.View.ShowErrMessage('''中间服务调用接口调用失败!''') return # 把响应的json字符串转换为json对象 result = JObject.Parse(result) if result["Code"] != "0": this.View.ShowErrMessage('''错误代码{0}!,信息:{1}'''.format(result["Code"], result["Message"])) return def get(url): try: 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 except: return None def post(url, postdata): try: encode = "UTF-8" webRequest = HttpWebRequest.Create(url) webRequest.Method = "POST" webRequest.ContentType = "application/json" paraUrlCoded = postdata payload = Encoding.GetEncoding(encode.ToUpper()).GetBytes(paraUrlCoded) webRequest.ContentLength = payload.Length writer = webRequest.GetRequestStream() writer.Write(payload, 0, payload.Length) writer.Close() response = webRequest.GetResponse() sr = StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encode)) res = sr.ReadToEnd() return res except: return None
具体的get/post方法自行提取(方法来源于社区,具体文章找不到了。如有侵权还望告知,【emoji】)。
感谢大佬提供的python学习资料:https://vip.kingdee.com/people/name-2147471799?productLineId=1&lang=zh-CN
赞 7
7人点赞
还没有人点赞,快来当第一个点赞的人吧!
打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!
推荐阅读