[英]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.