Sending and receiving data from web service in winform

自闭症网瘾萝莉.ら 提交于 2019-12-13 21:21:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!