Click here to Skip to main content
15,896,359 members
Articles / Desktop Programming / Windows Forms

Web Service Testing Stub Using HTTP POST

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
21 Jul 2010CPOL1 min read 28.4K   520   9   2
How to create a stub that posts information to a Web Service via HTTP POST.

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.

C#
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();
        //the URL is on the application
        string strURL = this.txtURL.Text;

        //go find the raw XML
        this.openFileDialog1.Filter = "XML files (*.xml) | *.xml";
        this.openFileDialog1.ShowDialog();
        //load the XML
        try
        {
            xmlDoc.Load(this.openFileDialog1.FileName.ToString());
        }
        catch (XmlException xmlEx)
        {
            MessageBox.Show(xmlEx.Message);
        }

        //Create the Web request
        HttpWebRequest request = null;

        try
        {
            request = (HttpWebRequest)WebRequest.Create(strURL);
        }
        catch (HttpListenerException httpEx)
        {
            MessageBox.Show(httpEx.Message);
        }

        //Protocol Header -> DISEventPost=<xml...
        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;

        //sends the data onto the http pipe
        Stream sw = null;
        try
        {
            sw = request.GetRequestStream();
        }
        catch (Exception streamEx)
        {
            MessageBox.Show(streamEx.Message);
        }
        sw.Write(byteArray, 0, byteArray.Length);
        sw.Close();

        //catches the http response
        HttpWebResponse response = null;
        try
        {
            response = (HttpWebResponse)request.GetResponse();
        }
        catch (WebException WebEx)
        {
            MessageBox.Show(WebEx.Message);
        }

        //writes the respose to the text field
        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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Hi all,

I am a long-time coder. Since 8th grade with Apple and Commodore Pet! Now, I'm a computer consultant and software developer in new york, ny.

Comments and Discussions

 
GeneralMy vote of 3 Pin
Robert Cooley21-Jul-10 21:59
Robert Cooley21-Jul-10 21:59 
GeneralRe: My vote of 3 Pin
Tommie Carter22-Jul-10 9:02
Tommie Carter22-Jul-10 9:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.