Tuesday, February 23, 2010

C# - example of using Web Service

Assume a web service (with .ASMX file) have been created and registered properly.

Below please find steps to utilize the Web Serice;

1) Use "Add Reference --> Add Web reference" to add the web service by using "Visual Studio" (to specify the URL of the ASMX file, e.g.: http://xxx/sample.asmx). It will create a name space of the service.

2) Use code as below to call function(s) provided by the service:

try
{
//身份验证
cn.xxx.MySoapHeader soapHeader = new cn.xxx.MySoapHeader();
soapHeader.UserName = "test";
soapHeader.PassWord = "test";
string FileName = "";
byte[] AccessoriesByte =null;

cn.xxx.SendEmail service = new cn.xxx.SendEmail();
service.MySoapHeaderValue = soapHeader;

int result=service.SendXXXEmail("111", "222");
if (result==1) MessageBox.Show("Success"); }
else MessageBox.Show("Failure");+ "]");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
...
}

No comments: