通过 http 请求调用 ASP.NET Web 服务方法 [英] Invoking an ASP.NET web service method via an http request

查看:28
本文介绍了通过 http 请求调用 ASP.NET Web 服务方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用 C# 的 http POST 请求调用 ASP.NET Web 服务(即我不想使用通过运行 wsdl.exe 生成的 SoapHttpClientProtocol 对象).

I want to invoke an ASP.NET web service via an http POST request using C# (i.e. I don't want to use the SoapHttpClientProtocol object generated by running wsdl.exe).

据我所知,这个过程包括:

As far as I can tell, the process involves:

  1. 创建一个HttpWebRequest对象,该对象指向Web服务的url/方法,使用方法;

  1. creating an HttpWebRequest object which points to the url/method of the web service, with the method;

创建一个 SOAP xml 信封;

Creating a SOAP xml envelope;

使用 XmlSerializer 序列化我想传递给 Web 方法的任何参数;

Serialising any parameters I want to pass to the web method using an XmlSerializer;

发出请求,并解析响应.

Making the request, and parsing the response.

我想这样做而不必复制和使用生成的代码.

I would like to do this without having to copy and use generated code.

(1) 看起来很简单;

(1) seems pretty straightforward;

(2) 我不知道这里的信封是否是标准的,或者它应该如何根据我调用的网络服务方法而改变.如果服务需要,我想我可能需要添加自定义肥皂头?

(2) I don't know if the envelope here is standard, or how it should change depending on the webservice method I am calling. I guess I might need to add custom soap headers if required by the service?

(3) 这样做的过程是什么?我假设我需要做这样的事情:

(3) What is the process of doing this? I assume that I need to do something like this:

MyClass myObj;
XmlSerializer ser = new XmlSerializer(myObj.GetType());
TextWriter writer = new StringWriter();
ser.Serialize(writer, myObj);
string soapXml = writer.ToString();
writer.Close();

另外,我想我应该将soapXml添加到soap:Body元素中

Also, I guess I should add the soapXml to the soap:Body element

(4) 我相信我也应该提取和反序列化soap:Body 元素的内容.使用(3)中的逆过程可以吗?

(4) I believe I should extract and deserialize the contents of the soap:Body element as well. Is it OK to use the reverse of the process in (3)?

谢谢,

K.

推荐答案

我不知道为什么要这样做,但这里有一个手动调用 Web 服务的示例.请保证永远不会在生产代码中使用它.

I don't know why I am doing this but here's an example of invoking a web service manually. Please promise to never use this in a production code.

假设您有以下 SOAP 服务:

Suppose you had the following SOAP service:

public class Foo
{
    public int Id { get; set; }
    public string Name { get; set; }
}

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service1 : System.Web.Services.WebService
{
    [WebMethod]
    public string HelloWorld(Foo foo)
    {
        return "Hello World";
    }
}

您可以像这样手动调用它:

You can invoke it manually like this:

class Program
{
    static void Main(string[] args)
    {
        using (WebClient client = new WebClient())
        {
            client.Headers.Add("SOAPAction", ""http://tempuri.org/HelloWorld"");
            client.Headers.Add("Content-Type", "text/xml; charset=utf-8");
            var payload = @"<?xml version=""1.0"" encoding=""utf-8""?><soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><soap:Body><HelloWorld xmlns=""http://tempuri.org/""><foo><Id>1</Id><Name>Bar</Name></foo></HelloWorld></soap:Body></soap:Envelope>";
            var data = Encoding.UTF8.GetBytes(payload);
            var result = client.UploadData("http://localhost:1475/Service1.asmx", data);
            Console.WriteLine(Encoding.Default.GetString(result));
        }
    }
}

这篇关于通过 http 请求调用 ASP.NET Web 服务方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