深度优先

这个家伙好懒,除了文章什么都没留下

0%

【WinForm】飞凤平台 - api调试工具

由于需要经常调用飞凤平台上的各种接口,为了方便调试接口写了个小程序:

有两个功能:格式化json代码,编辑json时自动补全tab键

飞凤平台api:http://develop.feifengiot.com/docCenter#/apiDetail/132/1968

代码很简单:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using aliyun_api_gateway_sdk.Constant;
using aliyun_api_gateway_sdk.Util;
using Newtonsoft.Json;

namespace FeiFengTools
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.txtBody.Focus();
}


private string ConvertJsonString(string str)
{
//格式化json字符串
JsonSerializer serializer = new JsonSerializer();
TextReader tr = new StringReader(str);
JsonTextReader jtr = new JsonTextReader(tr);
object obj = serializer.Deserialize(jtr);
if (obj != null)
{
StringWriter textWriter = new StringWriter();
JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
{
Formatting = Formatting.Indented,
Indentation = 1,
IndentChar = '\t'
};
serializer.Serialize(jsonWriter, obj);
return textWriter.ToString();
}
else
{
return str;
}
}

private void btnSend_Click(object sender, EventArgs e)
{
String bobyContent = this.txtBody.Text;
String path = this.txtApi.Text;
String appKey = this.txtAppkey.Text;
String appSecret = this.txtAppSecret.Text;
String host = this.txtHost.Text;

Dictionary<String, String> headers = new Dictionary<string, string>();
Dictionary<String, String> querys = new Dictionary<string, string>();
Dictionary<String, String> bodys = new Dictionary<string, string>();
List<String> signHeader = new List<String>();

//设定Content-Type,根据服务器端接受的值来设置¸
headers.Add(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_STREAM);
//设定Accept,根据服务器端接受的值来设置
headers.Add(HttpHeader.HTTP_HEADER_ACCEPT, ContentType.CONTENT_TYPE_JSON);

//注意:如果有非Form形式数据(body中只有value,没有key);如果body中是key/value形式数据,不要指定此行
headers.Add(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.Base64AndMD5(Encoding.UTF8.GetBytes(bobyContent)));

//注意:业务body部分
bodys.Add("", bobyContent);

//指定参与签名的header
signHeader.Add(SystemHeader.X_CA_TIMESTAMP);

using (HttpWebResponse response = HttpUtil.HttpPost(host, path, appKey, appSecret, 30000, headers, querys, bodys, signHeader))
{
Stream st = response.GetResponseStream();
StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
// 通过reader.ReadToEnd获取返回结果,正常情况下结果是一个json string

//Console.WriteLine(reader.ReadToEnd());

this.txtResult.Text = ConvertJsonString(reader.ReadToEnd());
}
}

private void btnClear_Click(object sender, EventArgs e)
{
this.txtResult.Text = string.Empty;
}

private void txtBody_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Control || e.KeyCode == Keys.Enter)
{
int totalline = txtBody.GetLineFromCharIndex(txtBody.Text.Length) + 1;
int index = txtBody.GetFirstCharIndexOfCurrentLine();
int line = txtBody.GetLineFromCharIndex(index) + 1;
string strLine = txtBody.Lines[line -2];

string insertString = string.Empty;
int n = 0;
while (strLine.IndexOf("\t") == 0)
{
insertString += "\t";
strLine = strLine.Remove(0,1);
n++;
}

int idx = txtBody.SelectionStart;
txtBody.Text = txtBody.Text.Insert(txtBody.SelectionStart, insertString);
txtBody.SelectionStart = idx + n;
txtBody.Focus();
}
}

private void btnTimesToDate_Click(object sender, EventArgs e)
{
try
{

//DateTime dateTimeStart = DateTime.Parse("1970/1/1 08:00");
//timesTamp = timesTamp / 1000;
//return dateTimeStart.AddSeconds(timesTamp);

DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(this.timesTamp1.Text.Substring(0,10) + "0000000");
TimeSpan toNow = new TimeSpan(lTime);
this.timesDate1.Text = dateTimeStart.Add(toNow).ToString("yyyy-MM-dd HH:mm:ss");
}
catch (Exception ex)
{
this.timesDate1.Text = ex.Message;
}
}

private void btnDateToTimes_Click(object sender, EventArgs e)
{
try
{
long start = (DateTime.Parse(this.timesDate2.Text).ToUniversalTime().Ticks - 621355968000000000) / 10000;
this.timesTamp2.Text = start.ToString();
}
catch (Exception ex)
{
this.timesDate1.Text = ex.Message;
}
}
}
}