Add FrmTestRestService.

This commit is contained in:
2017-12-21 10:33:09 +01:00
parent 16c43862b8
commit 5da9cae021
5 changed files with 366 additions and 0 deletions

View File

@@ -0,0 +1,190 @@
namespace VAR.Toolbox.UI
{
partial class FrmTestRestService
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.txtUrl = new System.Windows.Forms.TextBox();
this.txtUrlApiMethod = new System.Windows.Forms.TextBox();
this.txtParameters = new System.Windows.Forms.TextBox();
this.txtBody = new System.Windows.Forms.TextBox();
this.lblURL = new System.Windows.Forms.Label();
this.lblUrlApiMethod = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.btnTest = new System.Windows.Forms.Button();
this.txtResult = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// txtUrl
//
this.txtUrl.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtUrl.Location = new System.Drawing.Point(123, 12);
this.txtUrl.Name = "txtUrl";
this.txtUrl.Size = new System.Drawing.Size(298, 20);
this.txtUrl.TabIndex = 0;
//
// txtUrlApiMethod
//
this.txtUrlApiMethod.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtUrlApiMethod.Location = new System.Drawing.Point(123, 39);
this.txtUrlApiMethod.Name = "txtUrlApiMethod";
this.txtUrlApiMethod.Size = new System.Drawing.Size(298, 20);
this.txtUrlApiMethod.TabIndex = 1;
//
// txtParameters
//
this.txtParameters.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtParameters.Location = new System.Drawing.Point(123, 66);
this.txtParameters.Multiline = true;
this.txtParameters.Name = "txtParameters";
this.txtParameters.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtParameters.Size = new System.Drawing.Size(298, 50);
this.txtParameters.TabIndex = 2;
//
// txtBody
//
this.txtBody.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtBody.Location = new System.Drawing.Point(123, 122);
this.txtBody.Multiline = true;
this.txtBody.Name = "txtBody";
this.txtBody.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtBody.Size = new System.Drawing.Size(298, 59);
this.txtBody.TabIndex = 3;
//
// lblURL
//
this.lblURL.AutoSize = true;
this.lblURL.Location = new System.Drawing.Point(12, 15);
this.lblURL.Name = "lblURL";
this.lblURL.Size = new System.Drawing.Size(29, 13);
this.lblURL.TabIndex = 4;
this.lblURL.Text = "URL";
//
// lblUrlApiMethod
//
this.lblUrlApiMethod.AutoSize = true;
this.lblUrlApiMethod.Location = new System.Drawing.Point(12, 42);
this.lblUrlApiMethod.Name = "lblUrlApiMethod";
this.lblUrlApiMethod.Size = new System.Drawing.Size(71, 13);
this.lblUrlApiMethod.TabIndex = 5;
this.lblUrlApiMethod.Text = "UrlApiMethod";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 69);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(60, 13);
this.label1.TabIndex = 6;
this.label1.Text = "Parameters";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 125);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(31, 13);
this.label2.TabIndex = 7;
this.label2.Text = "Body";
//
// btnTest
//
this.btnTest.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnTest.Location = new System.Drawing.Point(346, 187);
this.btnTest.Name = "btnTest";
this.btnTest.Size = new System.Drawing.Size(75, 23);
this.btnTest.TabIndex = 8;
this.btnTest.Text = "Test";
this.btnTest.UseVisualStyleBackColor = true;
this.btnTest.Click += new System.EventHandler(this.btnTest_Click);
//
// txtResult
//
this.txtResult.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtResult.Location = new System.Drawing.Point(12, 216);
this.txtResult.Multiline = true;
this.txtResult.Name = "txtResult";
this.txtResult.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtResult.Size = new System.Drawing.Size(409, 206);
this.txtResult.TabIndex = 9;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 200);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(37, 13);
this.label3.TabIndex = 10;
this.label3.Text = "Result";
//
// FrmTestRestService
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(433, 434);
this.Controls.Add(this.label3);
this.Controls.Add(this.txtResult);
this.Controls.Add(this.btnTest);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.lblUrlApiMethod);
this.Controls.Add(this.lblURL);
this.Controls.Add(this.txtBody);
this.Controls.Add(this.txtParameters);
this.Controls.Add(this.txtUrlApiMethod);
this.Controls.Add(this.txtUrl);
this.Name = "FrmTestRestService";
this.Text = "TestRestService";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox txtUrl;
private System.Windows.Forms.TextBox txtUrlApiMethod;
private System.Windows.Forms.TextBox txtParameters;
private System.Windows.Forms.TextBox txtBody;
private System.Windows.Forms.Label lblURL;
private System.Windows.Forms.Label lblUrlApiMethod;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button btnTest;
private System.Windows.Forms.TextBox txtResult;
private System.Windows.Forms.Label label3;
}
}

