0

我编写了一个 WinForm 应用程序,它应该使用 Web 服务来检索所需的信息。地址是这样的:

我在这里和 msdn 中阅读了一些文章,还为我的项目添加了服务参考,但是当我想使用我不能使用的方法时。

现在我很困惑。我需要将用户名和密码发送给服务,并在验证后发送一个 ID,Web 服务发回适当的信息,然后我需要发回我收到的日志。可以通过 WinForm 吗?我该怎么做?

一些示例代码或参考将被应用。

4

1 回答 1

0

添加新的 Web 服务应用程序项目(名称设置为 SoapHeaderAuth)并添加代码,如下所示

 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";
     }
  }
 }

将 Web 引用添加到上述 Web 服务应用程序,在“添加 Web 引用”对话框中将 Web 引用名称指定为 localhost。接下来,将以下代码粘贴到客户端中,客户端可以是窗口形式或 Web 应用程序。

     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();
于 2013-09-03T06:44:14.433 回答