65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 HTTP POST 的 Web 服务测试存根

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (1投票)

2010年7月21日

CPOL

1分钟阅读

viewsIcon

28823

downloadIcon

536

如何创建一个通过 HTTP POST 将信息发布到 Web 服务的测试桩。

引言

这个测试桩解决了通过允许你将 XML 文件作为 HTTP POST 参数发布到 Web 服务方法来测试 Web 服务的难题。 总体而言,编译后,你应该能够将该工具用于任何 Web 服务 URI 和方法。

背景

任何对 Web 服务新手的人都应该从了解基本技术开始。 W3Schools (https://w3schools.org.cn/webservices/default.asp) 提供了大量教程供你入门。 我在查看 soapUI 产品后,决定需要自己编写一个测试应用程序。 问题是我需要使用 HTTP POST 事件来测试我的应用程序。 这个工具就是这个需求的产物。

丑陋但有用

使用代码

将代码插入到你的按钮点击事件中即可开始使用。 代码将说明 Web 请求/响应的结构。 我添加了另一个类来覆盖你在访问 SSL 站点时可能使用的任何证书。 你应该验证目标 URI 是否可以从客户端机器上的 Web 浏览器中查看。 代码不会安装 SSL 证书。 如果需要,你应该打开 SSL 站点并使用浏览器安装证书。 该工具可以用于 HTTP 和 HTTPS URI。

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;
    }
}
}

关注点

我在构建这个项目时学到了很多关于 Web 请求和响应的知识。

历史

  • 2010-07-21:首次上传此测试桩。
© . All rights reserved.