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的方法