由于需要经常调用飞凤平台上的各种接口,为了方便调试接口写了个小程序:
有两个功能:格式化json代码,编辑json时自动补全tab键
飞凤平台api:http://develop.feifengiot.com/docCenter#/apiDetail/132/1968
代码很简单:
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;
}
}
}
}