在winform中从web服务发送和接收数据 [英] Sending and receiving data from web service in winform

查看:71
本文介绍了在winform中从web服务发送和接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wrote a WinForm application which should use a Web service to retrieve needed information. the address is this :

我在这里和 msdn 中阅读了一些文章,并在我的项目中添加了服务引用,但是当我想使用这些方法时却无法使用.

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.

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

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.

推荐答案

添加新的 Web Service Application 项目(名称设置为 SoapHeaderAuth)并添加代码,如下所示

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

将 Web 引用添加到上述 Web 服务应用程序,在添加 Web 引用"对话框中将 Web 引用名称指定为 localhost.接下来,将以下代码粘贴到 Client 中,它可以是窗口窗体或 Web 应用程序.

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();

这篇关于在winform中从web服务发送和接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