View File

@@ -0,0 +1,152 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace VAR.Toolbox.UI
{
public partial class FrmTestRestService : Form
{
public FrmTestRestService()
{
InitializeComponent();
}
private void btnTest_Click(object sender, EventArgs e)
{
try
{
string url = txtUrl.Text;
string urlApiMethod = txtUrlApiMethod.Text;
Dictionary<string, string> parms = StringToDictionary(txtParameters.Text);
string body = txtBody.Text;
string result = CallApi(url, urlApiMethod, parms, body);
txtResult.Text = result;
}
catch (Exception ex)
{
StringBuilder sbException = new StringBuilder();
while (ex != null)
{
sbException.AppendFormat("{0}\r\n{1}\r\n\r\n", ex.Message, ex.StackTrace);
ex = ex.InnerException;
}
txtResult.Text = sbException.ToString();
}
}
/// <summary>
/// Deseria una cadena a un diccionario string,string
/// </summary>
/// <param name="str">The STR.</param>
/// <returns></returns>
/// <author>VAR</author>
public static Dictionary<string, string> StringToDictionary(string str)
{
var dic = new Dictionary<string, string>();
List<string> pairs = SplitUnscaped(str, ',');
foreach (string pair in pairs)
{
List<string> values = SplitUnscaped(pair, ':');
if (values.Count < 2) continue;
string key = values[0].Replace("\\:", ":").Replace("\\,", ",");
string val = values[1].Replace("\\:", ":").Replace("\\,", ",");
dic.Add(key, val);
}
return dic;
}
/// <summary>
/// Serializa un diccionario string,string a una cadena.
/// </summary>
/// <param name="dic">The dic.</param>
/// <returns></returns>
/// <author>VAR</author>
public static string DictionaryToString(Dictionary<string, string> dic)
{
var sb = new StringBuilder();
foreach (KeyValuePair<string, string> entrada in dic)
{
string sKey = entrada.Key.Replace(":", "\\:").Replace(",", "\\,");
string sVal = entrada.Value.Replace(":", "\\:").Replace(",", "\\,");
sb.AppendFormat("{0}:{1},", sKey, sVal);
}
return sb.ToString();
}
/// <summary>
/// Parte una cadena usando un caracter, evitando usar las ocurrencias escapadas con '\\'
/// </summary>
/// <param name="str">The STR.</param>
/// <param name="splitter">The splitter.</param>
/// <returns></returns>
/// <author>VAR</author>
public static List<string> SplitUnscaped(string str, char splitter)
{
var strs = new List<string>();
int j, i;
int n = str.Length;
for (j = 0, i = 0; i < n; i++)
{
if (str[i] == '\\') i++;
else if (str[i] == splitter)
{
strs.Add(str.Substring(j, i - j));
j = i + 1;
}
}
if (i >= j) strs.Add(str.Substring(j, n - j));
return strs;
}
private static CookieContainer _cookieJar = new CookieContainer();
public static string CallApi(string urlService, string urlApiMethod, Dictionary<string, string> prms, string content)
{
var sbRequestUrl = new StringBuilder();
sbRequestUrl.Append(urlService);
sbRequestUrl.Append(urlApiMethod);
if (prms != null)
{
foreach (KeyValuePair<string, string> pair in prms)
{
sbRequestUrl.AppendFormat("&{0}={1}", pair.Key, Uri.EscapeUriString(pair.Value));
}
}
if (sbRequestUrl.Length > 2048)
{
throw new Exception(string.Format("CallApi: Request URL longer than 2048: url: \"{0}\"", sbRequestUrl.ToString()));
}
var http = (HttpWebRequest)WebRequest.Create(new Uri(sbRequestUrl.ToString()));
http.CookieContainer = _cookieJar;
http.Accept = "application/json";
http.ContentType = "application/json; charset=utf-8";
http.Method = "POST";
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(content);
Task<Stream> requestStreamTask = http.GetRequestStreamAsync();
requestStreamTask.Wait();
Stream requestStream = requestStreamTask.Result;
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Flush();
Task<WebResponse> responseTask = http.GetResponseAsync();
responseTask.Wait();
WebResponse response = responseTask.Result;
var stream = response.GetResponseStream();
var sr = new StreamReader(stream);
return sr.ReadToEnd();
}
}
}

