简体   繁体   English

如何调用不在.NET中开发的Web服务

[英]How to call a webservice that wasn't developed in .NET

In the past, I have had experience calling WCF services or ASMX services, you just add service reference to the .svc or .asmx url and it generates the proxy for you, and then you have intellisense for calling the methods. 过去,我曾有过调用WCF服务或ASMX服务的经验,您只需将服务引用添加到.svc或.asmx url,然后它会为您生成代理,然后您便具有了智能感知来调用这些方法。

Is it the same for a non .net webservice? 非.net Web服务是否相同?

I need to call this http://www.earthtools.org/webservices.htm 我需要将此称为http://www.earthtools.org/webservices.htm

from c# code in a winrt app, but I just dont know how 从winrt应用程序中的c#代码中获取,但是我只是不知道如何

If you have WSDL then you can add reference to that service. 如果您有WSDL,则可以添加对该服务的引用。 but here you try to call REST service. 但是在这里您尝试调用REST服务。

you need to send post request and rad the response as: 您需要发送发帖请求并将响应发送为:

private const string BASE_URL = "http://www.earthtools.org/timezone";
private const string REQUEST_URL_FORMAT = "{0}/{1}/{2}";

public timezone GetTimeZone(double latitude, double longitude)
{
    var uriString = String.Format(REQUEST_URL_FORMAT, BASE_URL, latitude, longitude);
    var requestUri = new Uri(uriString);

    var request = WebRequest.Create(requestUri);
    using (var response = request.GetResponse())
    {
        using (var responseStream = response.GetResponseStream())
        {
            var ser = new XmlSerializer(typeof (timezone));
            var result = (timezone) ser.Deserialize(responseStream);
            return result;
        }
    }
}

example from here . 这里的例子。

If they were SOAP web services, then you could still point Visual Studio to the .WSDL endpoint and have it generate the client proxy. 如果它们是SOAP Web服务,则仍可以将Visual Studio指向.WSDL端点,并使其生成客户端代理。 But these appear to be REST services. 但是这些似乎是REST服务。 I think your best bet is to use XDocument.Load or somesuch, and then use Linq-to-XML or XML deserialization to convert the XML into C# classes. 我认为您最好的选择是使用XDocument.Load或诸如此类,然后使用Linq-to-XML或XML反序列化将XML转换为C#类。

For example, let's say you want to call this service . 例如,假设您要调用此服务 An easy way to get lat/lng result would be something like this: 一个简单的方法来获得经纬度结果将是这样的:

XDocument xdoc = XDocument.Load("http://www.earthtools.org/height/52.4822/-1.8946");
var lat = (decimal)xdoc.Element("location").Element("latitude");
var lng = (decimal)xdoc.Element("location").Element("longitude");

Microsoft Marketing guy's will tell that web services are interoperable, even with heterogeneous system. 微软营销专家会告诉您,即使使用异构系统,Web服务也可以互操作。

It's half the truth. 这是事实的一半。 Web services standard is actually multiplatform. Web服务标准实际上是多平台的。 On the field, however, it can be a bit more complicated. 但是,在现场,情况可能会有些复杂。 Different philosophy of different systems can lead to different behaviors. 不同系统的不同理念可能导致不同的行为。

Not long ago, I add to query a Web service built with Java Web logic (not sure of the system, I was on the client side) with Visual Studio 2010. It was a bit of pain because : 不久前,我添加了查询使用Visual Studio 2010用Java Web逻辑构建的Web服务(不确定系统,我是客户端)。这有点麻烦,因为:

  1. Visual Studio proxy wizard only understand inline schema. Visual Studio代理向导仅了解内联架构。 Here the schema was references within the wsdl using <xsd:import> . 在这里,架构是使用<xsd:import>在wsdl中的引用。 Visual Studio does not understand that Visual Studio不明白
  2. Then I moved to svcutil to generate from command line the proxies. 然后,我移至svcutil以从命令行生成代理。 This was not working directly, because of some errors in the WSDL file (this file was manually build by the service provider). 由于WSDL文件中的某些错误(此文件是由服务提供商手动构建的),因此无法直接运行。 I have to ask to the service provider to clean it's wsdl file 我必须请服务提供商清理它的wsdl文件
  3. the xsd file representing the DTOs were shared by several web services. 代表DTO的xsd文件已由多个Web服务共享。 This lead to duplicate the same classes in each proxies, even if not used by all services. 即使没有被所有服务使用,这也会导致每个代理中重复相同的类。 I had to tweak with partial classes to "rewrap" into unique classes. 我不得不调整部分类以“重新包装”为唯一的类。

To conclude, you can the limit of the term "Interoperability". 总之,您可以限制术语“互操作性”。 The standards say it's interoperable, but the field says not always. 标准说它是可互操作的,但是领域并不总是这样。

I welcome the newcoming world of "rest" services. 我欢迎“休息”服务的新世界。 This can simplify a lot the interoperability. 这样可以简化很多互操作性。 The drawback is that you may have to build yourself a part of the plumbing. 缺点是您可能必须自己构建管道的一部分。

Good news, the service you mentioned seems to embrace this rest philosophy. 好消息,您提到的服务似乎也包含了这种休息理念。 It won't be straightforward, but you can build simple Http request (post, get or put) to query this service. 这不会很简单,但是您可以构建简单的Http请求(发布,获取或放置)以查询此服务。

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

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