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

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

问题描述

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

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对象指向该网络服务的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.

我想做到这一点无需进行复制和使用产生code。

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

(1)似乎pretty简单;

(1) seems pretty straightforward;

(2)我不知道这里的​​信封标准或应该如何根据不同的web服务方法我打电话更改。我想,如果要求的服务,我可能需要添加自定义的SOAP头?

(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)我相信我应该提取和反序列化肥皂的内容: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服务的一个例子。请保证在生产code从来不使用这个。

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天全站免登陆