在 .NET 中使用带有 http 请求响应的 Web 服务?

发布于 2024-11-14 16:42:44 字数 113 浏览 1 评论 0原文

我需要创建 xml 消息并将其发送到 Web 服务。然后我应该通过查看来自服务的响应 xml 来处理响应。我以前使用过 WCF,但我应该使用旧样式。

我应该从哪里开始?

提前致谢。

I need to create xml message and send it to the web service. Then I should handle the response by looking at the response xml that is coming from service. I have used WCF before but I should do it with old style.

Where should I start ?

Thanks in advance.

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

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

发布评论

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

评论(3

酒与心事 2024-11-21 16:42:44

下面是一些基本的 C# 代码,可以执行您想要的操作,其中 url 是您正在调用的 Web 服务的 URL,action 是服务的肥皂操作,信封是包含请求的肥皂信封的字符串:

WebRequest request = CreateHttpRequestFromSoapEnvelope(url, action, envelope);
WebResponse response = request.GetResponse();

private WebRequest CreateHttpRequestFromSoapEnvelope(string url, string action, string envelope)
{
    WebRequest request = WebRequest.Create(new Uri(url));
    request.Method = "POST";
    request.ContentType = "text/xml";
    request.Headers.Add(action);
    ServicePointManager.Expect100Continue = false;

    ApplyProxyIfRequired(request);

    using (Stream stream = request.GetRequestStream())
    {
        using (StreamWriter streamWriter = new StreamWriter(stream))
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            builder.Append(envelope);
            string message = builder.ToString();
            streamWriter.Write(message);
        }
    }

    return request;
}

Here's some basic C# code that does what you want, where url is the URL of the web service you're calling, action is the soap action of the service and envelope is a string containing the soap envelope for the request:

WebRequest request = CreateHttpRequestFromSoapEnvelope(url, action, envelope);
WebResponse response = request.GetResponse();

private WebRequest CreateHttpRequestFromSoapEnvelope(string url, string action, string envelope)
{
    WebRequest request = WebRequest.Create(new Uri(url));
    request.Method = "POST";
    request.ContentType = "text/xml";
    request.Headers.Add(action);
    ServicePointManager.Expect100Continue = false;

    ApplyProxyIfRequired(request);

    using (Stream stream = request.GetRequestStream())
    {
        using (StreamWriter streamWriter = new StreamWriter(stream))
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            builder.Append(envelope);
            string message = builder.ToString();
            streamWriter.Write(message);
        }
    }

    return request;
}
陪我终i 2024-11-21 16:42:44

如果您不想使用 WCF / ASMX 客户端,您应该首先学习 HTTP 和 SOAP (< a href="http://www.w3.org/TR/2000/NOTE-SOAP-20000508/" rel="nofollow">1.1, 1.2) 了解 POST 请求以及消息构造和读取所需的 HTTP 标头 + HttpWebRequest。这样做没有意义 - 坚持使用 WCF 或 ASMX (这实际上是老方法)。

If you don't want to use WCF / ASMX clients you should start by learning HTTP and SOAP (1.1, 1.2) to understand needed HTTP headers for POST requests and message construction and reading + HttpWebRequest. Doing it this way doesn't make sense - stick with WCF or ASMX (that is actually the old way).

握住你手 2024-11-21 16:42:44

添加对 Web 服务的引用。 Visual Studio 将为您创建类,这样您就不需要自己创建 XML 请求并解析 XML 响应。
检查此链接 http://msdn.microsoft.com /en-us/library/d9w023sx(v=VS.90).aspx

Add a reference to the web service. Visual Studio will create classes for you so that you don't need to create the XML request and parse the XML response yourself.
Check this link http://msdn.microsoft.com/en-us/library/d9w023sx(v=VS.90).aspx

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