C/S客户端Winform窗体调用WebApi接口(C# 实例)
作者:C/S框架网|www.cscode.ne  发布日期:2019-03-06 08:52:12
  C/S客户端Winform窗体调用WebApi接口(C# 实例)

C/S客户端Winform窗体调用WebApi接口(C# 实例)


C/S客户端的Winform窗体模拟APP调用WebApi接口:

C# Code:

/// <summary>
/// 数据操作请求(模拟APP端请求)
/// </summary>
/// <param name="dbid">数据账套编号</param>
/// <param name="userID">用户账号,对应用户表的Account字段</param>
/// <param name="token">令牌,登录成功后自动分配</param>
/// <param name="phone">手机号码</param>
/// <param name="actionID">接口编号、功能编号</param>
/// <param name="op">具体操作,如:Add/Delete/Edit/Query</param>
/// <param name="dataEncrypt">数据是否加密</param>
/// <returns></returns>
public static ModelRequestAction GetRequest4App(string dbid,
string userID, string token, string phone, int actionID, string op, bool dataEncrypt = true)
{
   //模拟APP客户端数据
   ModelRequestClientData4App M = new ModelRequestClientData4App();
   M.UserID = userID;
   M.Token = token;//令牌
   M.IP = "127.0.0.1";
   M.DeviceMID = "我的设备识别码";
   M.Version = "1.928";
   M.Client = "iOS";
   M.Phone = phone;//手机号码
   M.DBID = dbid;
   
   string clientData = JsonConvert.SerializeObject(M, JsonSettings.Current);
   string data = "{\"Key\":PO201900012345}";//传统的参数(JSON)
   
   //实例化WebApi接口所传递的参数对象
   ModelRequestAction mr = new ModelRequestAction();
   mr.Action = actionID;
   mr.Operation = op;
   mr.ApiKey = PrivateData.apikey;
   mr.Data = dataEncrypt ? CryptoHelper.DESEncrypt(data, PrivateData.DES_Key, PrivateData.DES_iv) : data;
   mr.Sign = CryptoHelper.ToMD5(mr.ApiKey + mr.Data + PrivateData.secret);//Sign数字签名
   mr.DataIsEncrypted = true;
   mr.ClientData = clientData;
   
   return mr;
}

//来源:C/S框架网(www.csframework.com) QQ:23404761



Winform界面的Button.Click事件


C# Code:

private void btnPOST_Click(object sender, EventArgs e)
{
   if (cmbUsers.SelectedItem == null) throw new Exception("请选择用户或登录!");
   
   //选择一个用户实例
   ModelUser user = cmbUsers.SelectedItem as ModelUser;
   
   object o = ModelExample.GetRequest4App("WebApi_TestDB", user.Account, user.Token, user.Phone, 949494, "AddCustomer");
   
   txtMethod.Text = "api/do/post";
   txtParam.Text = JsonConvert.SerializeObject(o);
   
   string url = txtURL.Text + txtMethod.Text;
   string result = WebApiTools.Post(url, txtParam.Text);//WebApi接口请求
   txtResponse.Text = result;
   
   ModelResponse R = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelResponse>(result);
   ShowObject(dataGridView1, R);
   
   MessageBox.Show("操作完成.\r\n" + R.Message);
}

//来源:C/S框架网(www.csframework.com) QQ:23404761



贴图图片-微信截图_20190306084533


C/S客户端的Winform窗体直接调用WebApi接口:

C# Code:

private void btnJSON_Click(object sender, EventArgs e)
{
   //数据对象
   ModelRequestClientData clientData = new ModelRequestClientData();
   clientData.UserID = "admin";
   clientData.Token = "12345123412341234";
   clientData.IP = "127.0.0.1";
   clientData.DBID = "WebApi_TestDB";
   
   string jsonClientData = JsonConvert.SerializeObject(clientData);
   string jsonData = "{\"Delete\":\"PO2019837300222\"}";//删除采购订单
   
   ModelRequestAction M = new ModelRequestAction();
   M.Action = int.Parse(txtAction.Text);
   M.Operation = txtOP.Text;
   M.ApiKey = txtApiKey.Text;
   M.ClientData = jsonClientData;
   //M.Data = chkEncrypt.Checked ? PrivateData.Key.Encrypt(txtData.Text) : txtData.Text;
   M.Data = jsonData;
   M.Sign = CryptoHelper.ToMD5(txtApiKey.Text + M.Data + txtSecretKey.Text);//数字签名,12345678=SecureKey私钥
   M.DataIsEncrypted = chkEncrypt.Checked;
   
   string json = JsonConvert.SerializeObject(M, JsonSettings.Current);
   txtParam.Text = json;
}

//来源:C/S框架网(www.csframework.com) QQ:23404761



相关产品:

C#.NET WebApi开发框架-标准版V1.0




C#.NET WebApi开发框架-标准版V1.0




上一篇 下一篇