以下为服务器端的代码(测试地址:"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 System.ComponentModel;
using TestWebService;
namespace Web
{
/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[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的方法
5 个解决方案
#1
没有人顶啊!
#2
就没有人帮忙吗?
#4
嗯这个例子很好,谢谢ZengHD!不过好像可能误解了我的意思,我这边不是vb调用返加实体类的ws接口。我是想对ws过进一些访问安全处理,就是用户名与密码匹配后才'同意'访问。当然这两个参数(用户名与密码)不可能以Helloworld的参数进行传递,所以这样就使用了soapheader。不知道vb对soaheader的处理方法是怎么样的
#5
这个蛮重要的不知道为什么没有人回答
不过已经解决了
http://www.9diamond.com
不过已经解决了
Option Explicit
Private soapclient As SoapClient30
Private Const WSDL_NAMESPACE = "http://tempuri.org/"
'The WSDL_RUL web service URL.
Private Const WSDL_URL = "http://192.168.0.201/webtest/WebService.asmx"
Private Const username = "admin"
Private Const password = "123"
Private Sub Form_Load()
Set soapclient = New SoapClient30
soapclient.MSSoapInit WSDL_URL + "?wsdl"
End Sub
Private Function CreateConnector(ByVal Method As String) As HttpConnector30
Dim Serializer As SoapSerializer30
Dim Connector As ISoapConnector
Set Connector = New HttpConnector30
Connector.Property("EndPointURL") = WSDL_URL
Connector.Connect
Connector.Property("SoapAction") = WSDL_NAMESPACE + Method
Connector.BeginMessage
Set Serializer = New SoapSerializer30
Serializer.Init Connector.InputStream
Serializer.StartEnvelope
Serializer.StartHeader
Serializer.StartElement "Credentials", WSDL_NAMESPACE
Serializer.StartElement "Name", WSDL_NAMESPACE
Serializer.WriteString username
Serializer.EndElement
Serializer.StartElement "Password", WSDL_NAMESPACE
Serializer.WriteString password
Serializer.EndElement
Serializer.EndElement
Serializer.EndHeader
Serializer.StartBody
Serializer.StartElement Method, WSDL_NAMESPACE
Serializer.EndElement
Serializer.EndBody
Serializer.EndEnvelope
Connector.EndMessage
Set CreateConnector = Connector
End Function
Private Sub GetAbout_Click()
Dim Reader As SoapReader30
Set Reader = New SoapReader30
'Create and send a SOAP GetAbout request and receive the result.
Reader.Load CreateConnector("HelloWorld").OutputStream
If Not Reader.Fault Is Nothing Then
'Display the error message received from server.
Text1 = " " + Reader.FaultDetail.xml
Else
'Display the complex type Version
Text1 = Reader.Body.firstChild.firstChild.xml
End If
End Sub
http://www.9diamond.com
#1
没有人顶啊!
#2
就没有人帮忙吗?
#3
#4
嗯这个例子很好,谢谢ZengHD!不过好像可能误解了我的意思,我这边不是vb调用返加实体类的ws接口。我是想对ws过进一些访问安全处理,就是用户名与密码匹配后才'同意'访问。当然这两个参数(用户名与密码)不可能以Helloworld的参数进行传递,所以这样就使用了soapheader。不知道vb对soaheader的处理方法是怎么样的
#5
这个蛮重要的不知道为什么没有人回答
不过已经解决了
http://www.9diamond.com
不过已经解决了
Option Explicit
Private soapclient As SoapClient30
Private Const WSDL_NAMESPACE = "http://tempuri.org/"
'The WSDL_RUL web service URL.
Private Const WSDL_URL = "http://192.168.0.201/webtest/WebService.asmx"
Private Const username = "admin"
Private Const password = "123"
Private Sub Form_Load()
Set soapclient = New SoapClient30
soapclient.MSSoapInit WSDL_URL + "?wsdl"
End Sub
Private Function CreateConnector(ByVal Method As String) As HttpConnector30
Dim Serializer As SoapSerializer30
Dim Connector As ISoapConnector
Set Connector = New HttpConnector30
Connector.Property("EndPointURL") = WSDL_URL
Connector.Connect
Connector.Property("SoapAction") = WSDL_NAMESPACE + Method
Connector.BeginMessage
Set Serializer = New SoapSerializer30
Serializer.Init Connector.InputStream
Serializer.StartEnvelope
Serializer.StartHeader
Serializer.StartElement "Credentials", WSDL_NAMESPACE
Serializer.StartElement "Name", WSDL_NAMESPACE
Serializer.WriteString username
Serializer.EndElement
Serializer.StartElement "Password", WSDL_NAMESPACE
Serializer.WriteString password
Serializer.EndElement
Serializer.EndElement
Serializer.EndHeader
Serializer.StartBody
Serializer.StartElement Method, WSDL_NAMESPACE
Serializer.EndElement
Serializer.EndBody
Serializer.EndEnvelope
Connector.EndMessage
Set CreateConnector = Connector
End Function
Private Sub GetAbout_Click()
Dim Reader As SoapReader30
Set Reader = New SoapReader30
'Create and send a SOAP GetAbout request and receive the result.
Reader.Load CreateConnector("HelloWorld").OutputStream
If Not Reader.Fault Is Nothing Then
'Display the error message received from server.
Text1 = " " + Reader.FaultDetail.xml
Else
'Display the complex type Version
Text1 = Reader.Body.firstChild.firstChild.xml
End If
End Sub
http://www.9diamond.com