Introduction
This stub solves the problem of testing a Web Service by allowing you to post XML files to the Web Service method as an HTTP POST parameter. Overall, you should be able to use the tool once compiled against any Web Service URI and method.
Background
Anyone new to Web Services will want to start by understanding the basic technology. W3Schools (http://www.w3schools.com/webservices/default.asp) has a healthy set of tutorials to get started. I decided that I needed to roll my own test application after taking a look at the soapUI product. The problem was that I needed to test my application using HTTP POST events. This tool is the result of that need.
Ugly but useful
Using the code
Plug the code into your button click event to get started. The code will illustrate how the web request / responses are shaped. I've added another class to overwrite any certificates you might use in accessing an SSL site. You should verify that the target URI is viewable in the web browser from your client machine. The code does not install SSL certificates. You should open an SSL site if needed and install the certificate using the browser. The tool will work against HTTP and HTTPS URIs.
using System;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Threading;
namespace DISClientTester
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
ServicePointManager.DefaultConnectionLimit = 100;
txtAbstract.Clear();
string strURL = this.txtURL.Text;
this.openFileDialog1.Filter = "XML files (*.xml) | *.xml";
this.openFileDialog1.ShowDialog();
try
{
xmlDoc.Load(this.openFileDialog1.FileName.ToString());
}
catch (XmlException xmlEx)
{
MessageBox.Show(xmlEx.Message);
}
HttpWebRequest request = null;
try
{
request = (HttpWebRequest)WebRequest.Create(strURL);
}
catch (HttpListenerException httpEx)
{
MessageBox.Show(httpEx.Message);
}
string content = textBox1.Text + xmlDoc.OuterXml;
byte[] byteArray = null;
try
{
byteArray = Encoding.UTF8.GetBytes(content);
}
catch (Exception sysEx)
{
MessageBox.Show(sysEx.Message);
}
request.Method = "POST";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)";
request.ContentType = "application/x-www-form-urlencoded";
request.Credentials = CredentialCache.DefaultNetworkCredentials;
request.ContentLength = byteArray.Length;
request.Timeout = 1000000;
request.KeepAlive = false;
Stream sw = null;
try
{
sw = request.GetRequestStream();
}
catch (Exception streamEx)
{
MessageBox.Show(streamEx.Message);
}
sw.Write(byteArray, 0, byteArray.Length);
sw.Close();
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException WebEx)
{
MessageBox.Show(WebEx.Message);
}
if (response != null)
{
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
this.txtAbstract.Text = sr.ReadToEnd(); ;
}
}
}
}
public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy
{
public TrustAllCertificatePolicy() { }
public bool CheckValidationResult(ServicePoint sp,
X509Certificate cert, WebRequest req, int problem)
{
return true;
}
}
}
Points of Interest
I learned a ton about web requests and responses while building this project.
History
- 07-21-2010: Initial upload of this stub.