View File

@@ -39,6 +39,7 @@
this.niTray = new System.Windows.Forms.NotifyIcon(this.components); this.niTray = new System.Windows.Forms.NotifyIcon(this.components);
this.btnExit = new System.Windows.Forms.Button(); this.btnExit = new System.Windows.Forms.Button();
this.btnTestSoapService = new System.Windows.Forms.Button(); this.btnTestSoapService = new System.Windows.Forms.Button();
this.btnTestRestService = new System.Windows.Forms.Button();
this.SuspendLayout(); this.SuspendLayout();
// //
// btnBase64 // btnBase64
@@ -137,11 +138,22 @@
this.btnTestSoapService.UseVisualStyleBackColor = true; this.btnTestSoapService.UseVisualStyleBackColor = true;
this.btnTestSoapService.Click += new System.EventHandler(this.btnTestSoapService_Click); this.btnTestSoapService.Click += new System.EventHandler(this.btnTestSoapService_Click);
// //
// btnTestRestService
//
this.btnTestRestService.Location = new System.Drawing.Point(180, 92);
this.btnTestRestService.Name = "btnTestRestService";
this.btnTestRestService.Size = new System.Drawing.Size(165, 36);
this.btnTestRestService.TabIndex = 9;
this.btnTestRestService.Text = "TestRestService";
this.btnTestRestService.UseVisualStyleBackColor = true;
this.btnTestRestService.Click += new System.EventHandler(this.btnTestRestService_Click);
//
// FrmToolbox // FrmToolbox
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(352, 400); this.ClientSize = new System.Drawing.Size(352, 400);
this.Controls.Add(this.btnTestRestService);
this.Controls.Add(this.btnTestSoapService); this.Controls.Add(this.btnTestSoapService);
this.Controls.Add(this.btnExit); this.Controls.Add(this.btnExit);
this.Controls.Add(this.lblToolbox); this.Controls.Add(this.lblToolbox);
@@ -174,6 +186,7 @@
private System.Windows.Forms.NotifyIcon niTray; private System.Windows.Forms.NotifyIcon niTray;
private System.Windows.Forms.Button btnExit; private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.Button btnTestSoapService; private System.Windows.Forms.Button btnTestSoapService;
private System.Windows.Forms.Button btnTestRestService;
} }
} }

View File

@@ -100,6 +100,11 @@ namespace VAR.Toolbox.UI
CreateWindow(typeof(FrmTestSoapService)); CreateWindow(typeof(FrmTestSoapService));
} }
private void btnTestRestService_Click(object sender, EventArgs e)
{
CreateWindow(typeof(FrmTestRestService));
}
#endregion UI events #endregion UI events
#region Window handling #region Window handling

View File

@@ -106,6 +106,12 @@
<Compile Include="UI\FrmProxyCmd.Designer.cs"> <Compile Include="UI\FrmProxyCmd.Designer.cs">
<DependentUpon>FrmProxyCmd.cs</DependentUpon> <DependentUpon>FrmProxyCmd.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="UI\FrmTestRestService.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="UI\FrmTestRestService.Designer.cs">
<DependentUpon>FrmTestRestService.cs</DependentUpon>
</Compile>
<Compile Include="UI\FrmTestSoapService.cs"> <Compile Include="UI\FrmTestSoapService.cs">
<SubType>Form</SubType> <SubType>Form</SubType>
</Compile> </Compile>