问题
I wrote a WinForm application which should use a Web service to retrieve needed information. the address is this :
I read some articles here and in msdn and also added service reference to my project but when I want to use the methods I can't.
Now I've got confused. I need to send username and password to service and after authenticating send an ID and the web service sends back the appropriate information then I need to send back the log of what I've received. Is it possible through WinForm ? How can I do it?
Some sample code or reference would be appriciatted.
回答1:
Add the new Web Service Application project (with name set as SoapHeaderAuth) and add the code, as given below
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Configuration;
[WebService(Namespace ="www.XMLWebServiceSoapHeaderAuth.net")
][WebServiceBinding(ConformsTo =WsiProfiles.BasicProfile1_1)]
public class Service:System.Web.Services.WebService
{
public AuthSoapHd spAuthenticationHeader;
public Service()
{
}
public class AuthSoapHd: SoapHeader
{
public string strUserName;
public string strPassword;
}
[WebMethod,SoapHeader("spAuthenticationHeader")]
public string HelloWorld()
{
if (spAuthenticationHeader.strUserName =="TestUser" &&
spAuthenticationHeader.strPassword =="TestPassword")
{
return "User Name : " +spAuthenticationHeader.strUserName + " and " +
"Password : " +spAuthenticationHeader.strPassword;
}
else
{
return "Access Denied";
}
}
}
Add the Web Reference to the above web service application, specifying Web Reference Name as localhost in the Add Web Reference dialog. Next, paste the following code in Client which may be either window form or web application.
localhost.Service objWebService = newlocalhost.Service();
localhost.AuthSoapHd objAuthSoapHeader = newlocalhost.AuthSoapHd();
string strUsrName ="your usernmaen"
string strPassword ="Your password"
objAuthSoapHeader.strUserName = strUsrName;
objAuthSoapHeader.strPassword = strPassword;
objWebService.AuthSoapHdValue =objAuthSoapHeader;
string str = objWebService.HelloWorld();
来源:https://stackoverflow.com/questions/18585536/sending-and-receiving-data-from-web-service-in-winform