在 ASMX 中测试自定义 SOAP 标头

发布于 2024-07-06 16:42:59 字数 93 浏览 4 评论 0原文

ASMX生成的测试表单对于测试操作来说非常方便。 然而,没有明显的方法来包含 SOAP 标头。

如何在不编写客户端程序来使用该服务的情况下测试标头?

The test form generated by ASMX is pretty handy for testing operations. However, there is no apparent way to include SOAP headers.

How can you test your headers without programming a client to use the service?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

把人绕傻吧 2024-07-13 16:42:59

如果您关心互操作性,请不要使用 .net 客户端应用程序来测试 .net Web 服务。 使用 SOAPUI 等适当的工具来测试您的 Web 服务。 www.soapui.org

该工具是用 Java 编写的,但它是免费的,并且非常方便测试任何类型的网络服务。

If you care about interop, don't use .net client apps to test .net web services. Use a proper tool like SOAPUI to test your web service. www.soapui.org

The tool is written in Java, but it is free and darn handy for testing any kind of web service.

淡莣 2024-07-13 16:42:59

如果我明白您想要做什么,您可以使用 asmx 提供的 SOAP 执行 HTTP 请求。 如果您在浏览器中打开 asmx,您将获得 Web 服务中可用方法的列表。 点击你想要测试的方法,你会得到一个可以使用的SOAP请求,只需填写你想要测试的值即可。 下面是可用于测试 SOAP 的代码。

// Set SOAP Message
string msg = "<?xml version='1.0' encoding='UTF-8'?><soap:Envelope>";
...
...

// Make http request
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://linktoyour/service.asmx");

req.Headers.Add("SOAPAction", "http://linktoyour/NameOfFuntion");

req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(msg);

req.ContentLength = bytes.Length;

System.IO.Stream st = req.GetRequestStream();
st.Write(bytes,0,bytes.Length);
st.Close();

// Read response
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
System.IO.Stream st1 = res.GetResponseStream();

System.IO.StreamReader sr = new System.IO.StreamReader(st1, System.Text.Encoding.UTF8);

string txt = sr.ReadToEnd();

// Display response
Response.Write(txt);

If I understand what you are trying to do, you can can just do an HTTP request using the SOAP that is provided by your asmx. If you open your asmx in the broswer, you get a list of methods available in your web service. Click on the method you want to test and you will get a SOAP request you can use, just fill in the values you want to test. Below is the code you can use to test the SOAP.

// Set SOAP Message
string msg = "<?xml version='1.0' encoding='UTF-8'?><soap:Envelope>";
...
...

// Make http request
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://linktoyour/service.asmx");

req.Headers.Add("SOAPAction", "http://linktoyour/NameOfFuntion");

req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(msg);

req.ContentLength = bytes.Length;

System.IO.Stream st = req.GetRequestStream();
st.Write(bytes,0,bytes.Length);
st.Close();

// Read response
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
System.IO.Stream st1 = res.GetResponseStream();

System.IO.StreamReader sr = new System.IO.StreamReader(st1, System.Text.Encoding.UTF8);

string txt = sr.ReadToEnd();

// Display response
Response.Write(txt);
囍笑 2024-07-13 16:42:59

你问

如何在不编写客户端程序来使用该服务的情况下测试标头?

答案是您应该对客户端进行编程以使用该服务。

编写代码来使用您的服务的开发人员将被迫编写一个客户端,该客户端使用您设计糟糕、难以使用的服务及其奇怪的标头。 你最好在他们发现之前发现{设计糟糕、难以使用、奇怪}。

这样,您就可以将服务重新设计为{精心设计、使用愉快、正常}。

顺便说一句,为您的 Web 服务编写单元测试是一个好方法。 这样,你就拥有了小而简单的客户。

You asked

How can you test your headers without programming a client to use the service?

The answer is that you should program a client to use the service.

The developers who will write the code to consume your service are going to be forced to write a client that uses your badly-designed, hard-to-use service with its strange headers. It's better you find out about {badly-designed, hard-to-use, strange} before they do.

That way, you can redesign the service to be {well-designed, pleasure-to-use, normal}.

BTW, writing unit tests for your web service is a good way to do this. That way, you've got small, simple, clients.

睫毛溺水了 2024-07-13 16:42:59

不要直接访问标头,而是提供代码访问的抽象。 例如,如果您有一个名为“Customer”的标头,您可以提供一个可以访问的上下文类,如下所示:

string customer = MyContext.Current.Customer;

现在,您所要做的就是在测试类中交换不需要所有管道的模拟实现。

但请注意,在 Web 服务器之外测试 ASMX 并不完全理想,因为它可能会错过序列化等内容。 如果您可以部署到测试服务器并测试部署的副本,那么情况会更好。 如果您真的关心测试,WCF 是一个更好的选择,因为您可以在测试中相当轻松地自行托管 WCF。

Instead of directly accessing the headers, provide an abstraction that your code accesses instead. For example, if you have a header called "Customer", you might provide a context class you can access like so:

string customer = MyContext.Current.Customer;

Now, all you have to do is swap in a mock implementation in your test classes that doesn't require all the plumbing.

Note, however, that testing ASMX outside of a web server isn't exactly ideal since it can miss things like serialization. If you could deploy to a test server and test the deployed copy, you would be better off. If you really care about testing, WCF is a better option since you can self host WCF rather easily in tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文