宝塔 运维平台 C#版接口调用-看了下竟然没有C#的接口?好吧,我们来贡献一个

 

【新提醒】API接口使用教程 - 第三方应用 - 宝塔面板论坛  https://www.bt.cn/bbs/thread-20376-1-1.html

image.png

 

看了下竟然没有C#的接口?好吧,我们来贡献一个

 

1、首先开启接口API功能,然后设置密匙,设置ip白名单,如果自己的ip不清楚,可以百度一下

image.png

 

 

2、C#例子,SDK下载如下

image.png

 

 

image.png

 

返回结果如下:json如下

 

 

{"memTotal": 1837, "memFree": 107, "memBuffers": 2, "memCached": 1019, "memRealUsed": 709, "cpuNum": 1, "cpuRealUsed": 1.0, "time": "41天", "system": "CentOS  7.8.2003 x86_64(Py3.7.9)", "isuser": 0, "isport": true, "version": "7.8.0"}

 

"time": "41天"和首页显示的“已不间断运行41天”匹配,Centos版本也获取到了的。 之前一直请求不成功,加上ToLower()就好了,更多的C#接口整理中。

 

image.png

 

 

3、集成Ftp接口示例: 我们可以通过浏览器获取抓包获取到参数如下

image.png

 

 

image.png

 

 

image.png

 

 

接口地址是http://ip:8888/ftp?action=AddUser

参数是 ftp_username=123&ftp_password=bccZbfAhNe2cxwmx&path=%2Fwww%2Fwwwroot%2F123&ps=123

注意path一定要url编码,自己调试时发现不编码可能会有问题;

 

通过接口调用成功如下:

 

image.png

 

 

完整的C#代码:

 

 1       try
 2             {
 3                 //                签名算法
 4                 //api_sk = 接口密钥 (在面板设置页面 - API 接口中获取)
 5                 //request_time = 当前请求时间的 uinx 时间戳 ( php: time() / python: time.time() )
 6                 //request_token = md5(string(request_time) + md5(api_sk))
 7                 //PHP 示例: $request_token = md5($request_time . ‘’ . md5($api_sk))
 8                 //签名:
 9                 //参数名称 参数值 说明
10                 //request_time 当前 uinx 时间戳 [必传]
11                 //request_token md5(string(request_time) + md5(api_sk)) [必传]
12                 //其它参数 功能接口需要的其它参数 [可选]
13                 //注意事项:
14                 //1、请统一使用 POST 方式请求 API 接口
15                 //2、为了确保请求效率,请保存 cookie,并在每次请求时附上 cookie
16                 //3、为了面板安全考虑,请务必添加 IP 白名单
17                 //4、所有响应内容统一为 Json 数据格式
18  
19  
20                 // 接口密钥 (在面板设置页面 - API 接口中获取)
21                 string api_sk = BaotaHelper._apiKey;
22                 //接口地址和方法
23                 string url = "http://ip:8888/ftp?action=AddUser";
24                 //Unix时间戳
25                 string timestamp = BaotaHelper.GetUnix(DateTime.Now).ToString();
26                 string md5ApiSK = BaotaHelper.GetMd5(api_sk).ToLower();
27                 string request_token = BaotaHelper.GetMd5(timestamp + md5ApiSK).ToLower();//md5(string(request_time) + md5(api_sk))
28                 // ftp_username=123&ftp_password=bccZbfAhNe2cxwmx&path=%2Fwww%2Fwwwroot%2F123&ps=123
29               //  string postData = "request_time=" + timestamp + "&request_token=" + request_token + "&ftp_username=" + txtUserName.Text.Trim() + "&ftp_password="+txtPwd.Text.Trim() + "&path=" + txtServerPath.Text.Trim()  ;//"&ps=" + "这个是备注";
30  
31  
32                 //注意path需要url编码, path=/www/wwwroot/123 对应的为 path=%2Fwww%2Fwwwroot%2F123
33  
34                 string path = System.Web.HttpUtility.UrlEncode(txtServerPath.Text.Trim(), System.Text.Encoding.UTF8); //编码
35  
36  
37                 string postData = "request_time=" + timestamp + "&request_token=" + request_token + "&ftp_username=" + txtUserName.Text.Trim() + "&ftp_password=" + txtPwd.Text.Trim() + "&path=" + path + "&ps=" + txtUserName.Text.Trim();
38  
39  
40                 string resultJson = HttpUtils.DoPost(url, postData);
41  
42                 richTextBox1.Text = resultJson;
43  
44             }
45             catch (Exception ex)
46             {
47                 MessageBox.Show(ex.Message);
48             }

 


大功告成,完成代码下载百度云。欢迎点赞。

 

链接:https://pan.baidu.com/s/1lZYSBnUEHONIWBWx1F0F3Q 

提取码:b3v3 

 

 

 

 

posted @ 2022-02-25 11:45  源码DNA分享  阅读(316)  评论(0编辑  收藏  举报