diff --git a/VAR.Toolbox/UI/FrmTestRestService.Designer.cs b/VAR.Toolbox/UI/FrmTestRestService.Designer.cs new file mode 100644 index 0000000..db0de02 --- /dev/null +++ b/VAR.Toolbox/UI/FrmTestRestService.Designer.cs @@ -0,0 +1,190 @@ +namespace VAR.Toolbox.UI +{ + partial class FrmTestRestService + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + 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; + } +} + diff --git a/VAR.Toolbox/UI/FrmTestRestService.cs b/VAR.Toolbox/UI/FrmTestRestService.cs new file mode 100644 index 0000000..4f36dd4 --- /dev/null +++ b/VAR.Toolbox/UI/FrmTestRestService.cs @@ -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 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(); + } + } + + /// + /// Deseria una cadena a un diccionario string,string + /// + /// The STR. + /// + /// VAR + public static Dictionary StringToDictionary(string str) + { + var dic = new Dictionary(); + List pairs = SplitUnscaped(str, ','); + foreach (string pair in pairs) + { + List 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; + } + + /// + /// Serializa un diccionario string,string a una cadena. + /// + /// The dic. + /// + /// VAR + public static string DictionaryToString(Dictionary dic) + { + var sb = new StringBuilder(); + foreach (KeyValuePair entrada in dic) + { + string sKey = entrada.Key.Replace(":", "\\:").Replace(",", "\\,"); + string sVal = entrada.Value.Replace(":", "\\:").Replace(",", "\\,"); + sb.AppendFormat("{0}:{1},", sKey, sVal); + } + return sb.ToString(); + } + + /// + /// Parte una cadena usando un caracter, evitando usar las ocurrencias escapadas con '\\' + /// + /// The STR. + /// The splitter. + /// + /// VAR + public static List SplitUnscaped(string str, char splitter) + { + var strs = new List(); + 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 prms, string content) + { + var sbRequestUrl = new StringBuilder(); + sbRequestUrl.Append(urlService); + sbRequestUrl.Append(urlApiMethod); + if (prms != null) + { + foreach (KeyValuePair 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 requestStreamTask = http.GetRequestStreamAsync(); + requestStreamTask.Wait(); + Stream requestStream = requestStreamTask.Result; + requestStream.Write(bytes, 0, bytes.Length); + requestStream.Flush(); + + Task responseTask = http.GetResponseAsync(); + responseTask.Wait(); + WebResponse response = responseTask.Result; + var stream = response.GetResponseStream(); + var sr = new StreamReader(stream); + return sr.ReadToEnd(); + } + + } +} diff --git a/VAR.Toolbox/UI/FrmToolbox.Designer.cs b/VAR.Toolbox/UI/FrmToolbox.Designer.cs index d395c5c..0d9100e 100644 --- a/VAR.Toolbox/UI/FrmToolbox.Designer.cs +++ b/VAR.Toolbox/UI/FrmToolbox.Designer.cs @@ -39,6 +39,7 @@ this.niTray = new System.Windows.Forms.NotifyIcon(this.components); this.btnExit = new System.Windows.Forms.Button(); this.btnTestSoapService = new System.Windows.Forms.Button(); + this.btnTestRestService = new System.Windows.Forms.Button(); this.SuspendLayout(); // // btnBase64 @@ -137,11 +138,22 @@ this.btnTestSoapService.UseVisualStyleBackColor = true; 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 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(352, 400); + this.Controls.Add(this.btnTestRestService); this.Controls.Add(this.btnTestSoapService); this.Controls.Add(this.btnExit); this.Controls.Add(this.lblToolbox); @@ -174,6 +186,7 @@ private System.Windows.Forms.NotifyIcon niTray; private System.Windows.Forms.Button btnExit; private System.Windows.Forms.Button btnTestSoapService; + private System.Windows.Forms.Button btnTestRestService; } } diff --git a/VAR.Toolbox/UI/FrmToolbox.cs b/VAR.Toolbox/UI/FrmToolbox.cs index 2f1da14..39fac86 100644 --- a/VAR.Toolbox/UI/FrmToolbox.cs +++ b/VAR.Toolbox/UI/FrmToolbox.cs @@ -100,6 +100,11 @@ namespace VAR.Toolbox.UI CreateWindow(typeof(FrmTestSoapService)); } + private void btnTestRestService_Click(object sender, EventArgs e) + { + CreateWindow(typeof(FrmTestRestService)); + } + #endregion UI events #region Window handling diff --git a/VAR.Toolbox/VAR.Toolbox.csproj b/VAR.Toolbox/VAR.Toolbox.csproj index 160e5ee..11e3252 100644 --- a/VAR.Toolbox/VAR.Toolbox.csproj +++ b/VAR.Toolbox/VAR.Toolbox.csproj @@ -106,6 +106,12 @@ FrmProxyCmd.cs + + Form + + + FrmTestRestService.cs + Form