标题:vb调用 c#.net 需要用户验证的webservice方法
只看楼主
elppa
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-9-20
 问题点数:0 回复次数:0 
vb调用 c#.net 需要用户验证的webservice方法
服务器端用的是.net2005开发的很简单,用c#.net调用一点问题也没有,但vb6就不那么听话了。问题并不是因为vb6读取不到WebService方法,而是WebService进行加密后vb6的soapheader就不知道怎么用了

以下为服务器端的代码(测试地址:"http://127.0.0.1/webtest/WebService.asmx")

Credentials.cs
程序代码:
using System;
using System.Web.Services.Protocols;

namespace TestWebService
{
    public class Credentials:SoapHeader
    {
        public string Name;
        public string Password;
    }
}


WebService.asmx 的代码为
程序代码:
using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using  TestWebService;

namespace Web
{
    /// <summary>
    /// WebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://)]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class WebService : System.Web.Services.WebService
    {

        public WebService()
        {
        }


        public Credentials token;
        [WebMethod(Description = "带soap头的webservice")]
        [SoapHeader("token", Direction = SoapHeaderDirection.In)]
        public string HelloWorld(string username)
        {
            string name = username;
            if (token.Name == "admin" && token.Password == "123")
            {
                return "Hello World" + username;
            }
            else
            {
                throw new ApplicationException("Authentication Failed!");
            }
        }
        [WebMethod]
        public string GetTest(string str)
        {
            return "test"+"--"+str;
        }
    }
}




在.net的客房端调用
程序代码:
using System;
using System.Collections.Generic;
using System.Text;

namespace test
{
    public class MainClass
    {
        static void Main()
        {
            Console.Write(web());
            Console.ReadLine();
        }
        public static string web()
        {
            localhost.WebService myweb = new test.localhost.WebService();
            localhost.Credentials token = new test.localhost.Credentials();
            token.Name = "admin";
            token.Password = "123";
            myweb.CredentialsValue = token;
            try
            {
                return myweb.HelloWorld("hello");
            }
            catch
            {
                return ":(";
            }
        }
    }
}


以上是c#.net调用是没有问题的

-------------------------------------------------------------------------------
接下来是用vb6来调用
程序代码:
Option Explicit
'webservice  地址
Private Const wsurl = "http://127.0.0.1/webtest/WebService.asmx?wsdl"

'btn1_Click调用webservice的方法没有进行加密
Private Sub btn1_Click()
Dim soapclient3 As New SoapClient30
soapclient3.mssoapinit (wsurl)
MsgBox (soapclient3.GetTest("ReturnString"))
End Sub

'btn2_Click调用webservice的HelloWorld方法
Private Sub btn2_Click()
Dim soapclient3 As New SoapClient30
soapclient3.mssoapinit (wsurl)
'试了很多方法,实在不知道怎么样添加HeaderHandler 
'soapclient3.HeaderHandler 
MsgBox (soapclient3.HelloWorld("hello"))
End Sub


有谁知道针对我用.net写的服务端,用vb6来调用HelloWorld的方法
搜索更多相关主题的帖子: webservice 用户 验证 
2008-09-22 10:02



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-234477-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.352010 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved