From 16c43862b8dc74e67ca85cb02f714f922a685c42 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Thu, 21 Dec 2017 10:25:41 +0100 Subject: [PATCH] Add FrmTestSoapService --- VAR.Toolbox/UI/FrmTestSoapService.Designer.cs | 187 ++++++++++++++++++ VAR.Toolbox/UI/FrmTestSoapService.cs | 182 +++++++++++++++++ VAR.Toolbox/UI/FrmToolbox.Designer.cs | 51 +++-- VAR.Toolbox/UI/FrmToolbox.cs | 6 + VAR.Toolbox/VAR.Toolbox.csproj | 6 + 5 files changed, 406 insertions(+), 26 deletions(-) create mode 100644 VAR.Toolbox/UI/FrmTestSoapService.Designer.cs create mode 100644 VAR.Toolbox/UI/FrmTestSoapService.cs diff --git a/VAR.Toolbox/UI/FrmTestSoapService.Designer.cs b/VAR.Toolbox/UI/FrmTestSoapService.Designer.cs new file mode 100644 index 0000000..025ec55 --- /dev/null +++ b/VAR.Toolbox/UI/FrmTestSoapService.Designer.cs @@ -0,0 +1,187 @@ +namespace VAR.Toolbox.UI +{ + partial class FrmTestSoapService + { + /// + /// 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.label1 = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.label3 = new System.Windows.Forms.Label(); + this.label4 = new System.Windows.Forms.Label(); + this.txtUrl = new System.Windows.Forms.TextBox(); + this.txtInterface = new System.Windows.Forms.TextBox(); + this.txtMethod = new System.Windows.Forms.TextBox(); + this.txtParameters = new System.Windows.Forms.TextBox(); + this.txtResult = new System.Windows.Forms.TextBox(); + this.label5 = new System.Windows.Forms.Label(); + this.btnTest = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(12, 16); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(29, 13); + this.label1.TabIndex = 0; + this.label1.Text = "URL"; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(12, 43); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(49, 13); + this.label2.TabIndex = 1; + this.label2.Text = "Interface"; + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(12, 70); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(43, 13); + this.label3.TabIndex = 2; + this.label3.Text = "Method"; + // + // label4 + // + this.label4.AutoSize = true; + this.label4.Location = new System.Drawing.Point(12, 97); + this.label4.Name = "label4"; + this.label4.Size = new System.Drawing.Size(60, 13); + this.label4.TabIndex = 3; + this.label4.Text = "Parameters"; + // + // 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(98, 13); + this.txtUrl.Name = "txtUrl"; + this.txtUrl.Size = new System.Drawing.Size(389, 20); + this.txtUrl.TabIndex = 4; + // + // txtInterface + // + this.txtInterface.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.txtInterface.Location = new System.Drawing.Point(98, 40); + this.txtInterface.Name = "txtInterface"; + this.txtInterface.Size = new System.Drawing.Size(389, 20); + this.txtInterface.TabIndex = 5; + // + // txtMethod + // + this.txtMethod.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.txtMethod.Location = new System.Drawing.Point(98, 67); + this.txtMethod.Name = "txtMethod"; + this.txtMethod.Size = new System.Drawing.Size(389, 20); + this.txtMethod.TabIndex = 6; + // + // 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(98, 94); + this.txtParameters.Multiline = true; + this.txtParameters.Name = "txtParameters"; + this.txtParameters.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; + this.txtParameters.Size = new System.Drawing.Size(389, 103); + this.txtParameters.TabIndex = 7; + // + // 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, 234); + this.txtResult.Multiline = true; + this.txtResult.Name = "txtResult"; + this.txtResult.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; + this.txtResult.Size = new System.Drawing.Size(475, 196); + this.txtResult.TabIndex = 8; + // + // label5 + // + this.label5.AutoSize = true; + this.label5.Location = new System.Drawing.Point(12, 215); + this.label5.Name = "label5"; + this.label5.Size = new System.Drawing.Size(37, 13); + this.label5.TabIndex = 9; + this.label5.Text = "Result"; + // + // 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(412, 203); + this.btnTest.Name = "btnTest"; + this.btnTest.Size = new System.Drawing.Size(75, 23); + this.btnTest.TabIndex = 10; + this.btnTest.Text = "Test"; + this.btnTest.UseVisualStyleBackColor = true; + this.btnTest.Click += new System.EventHandler(this.btnTest_Click); + // + // FrmTestSoapService + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(499, 442); + this.Controls.Add(this.btnTest); + this.Controls.Add(this.label5); + this.Controls.Add(this.txtResult); + this.Controls.Add(this.txtParameters); + this.Controls.Add(this.txtMethod); + this.Controls.Add(this.txtInterface); + this.Controls.Add(this.txtUrl); + this.Controls.Add(this.label4); + this.Controls.Add(this.label3); + this.Controls.Add(this.label2); + this.Controls.Add(this.label1); + this.Name = "FrmTestSoapService"; + this.Text = "TestSoapService"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Label label1; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.Label label4; + private System.Windows.Forms.TextBox txtUrl; + private System.Windows.Forms.TextBox txtInterface; + private System.Windows.Forms.TextBox txtMethod; + private System.Windows.Forms.TextBox txtParameters; + private System.Windows.Forms.TextBox txtResult; + private System.Windows.Forms.Label label5; + private System.Windows.Forms.Button btnTest; + } +} \ No newline at end of file diff --git a/VAR.Toolbox/UI/FrmTestSoapService.cs b/VAR.Toolbox/UI/FrmTestSoapService.cs new file mode 100644 index 0000000..9938afe --- /dev/null +++ b/VAR.Toolbox/UI/FrmTestSoapService.cs @@ -0,0 +1,182 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Windows.Forms; +using System.Xml; + +namespace VAR.Toolbox.UI +{ + public partial class FrmTestSoapService : Form + { + public FrmTestSoapService() + { + InitializeComponent(); + } + + private void btnTest_Click(object sender, EventArgs e) + { + try + { + string url = txtUrl.Text; + string iface = txtInterface.Text; + string method = txtMethod.Text; + Dictionary parms = StringToDictionary(txtParameters.Text); + + Dictionary result = CallSoapMethod(url, iface, method, parms); + + txtResult.Text = DictionaryToString(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; + } + + /// + /// Llama a un metodo SOAP. Esto requiere que el binding del servicio WCF sea de tipo "basicHttpBinding" + /// + /// The URL. + /// The iface. + /// The method. + /// The parms. + /// + /// 12/05/2014 + /// VAR + public static Dictionary CallSoapMethod(string url, string iface, string method, Dictionary parms) + { + // Los servicios SOAP se llaman siempre a traves de HTTP. + if (url.ToLower().StartsWith("https://")) + { + url = string.Format("http://{0}", url.Substring("https://".Length)); + } + + // Construir peticion + var sbData = new StringBuilder(); + sbData.AppendFormat("\n"); + sbData.AppendFormat("\n"); + sbData.AppendFormat("\n"); + sbData.AppendFormat("<{0} xmlns=\"http://tempuri.org/\">\n", method); + foreach (KeyValuePair parm in parms) + { + sbData.AppendFormat("<{0}>{1}\n", parm.Key, parm.Value); + + // FIXME: Accept null values + //sbData.AppendFormat("<{0} i:nil=\"true\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" />\n", parm.Key); + } + sbData.AppendFormat("\n", method); + sbData.AppendFormat("\n"); + sbData.AppendFormat("\n"); + byte[] postData = Encoding.UTF8.GetBytes(sbData.ToString()); + + // Realizar peticion + var client = new System.Net.WebClient(); + client.Headers.Add("Accept", "text/xml"); + client.Headers.Add("Accept-Charset", "UTF-8"); + client.Headers.Add("Content-Type", "text/xml; charset=UTF-8"); + client.Headers.Add("SOAPAction", string.Format("\"{0}/{1}/{2}\"", "http://tempuri.org", iface, method)); + byte[] data; + try + { + data = client.UploadData(url, "POST", postData); + } + catch (Exception ex) + { + throw new Exception(string.Format("Failure calling SoapService: URL: {0}", url), ex); + } + string strData = System.Text.Encoding.UTF8.GetString(data); + var strReader = new StringReader(strData); + var xmlReader = new XmlTextReader(strReader); + + // Parsear resultado + Dictionary resultObject = new Dictionary(); + while (xmlReader.Read()) + { + if (xmlReader.NodeType == XmlNodeType.Element) + { + String name = xmlReader.Name; + if (name.Contains(":")) + { + name = name.Split(':')[1]; + } + resultObject.Add(name, xmlReader.ReadString()); + } + } + return resultObject; + } + + + } +} diff --git a/VAR.Toolbox/UI/FrmToolbox.Designer.cs b/VAR.Toolbox/UI/FrmToolbox.Designer.cs index b477ac3..d395c5c 100644 --- a/VAR.Toolbox/UI/FrmToolbox.Designer.cs +++ b/VAR.Toolbox/UI/FrmToolbox.Designer.cs @@ -38,15 +38,14 @@ this.pnlCover1 = new VAR.Toolbox.UI.PnlCover(); this.niTray = new System.Windows.Forms.NotifyIcon(this.components); this.btnExit = new System.Windows.Forms.Button(); + this.btnTestSoapService = new System.Windows.Forms.Button(); this.SuspendLayout(); // // btnBase64 // - this.btnBase64.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); this.btnBase64.Location = new System.Drawing.Point(9, 52); this.btnBase64.Name = "btnBase64"; - this.btnBase64.Size = new System.Drawing.Size(159, 34); + this.btnBase64.Size = new System.Drawing.Size(165, 34); this.btnBase64.TabIndex = 0; this.btnBase64.Text = "Base64"; this.btnBase64.UseVisualStyleBackColor = true; @@ -54,11 +53,9 @@ // // btnProxyCmd // - this.btnProxyCmd.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); this.btnProxyCmd.Location = new System.Drawing.Point(9, 92); this.btnProxyCmd.Name = "btnProxyCmd"; - this.btnProxyCmd.Size = new System.Drawing.Size(159, 36); + this.btnProxyCmd.Size = new System.Drawing.Size(165, 36); this.btnProxyCmd.TabIndex = 1; this.btnProxyCmd.Text = "ProxyCmd"; this.btnProxyCmd.UseVisualStyleBackColor = true; @@ -66,11 +63,9 @@ // // btnWebcam // - this.btnWebcam.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); this.btnWebcam.Location = new System.Drawing.Point(9, 135); this.btnWebcam.Name = "btnWebcam"; - this.btnWebcam.Size = new System.Drawing.Size(159, 36); + this.btnWebcam.Size = new System.Drawing.Size(165, 36); this.btnWebcam.TabIndex = 2; this.btnWebcam.Text = "Webcam"; this.btnWebcam.UseVisualStyleBackColor = true; @@ -78,12 +73,10 @@ // // btnTunnelTCP // - this.btnTunnelTCP.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); this.btnTunnelTCP.Location = new System.Drawing.Point(9, 175); this.btnTunnelTCP.Margin = new System.Windows.Forms.Padding(2); this.btnTunnelTCP.Name = "btnTunnelTCP"; - this.btnTunnelTCP.Size = new System.Drawing.Size(159, 36); + this.btnTunnelTCP.Size = new System.Drawing.Size(165, 36); this.btnTunnelTCP.TabIndex = 5; this.btnTunnelTCP.Text = "TunnelTCP"; this.btnTunnelTCP.UseVisualStyleBackColor = true; @@ -97,29 +90,25 @@ this.lblToolbox.Location = new System.Drawing.Point(9, 9); this.lblToolbox.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0); this.lblToolbox.Name = "lblToolbox"; - this.lblToolbox.Size = new System.Drawing.Size(159, 40); + this.lblToolbox.Size = new System.Drawing.Size(336, 40); this.lblToolbox.TabIndex = 6; this.lblToolbox.Text = "Toolbox"; this.lblToolbox.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // pnlSuspension1 // - this.pnlSuspension1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); - this.pnlSuspension1.Location = new System.Drawing.Point(10, 323); + this.pnlSuspension1.Location = new System.Drawing.Point(180, 241); this.pnlSuspension1.Margin = new System.Windows.Forms.Padding(1); this.pnlSuspension1.Name = "pnlSuspension1"; - this.pnlSuspension1.Size = new System.Drawing.Size(155, 114); + this.pnlSuspension1.Size = new System.Drawing.Size(165, 114); this.pnlSuspension1.TabIndex = 4; // // pnlCover1 // - this.pnlCover1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); - this.pnlCover1.Location = new System.Drawing.Point(10, 225); + this.pnlCover1.Location = new System.Drawing.Point(12, 241); this.pnlCover1.Margin = new System.Windows.Forms.Padding(1); this.pnlCover1.Name = "pnlCover1"; - this.pnlCover1.Size = new System.Drawing.Size(155, 96); + this.pnlCover1.Size = new System.Drawing.Size(162, 114); this.pnlCover1.TabIndex = 3; // // niTray @@ -130,21 +119,30 @@ // // btnExit // - this.btnExit.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); - this.btnExit.Location = new System.Drawing.Point(12, 441); + this.btnExit.Location = new System.Drawing.Point(12, 359); this.btnExit.Name = "btnExit"; - this.btnExit.Size = new System.Drawing.Size(152, 29); + this.btnExit.Size = new System.Drawing.Size(333, 29); this.btnExit.TabIndex = 7; this.btnExit.Text = "Exit"; this.btnExit.UseVisualStyleBackColor = true; this.btnExit.Click += new System.EventHandler(this.btnExit_Click); // + // btnTestSoapService + // + this.btnTestSoapService.Location = new System.Drawing.Point(180, 52); + this.btnTestSoapService.Name = "btnTestSoapService"; + this.btnTestSoapService.Size = new System.Drawing.Size(165, 34); + this.btnTestSoapService.TabIndex = 8; + this.btnTestSoapService.Text = "TestSoapService"; + this.btnTestSoapService.UseVisualStyleBackColor = true; + this.btnTestSoapService.Click += new System.EventHandler(this.btnTestSoapService_Click); + // // FrmToolbox // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(175, 482); + this.ClientSize = new System.Drawing.Size(352, 400); + this.Controls.Add(this.btnTestSoapService); this.Controls.Add(this.btnExit); this.Controls.Add(this.lblToolbox); this.Controls.Add(this.btnTunnelTCP); @@ -175,6 +173,7 @@ private System.Windows.Forms.Label lblToolbox; private System.Windows.Forms.NotifyIcon niTray; private System.Windows.Forms.Button btnExit; + private System.Windows.Forms.Button btnTestSoapService; } } diff --git a/VAR.Toolbox/UI/FrmToolbox.cs b/VAR.Toolbox/UI/FrmToolbox.cs index 57c36f5..2f1da14 100644 --- a/VAR.Toolbox/UI/FrmToolbox.cs +++ b/VAR.Toolbox/UI/FrmToolbox.cs @@ -95,6 +95,11 @@ namespace VAR.Toolbox.UI CreateWindow(typeof(FrmTunnelTCP)); } + private void btnTestSoapService_Click(object sender, EventArgs e) + { + CreateWindow(typeof(FrmTestSoapService)); + } + #endregion UI events #region Window handling @@ -169,5 +174,6 @@ namespace VAR.Toolbox.UI } #endregion Window handling + } } diff --git a/VAR.Toolbox/VAR.Toolbox.csproj b/VAR.Toolbox/VAR.Toolbox.csproj index fde32c3..160e5ee 100644 --- a/VAR.Toolbox/VAR.Toolbox.csproj +++ b/VAR.Toolbox/VAR.Toolbox.csproj @@ -106,6 +106,12 @@ FrmProxyCmd.cs + + Form + + + FrmTestSoapService.cs + Form