2014-12-05 57 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 ....然後將它綁定到某個東西。