如何以编程方式使用.NET将信息发送到Web服务在C#中? [英] How do I programmatically send information to a web service in C# with .NET?

查看:185
本文介绍了如何以编程方式使用.NET将信息发送到Web服务在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这种算作这里重新发明轮子,但我需要知道通过HTTP / SOAP / XML和Web消息的Web服务进行通信。原因是我需要工作的第三方Web服务来沟通,但也有一些是错误的WSDL或东西,连接到它与.NET向导时,这是行不通的。

I know this sort of counts as reinventing the wheel here, but I need to know to communicate with a web service through http/soap/xml and web messages. The reason is I need to communicate with a third party web service for work, but there is something wrong with the WSDL or something and it does not work when connecting to it with the .NET wizard.

那么,谁能给我一个过程/简单的例子/等。如何做到这一点还是谁能给我一个链接到的地方,解释呢?我不是很精明与Web请求和响应。

So, can anyone give me a process/simple example/etc. of how to do this or can anyone give me a link to somewhere that explains it? I'm not very savvy with web requests and responses.

我如何创建和发送请求?我如何解析响应?

How do I construct and send the request? How do I parse the response?

下面是$ C $下一个简单的Web服务。 pretend所述的.asmx的地址是http://www.mwebb.com/TestSimpleService.asmx:

Here is the code for a simple web service. Pretend the address of the .asmx is "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 + "!";
        }
    }
}

我怎么会调用这个方法?

How would I call this method?

任何帮助是AP preciated。

Any help is appreciated.

修改

我真的只是想知道如何将数据发送到Web服务。我可以得到所有的方法/ SOAP动作/ URL数据,我可以解析响应数据。我只是不知道是什么物体使用或如何使用它们。

I really just want to know how to send the data to the web service. I can get all of the method/SOAP action/URL data and I can parse the response data. I just don't know what objects to use or how to use them.

如果有人知道几个简单的.NET SOAP客户端像肥皂水在Python中,这将有助于太多。

If anyone knows of a few simple .NET soap clients like SUDS in Python, that would help too.

推荐答案

如果您想直接沟通,我会考虑使用一个HttpWebRequest的作为最终一个Web服务调用仅仅是XML使用HTTP POST发送的。

If you want to communicate directly, I'd look into using an HTTPWebRequest as ultimately a webservice call is just XML sent using an HTTP POST.

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

The following link has some examples: http://geekswithblogs.net/marcel/archive/2007/03/26/109886.aspx

至于编程与.NET的一种方式接触之前测试的外部Web服务的方法是使用一个测试工具,如 SOAPUI 的产生,你认为需要发布到web服务,并手动与工具发送的确切XML

As a way of testing the external webservice before contacting it programmatically with .net one way is to use a test tool like SOAPUI to produce the exact XML you think needs to be posted to the webservice and to send it manually with that tool

然后就可以开发.NET相当于

Then you can develop the .net equivalent

编辑 - 这里有一个简单的例子,我敲了起来打电话给你的示例服务(使用SOAP1.2)根据上面的链接:

EDIT - here's a quick example I knocked up to call your example service (using SOAP1.2) based on the link above:

        {
            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 和看起来像它源自乔恩斯基特。

The ReadFully method comes from http://www.yoda.arachsys.com/csharp/readbinary.html and looks like it originated from Jon Skeet.

这篇关于如何以编程方式使用.NET将信息发送到Web服务在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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