简体   繁体   English

通过Microsoft Web API端点调用Web服务 - asmx?

[英]Invoking a web service - asmx - through a Microsoft Web API end point?

I am using Microsoft ASP.NET Web API 2 and one of my end points has to internally invoke a legacy asmx web service. 我正在使用Microsoft ASP.NET Web API 2 ,我的一个端点必须在内部调用传统的asmx Web服务。

Is this the correct way? 这是正确的方法吗?

...................................................
WebRequest req = WebRequest.Create(myWebServiceURL);

req.Method = "POST";
eq.ContentType = "application/x-www-form-urlencoded";

string postData = whateverDataNeedsToBePosted;

using ( Stream reqStream = req.GetRequestStream() )
      {
            reqStream.Write( new ASCIIEncoding().GetBytes( postData ),
                              0, postData.Length );
            reqStream.Close();
       }
 WebResponse resp = req.GetResponse();

................................................ ................................................

UPDATE: I do have a bunch of non-Microsoft technology web services (no asnx or svc). 更新:我确实有一堆非Microsoft技术Web服务(没有asnx或svc)。 Is the above method good enough for those type of services? 上述方法对于那些类型的服务是否足够好?

That will work, but you're making your life difficult :) 这会奏效,但你的生活很艰难:)

You can add a Service Reference to your project in Visual Studio and call methods in the ASMX service just like you would call methods in a referenced DLL. 您可以在Visual Studio中向项目添加服务引用 ,并在ASMX服务中调用方法,就像调用引用的DLL中的方法一样。 See this article . 看到这篇文章

UPDATE: 更新:

Yes, your method for calling other services will work, but I would check to see if adding your other services as Service References to your project works first. 是的,您调用其他服务的方法可行,但我会检查是否将您的其他服务作为服务引用添加到您的项目中。 The service reference feature works with all kinds of protocols (whether built with Microsoft technologies or not). 服务引用功能适用于各种协议(无论是否使用Microsoft技术构建)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM