简体   繁体   English

C#ASP.Net:如何在不使用WSDL或代理类的情况下在.NET 4.0 C#中调用Web服务

[英]C# ASP.Net : How to call a web service in .NET 4.0 C# without using the WSDL or proxy classes

I have to send some XML DATA to web service via POST Command , I have no information about the webserice or anything at all , i only know that i need to send a XMLstream to it , i know the server IP adress and the port , the XML tree of the parameters. 我必须通过POST命令将一些XML DATA发送到Web服务,我完全没有关于Webserice或任何信息的信息,我只知道我需要向其发送XMLstream,我知道服务器IP地址和端口,参数的XML树。 and that all , I am a bet lost on how to consumme this web service and get the response . 而且,我敢打赌如何使用此Web服务并获得响应。 I've never worked with Web services so please understand if my code have some major errors and kindly guide me : 我从未使用过Web服务,因此请理解我的代码是否存在一些重大错误,并为我提供指导:

public  void Execute()
    {
        HttpWebRequest request = CreateWebRequest();
        XmlDocument soapEnvelopeXml = new XmlDocument();
        soapEnvelopeXml.LoadXml(@"<?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> 

                    <mergeprofiles xmlns=""http://127.0.0.1:90"">
                    <Profile>
                    <Member>278615</Member>
                    <ExtRef>003D000001StY68IAF</ExtRef>zs 
                    <Name>LESCA</Name>
                    <ChangedBy>user</ChangedBy>
                    <FirstName>Paolo</FirstName>
                    <Street1>via magnanina 3947</Street1>
                     <Street2></Street2>
                    <Street3></Street3>
                    <City>VIGEVANO</City>
                    <ZIP>27029</ZIP>
                    <EMail>test@test.it</EMail>
                    <Phone></Phone>
                    <MobilePhone>+39331231233</MobilePhone>
                    <Fax></Fax>
                    <ISOLanguage>en</ISOLanguage>
                    <Salut1>Mr</Salut1>
                    <Homepage></Homepage>
                    <VATNo1>0</VATNo1>
                    </Profile>
                    <EndOfMessage>@@@</EndOfMessage>
                    </mergeprofiles>

</soap:Body>

</soap:Envelope>");


using (Stream stream = request.GetRequestStream())
        {
            soapEnvelopeXml.Save(stream);
        }

        using (WebResponse response = request.GetResponse())
        {
            using (StreamReader rd = new StreamReader(response.GetResponseStream()))
            {
                string soapResult = rd.ReadToEnd();
                Console.WriteLine(soapResult);
            }
        }
    }
    /// <summary>
    /// Create a soap webrequest to [Url]
    /// </summary>
    /// <returns></returns>
    public HttpWebRequest CreateWebRequest()
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"http://127.0.0.1:90/");
        webRequest.Headers.Add(@"SOAP:Action");
        webRequest.ContentType = "text/xml;charset=\"utf-8\"";
        webRequest.Accept = "text/xml";
        webRequest.Method = "POST";
        return webRequest;
    }

The problem is when ever I call the method Execute in page_load , it runs and runs and never stops. 问题是,每当我在page_load调用方法Execute时,它就会运行,并且永远不会停止。 The web service is working when I call using other methods, I don't know how or why is that happening, can you please help ? 当我使用其他方法进行调用时,Web服务正在运行,我不知道这种情况如何或为什么发生,请您帮忙吗?

Try following some of the steps from this article: 尝试按照本文中的一些步骤操作:

http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client

WebAPI is the latest round of .NET's service frameworks, and it differs quite a lot from the code-generated SOAP classes. WebAPI是.NET服务框架的最新一轮,它与代码生成的SOAP类有很大的不同。 Of particular use to you will probably be the following method: 以下方法可能对您特别有用:

response = await client.PostAsync("your url", new StreamContent(yourStream));

If you are using < .NET 4.5, you may not be able to use this syntax. 如果使用的是<.NET 4.5,则可能无法使用此语法。 In that case, try using this answer instead: 在这种情况下,请尝试使用以下答案:

.NET: Simplest way to send POST with data and read response .NET:发送带有数据的POST和读取响应的最简单方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Asp.net C#异步调用Web Service方法 - Asp.net C# Asynchronous call to Web Service method 最简单的C#客户端无需使用添加服务参考即可调用包含简单类型的ASP.Net Web服务 - Easiest C# client to call an ASP.Net web service containing simple types without using Add Service Reference 如何在 c# asp.net 内核中使用 web 服务 - How to consume a web service in c# asp.net core 如何从 ASP.Net C# 中的其他项目或解决方案调用 Web 服务 - How to call a web service from the other project or solution in ASP.Net C# 使用ASP .NET(C#)的wsdl解析器 - wsdl Parser using asp .net (C#) 如何使用 javascript 调用 ASP.NET c# 方法 - how to call an ASP.NET c# method using javascript 如何在 WSDL 中提供没有 &#39;elementformdefault = “qualified”&#39; 属性的 C# .Net Web 服务? - How to provide a C# .Net Web Service without an ‘elementformdefault = “qualified”’ attribute in the WSDL? 如何使用 ASP.NET ZD7EFA19FBE7D2372FD5ADB6024 ajax 在 web 服务完成时生成警报 - How to generate alert on completion of web service with ajax using ASP.NET C# 如何在asp.net C#4.0中调用异步方法? - How do I call async methods in asp.net C# 4.0? 如何在ASP.NET 4.0,C#Web应用程序中在线查看MS Office文件 - How to view MS office files online in asp.net 4.0, C# web application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM