Click here to Skip to main content
13,353,462 members (67,618 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 21 Jul 2010

Web Service Testing Stub Using HTTP POST

, 21 Jul 2010
Rate this:
Please Sign up or sign in to vote.
How to create a stub that posts information to a Web Service via HTTP POST.


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.


Anyone new to Web Services will want to start by understanding the basic technology. W3Schools ( 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()

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

        //go find the raw XML
        this.openFileDialog1.Filter = "XML files (*.xml) | *.xml";
        //load the XML
        catch (XmlException xmlEx)

        //Create the Web request
        HttpWebRequest request = null;

            request = (HttpWebRequest)WebRequest.Create(strURL);
        catch (HttpListenerException httpEx)

        //Protocol Header -> DISEventPost=<xml...
        string content = textBox1.Text + xmlDoc.OuterXml;
        byte[] byteArray = null;

            byteArray = Encoding.UTF8.GetBytes(content);
        catch (Exception sysEx)

        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;
            sw = request.GetRequestStream();
        catch (Exception streamEx)
        sw.Write(byteArray, 0, byteArray.Length);

        //catches the http response
        HttpWebResponse response = null;
            response = (HttpWebResponse)request.GetResponse();
        catch (WebException WebEx)

        //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.


  • 07-21-2010: Initial upload of this stub.


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


About the Author

Tommie Carter
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.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 3 Pin
RobertCooley21-Jul-10 22:59
memberRobertCooley21-Jul-10 22:59 
GeneralRe: My vote of 3 Pin
Tommie Carter22-Jul-10 10:02
memberTommie Carter22-Jul-10 10: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 21 Jul 2010
Article Copyright 2010 by Tommie Carter
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid