欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# 接口调用

程序员文章站 2022-04-14 21:55:38
...
using System; using System.Collections.Generic; using System.Text; using System.Collections.Specialized; using System.Net; using System.IO; using System.Text.RegularExpressions; using System.Security.Cryptography.X509Certificates; using System.Net.Security; using System.Windows.Forms; using Newtonsoft.Json.Linq;
public
/// <summary> /// Post公共索引请求 /// </summary> /// <param name="data">POST数据</param> /// <param name="callBack">回调</param> public void PostIdxReqesut(Dictionary<string, string> data, PostReqesutCallBackInfo callBackInfo) { if (CurSession==null) { if (data.ContainsKey("SID") && data.ContainsKey("Token")) { callBackInfo.CallBack(new ResponseResult("你已退出登录", callBackInfo.UserState)); return; } } WebClient client = new WebClient(); client.UploadValuesCompleted += Client_UploadValuesCompleted; ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(bypassAllCertificateStuff); NameValueCollection nvc = new NameValueCollection(); ; foreach (var kvp in data) { nvc[kvp.Key] = kvp.Value; } client.UploadValuesAsync(BasicInfo.IdxUrl, "POST", nvc, callBackInfo); } private static bool bypassAllCertificateStuff(object sender,X509Certificate cert, X509Chain chain, SslPolicyErrors errors) { return true; }


上述代码为Post服务请求方法和证书验证(https)
/// <summary>
        /// 回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Client_UploadValuesCompleted(object sender, UploadValuesCompletedEventArgs e)
        {
            PostReqesutCallBackInfo callBackInfo = e.UserState as PostReqesutCallBackInfo;
            if (callBackInfo == null)
            {
                DevTools.WriteNote("callBackInfo is null !");
                return;
            }       
            Action<ResponseResult> callBack = callBackInfo.CallBack;
            if (callBack == null)
            {
                DevTools.WriteNote("callBackInfo.CallBack is null !");
                return;
            }              
            try
            {
                if (e.Error != null)
                {
                    DevTools.WriteNote("(e.Error:网络错误 !");
                    ShowMessage(e.Error.Message, "网络错误");
                    callBack(new ResponseResult(e.Error.Message, callBackInfo.UserState));
                    return;
                }
                JToken retData = JToken.Parse(Encoding.UTF8.GetString(e.Result));
                //DevTools.WriteNote("retData:" + retData.Root.ToString());
                
                ResponseCode code = (ResponseCode)Enum.Parse(typeof(ResponseCode), retData.GetValue("Code"));
                string message = retData.GetValue("Msg");
                int messageType = retData.GetIntValue("MsgType").Value;
               // DevTools.WriteNote(code.ToString()+"----"+ messageType.ToString()+"----"+ retData["Data"].ToString());
                if (!string.IsNullOrWhiteSpace(message))
                    ShowServerMessage(message, messageType, null);
                callBack(new ResponseResult(new ResponseData(code, message, messageType, retData["Data"]), callBackInfo.UserState));
                if (code == ResponseCode.OffLine || code == ResponseCode.PushedOffLine)
                    Logout();
            }
            catch (Exception ex)
            {
                DevTools.WriteNote("服务端数据解析失败:" + ex.Message + "\r\n" + Encoding.UTF8.GetString(e.Result));
                //ShowErrorDialog("服务端数据解析失败!", "数据错误");
            }
        }

该方法为回调函数需要引用
相关标签: post

上一篇: 杜教筛入门

下一篇: python小技巧