【问题标题】:How do I programmatically send information to a web service in C# with .NET?如何使用 .NET 以编程方式将信息发送到 C# 中的 Web 服务?
【发布时间】:2010-12-30 16:31:17
【问题描述】:

我知道这算是重新发明轮子,但我需要知道通过 http/soap/xml 和 web 消息与 web 服务进行通信。原因是我需要与第三方 Web 服务进行通信才能工作,但是 WSDL 出现问题或使用 .NET 向导连接时无法正常工作。

那么,谁能给我一个过程/简单的例子/等等。如何做到这一点,或者任何人都可以给我一个链接到解释它的地方吗?我对网络请求和响应不是很了解。

如何构造和发送请求?如何解析响应?

这是一个简单网络服务的代码。假装.asmx的地址是“http://www.mwebb.com/TestSimpleService.asmx”:

using System;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

namespace TestSimpleService
{
    [WebService]
    public class Soap : System.Web.Services.WebService
    {
        [WebMethod]
        public string SayHello(string name)
        {
            return "Hello " + name + "!";
        }
    }
}

我该如何称呼这个方法?

感谢任何帮助。

编辑

我真的只是想知道如何将数据发送到 Web 服务。我可以获取所有方法/SOAP 操作/URL 数据,并且可以解析响应数据。我只是不知道要使用什么对象或如何使用它们。

如果有人知道一些简单的 .NET soap 客户端,例如 Python 中的 SUDS,那也会有所帮助。

【问题讨论】:

  • @Mike 您不需要在 VS 中使用 WSDL;如果您至少可以通过浏览器访问 WSDL,然后将 WSDL 下载到本地磁盘,则可以使用 wsdl.exe msdn.microsoft.com/en-us/library/7h3ystb6(v=VS.100).aspx
  • 如果您需要一种无 VS 的方法来使用 Web 服务:notepad-webservices.blogspot.com/2006/04/… 但仍然使用 wsdl
  • WSDL 有问题,所以我真的不能使用它。必须有一种方法可以使用 .NET 中的 http/soap 请求/响应对象来发送/接收数据。或者必须有一个简单的库来执行此操作,例如 Python 中的 Suds。
  • VS 有没有告诉你 WSDL 出了什么问题?
  • 网络服务是否以某种形式得到保护——例如如果它是使用 WSE 2.0 的旧 .net 1.1 Web 服务,则更高版本的 .net 将无法使用 WCF 进行互操作

标签: c# .net web-services webrequest


【解决方案1】:

如果您想直接通信,我会考虑使用 HTTPWebRequest,因为最终 Web 服务调用只是使用 HTTP POST 发送的 XML。

以下链接有一些示例:http://geekswithblogs.net/marcel/archive/2007/03/26/109886.aspx

作为在以编程方式与 .net 联系之前测试外部 Web 服务的一种方法,一种方法是使用像 SOAPUI 这样的测试工具来生成您认为需要发布到 Web 服务的确切 XML 并手动发送它那个工具

然后你可以开发 .net 等价物

编辑 - 这是一个快速示例,我根据上面的链接调用您的示例服务(使用 SOAP1.2):

        {
            string soap = @"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
   xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" 
   xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"">
  <soap:Body>
    <SayHello xmlns=""http://tempuri.org/"">
      <name>My Name Here</name>
    </SayHello>
  </soap:Body>
</soap:Envelope>";

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:2439/Soap.asmx");
            req.ContentType = "application/soap+xml;";
            req.Method = "POST";

            using (Stream stm = req.GetRequestStream())
            {
                using (StreamWriter stmw = new StreamWriter(stm))
                {
                    stmw.Write(soap);
                }
            }

            WebResponse response = req.GetResponse(); 
            Stream responseStream = response.GetResponseStream();

            // Do whatever you need with the response
            Byte[] myData = ReadFully(responseStream);
            string s = System.Text.ASCIIEncoding.ASCII.GetString(myData);
        }

ReadFully 方法来自 http://www.yoda.arachsys.com/csharp/readbinary.html,看起来它来自 Jon Skeet。

【讨论】:

  • Using 关于响应的声明?
【解决方案2】:

所选答案的代码对我不起作用。我必须在标题中添加SOAPAction 并更改ContentType

完整代码如下:

var strRequest = @"<soap12:Envelope> 
                    ... 
                    </soap12:Envelope>";

string webServiceUrl = "http://localhost:8080/AccontService.svc";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webServiceUrl);

request.Method = "POST";
request.ContentType = "text/xml;charset=UTF-8";         
request.Accept = "text/xml";
request.Headers.Add("SOAPAction", "http://tempuri.org/IAccountService/UpdateAccount");

byte[] data = Encoding.UTF8.GetBytes(strRequest);

request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string responseXmlString = reader.ReadToEnd();

return new HttpResponseMessage()
{
    Content = new StringContent(responseXmlString, Encoding.UTF8, "application/xml")
};

【讨论】:

    【解决方案3】:

    XML-RPC.NET 允许即时创建绑定。

    例如(他们网站上的一个例子):

    [XmlRpcUrl("http://betty.userland.com/RPC2")]
    public interface IStateName : IXmlRpcProxy
    {
        [XmlRpcMethod("examples.getStateName")]
        string GetStateName(int stateNumber); 
    }
    

    【讨论】:

    • 哦,没有意识到有区别。显示了我在这个问题上的知识:) 是的,我试过了,但没有用。猜猜这就是原因。
    • 所以不能通过这个 XML RPC 库来消费 SOAP? weblog.masukomi.org/writings/xml-rpc_vs_soap.htm
    【解决方案4】:

    如果您的服务真的像您的示例一样简单,那么只需使用“添加服务引用”并使用代理即可。

    如果这不起作用,则使用命令行 svcutil.exe 程序并发布它打印的错误消息。

    除非别无选择,否则不要使用 WSDL.EXE。

    【讨论】:

      猜你喜欢
      • 2016-07-09
      • 1970-01-01
      • 2014-09-06
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-11
      相关资源
      最近更新 更多