2014-12-05 64 views
0

我正在尝试将在线SOAP Web服务添加到我在Visual Studio中的Web应用程序中。我尝试了几个教程,但大多数教程着重于在本地主机上创建服务器。C#ASP.Net添加在线Web服务

在我的web应用程序中,我想使用以下url从web服务中获取所有国家/地区。 http://www.webservicex.net/country.asmx?op=GetCountries

它提供了以下SOAP请求和响应。

请求

POST /country.asmx HTTP/1.1 
Host: www.webservicex.net 
Content-Type: application/soap+xml; charset=utf-8 
Content-Length: length 

<?xml version="1.0" encoding="utf-8"?> 
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> 
    <soap12:Body> 
    <GetCountries xmlns="http://www.webserviceX.NET" /> 
    </soap12:Body> 
</soap12:Envelope> 

响应

HTTP/1.1 200 OK 
Content-Type: application/soap+xml; charset=utf-8 
Content-Length: length 

<?xml version="1.0" encoding="utf-8"?> 
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> 
    <soap12:Body> 
    <GetCountriesResponse xmlns="http://www.webserviceX.NET"> 
     <GetCountriesResult>string</GetCountriesResult> 
    </GetCountriesResponse> 
    </soap12:Body> 
</soap12:Envelope> 

我如何添加Web服务引用,

在Visual Studio中,我在我的项目点击 - >添加 - >服务参考 - >高级选项卡 - >添加Web引用 - >输入Web服务的URL(http://www.webservicex.net/country.asmx)。

enter image description here

我的问题是如何从Web服务的国家。我尝试了几种方法,但没有成功。 如何从我的asp文件中调用服务。

+0

你有没有尝试过使用任何其他类型的Web服务?它必须是肥皂..?你可以使用Linq的Sql Web服务..肥皂是如此当老旧和过时..'WCF的网络服务更容易 – MethodMan 2014-12-05 21:34:51

+0

那么,我的要求说我必须使用SOAP。 – Isuru 2014-12-05 21:51:45

+0

我知道这是你的老板想用SOAP编写的东西,因为你通过绕过'SOAP'生成有效的XML,无论如何,我认为你的问题涉及不正确地使用/实例化Web服务和/或代理 – MethodMan 2014-12-05 21:54:25

回答

1

您实例化建,然后调用一个成员函数的类:

protected void btn_Click(object sender, EventArgs e) 
{ 
    ServiceReference1.countrySoapClient Client = new ServiceReference1.countrySoapClient("countrySoap12"); 

    String Countries = Client.GetCountries(); 
} 

酷,返回的字符串看起来像从一个DataSet导出XML。你应该可以将它读入一个新的DataSet ....然后将它绑定到某个东西。