在 C++ 中自动生成存根代码访问网络服务?
我正在用我的手臂来回答这个问题。 我正在寻找一种工具,它可以通过自动生成访问网络服务的大部分代码来避免进行大量编码。
我正在尝试帮助那些使用不支持网络服务的 4GL 系统的人。然而,他们可以访问win32 dll - 这看起来是最简单的解决方案。他们所需要做的就是偶尔调用 Web 服务上的函数并返回结果。
自从我编写任何 C++ 以来已经有很长一段时间了,而我这样做的尝试暴露了我是多么生疏。
我使用过 gsoap2 工具包和 MS 的 svcutil.exe 工具来自动生成代码。
它们做了它们应该做的事情,但是与 Visual Studio 中使用 vb.net 或 c#.net 的添加引用工具不同,这些工具包不会生成我设法找到的存根访问类。 相反,它们为每个方法生成单独的函数调用,并且您必须向它们传递 httpcontexts 和一大堆其他东西 - 我真的不想一次性学习如何做。
我想做的是机械的:
- 采用 wsdl 定义
- 自动生成 Web 服务访问代码(完成 - gsoap2)
- 编写/生成一个小存根以打开 Web 服务并使用基本身份验证进行身份验证并返回 Web 服务实例类的实例。
- 发布为 dll
这个想法是拥有一个具有单个函数的 dll,例如 getws(用户名、密码、网址) 它将返回一个公开 Web 服务方法的对象 - 一个存根,没什么聪明的。
我知道我在这里抓住了救命稻草,但有人知道有一种工具/方法可以避免所有机械工作并最终得到一个简单的类,我可以对其进行修改以添加身份验证。
该 Web 服务有大约 30 个方法 - 我必须将它们全部公开,每个方法都有一个参数集合。编写一个存根类来调用 gsoap2 生成的函数将需要大量的输入,并且对于开始工作/调试来说是一场噩梦。一定有更好的方法。
我想做的是相当于下面的 .net 代码 - VS 自动生成 WS 代码。我所要做的就是在课堂上揭露它。
Private Shared oWs As WS.publicws = Nothing
Public Shared Function GetWS(ByVal Username As String, ByVal password As String, ByVal URL As String) As WS.publicws
Dim oBinding As New ServiceModel.BasicHttpBinding
If Not oWs Is Nothing Then Return oWs
Dim oEndPoint As New ServiceModel.EndpointAddress(URL)
oBinding.Security.Mode = ServiceModel.BasicHttpSecurityMode.TransportCredentialOnly
oBinding.Security.Transport.Realm = ServiceModel.HttpClientCredentialType.Basic
oWS = New WS.publicws (oBinding, oEndPoint)
oWS.ClientCredentials.UserName.UserName = username
oWS.ClientCredentials.UserName.Password = password
Using scope = New ServiceModel.OperationContextScope(oWs.InnerChannel)
ServiceModel.OperationContext.Current.OutgoingMessageProperties(System.ServiceModel.Channels.HttpRequestMessageProperty.Name) = httpRequestProperty
End Using
Return oWs
End Function
I'm chancing my arm with this question.
I'm looking for a tool which will avoid doing a lot of coding by autogenerating much of the code to access a webservice.
I'm trying to help out someone who uses a 4GL system which doesn't support webservices. However they can access a win32 dll - which looks like being the easiest solution. All they need to do is occasionally call a function on a web service and get a result back.
Its been a loooong time since I wrote any C++ and my attempts at doing this have just exposed how rusty I am.
I've played around with the gsoap2 toolkit and MS's svcutil.exe tool for auto generating code.
They do what they are supposed to do but, unlike the add reference tool in visual studio with vb.net or c#.net, these toolkits don't generate a stub access class that I managed to find.
Instead they generate individual function calls for each method and you have to pass them httpcontexts and a whole load of other stuff - something I don't really want to have to learn how to do for a one off.
What I want to do is mechanical:
- Take the wsdl definition
- AutoGenerate the Webservice access code (done - gsoap2)
- Write/generate a small stub to open the webservice and authenticate using basic authentication and to return an instance of the webservice instance class.
- publish as a dll
The idea being to have a single dll with a single function like
getws(username, password, url)
which will return an object which exposes the methods of the webservices - a stub, nothing clever.
I know I'm clutching at straws here but does anyone know of a tool/way to avoid all the mechanical work and to end up with a simple class which I can modify to add authentication.
The webservice has around 30 methods - and I have to expose them all, each has a collection of parameters. Writing a stub class to call the functions generated by gsoap2 would be a lot of typing and a nightmare to get to work/debug. Theres got to be a better way.
What I want to do is the equivalent of the .net code below - VS autogenerates the WS code. All I have to do is expose it in my class.
Private Shared oWs As WS.publicws = Nothing
Public Shared Function GetWS(ByVal Username As String, ByVal password As String, ByVal URL As String) As WS.publicws
Dim oBinding As New ServiceModel.BasicHttpBinding
If Not oWs Is Nothing Then Return oWs
Dim oEndPoint As New ServiceModel.EndpointAddress(URL)
oBinding.Security.Mode = ServiceModel.BasicHttpSecurityMode.TransportCredentialOnly
oBinding.Security.Transport.Realm = ServiceModel.HttpClientCredentialType.Basic
oWS = New WS.publicws (oBinding, oEndPoint)
oWS.ClientCredentials.UserName.UserName = username
oWS.ClientCredentials.UserName.Password = password
Using scope = New ServiceModel.OperationContextScope(oWs.InnerChannel)
ServiceModel.OperationContext.Current.OutgoingMessageProperties(System.ServiceModel.Channels.HttpRequestMessageProperty.Name) = httpRequestProperty
End Using
Return oWs
End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论