【问题标题】:Cannot access web service from VB.NET无法从 VB.NET 访问 Web 服务
【发布时间】:2013-07-11 15:30:38
【问题描述】:

几个月前,我在 VB.Net 中编写了一个访问 Cold Fusion Web 服务的应用程序。 Web 服务(位于云上的 CF 服务器上)在我的 VB.Net 项目中设置为 Web 参考。当我在运行 Windows Server 2003 的系统上执行构建的应用程序时,无论使用此 Web 服务都没有问题。

快进到今天。我正在尝试使用相同的网络/连接在相邻的 Win7 计算机上使用此 Web 服务,并且每次我在 VS2010 IDE 中或作为构建的应用程序运行该应用程序时,它都会给我一个错误:“底层连接已关闭:连接意外关闭。”

我没有对 Web 服务或 VB.Net 代码进行任何更改。

什么会导致这样的错误?

这是我使用该服务的 VB.NET 代码:

 Public Function ConnectedToTheInternet() As Boolean
    Dim myservice = New TestService.testservicecfc()

    Try
        myservice.echoString("hello")
        ConnectedToTheInternet = True
    Catch ex As Exception
        Windows.Forms.MessageBox.Show(ex.Message)
        ConnectedToTheInternet = False
    End Try

End Function

这是我的网络服务代码:

<cfcomponent output="false">
    <cffunction name="echoString" returnType="string" output="no" access="remote">
        <cfargument name="input" type="string">
        <cfreturn #arguments.input#>
    </cffunction>
</cfcomponenet>

我尝试禁用我的 Windows 防火墙,但没有效果。

【问题讨论】:

  • 我没有在每个使用它的方法中声明和实例化一个 Web 服务变量,而是创建了一个全局 Web 服务变量,它在我的应用程序第一次执行时被实例化。这似乎解决了这个问题。任何其他建议仍然欢迎。
  • 好的。我撒了谎。这仍然不起作用。

标签: vb.net web-services coldfusion coldfusion-10


【解决方案1】:

不太确定,不过我会尝试添加一些代码权限,查找基于角色的安全性,并确保您的应用程序在代码中拥有访问服务器的所有权限。

使用 GenericIdentityGenericPricipal 关键字。

这里是一些示例代码

Imports System
Imports System.Security.Principal 
Imports System.Threading

Public Shared Sub Main()
'Hard coded username
Dim strUsername As String = "admin"

'Create GenericIdentity
Dim gidMyID as New GenericIdentity(strUsername)
Dim strMyRole As String = "Some Role"

'Create GenericPrincipal
Dim gplMyPrincipal As New GenericPrincipal(MyIdentity, MyRoles)

'Store the principal for later use
Thread.CurrentPrincipal = gplMyPrincapal 

End Sub

您可以设置它,让它将您视为管理员,然后您需要做的就是告诉您的服务器您定义的角色可以访问网络服务。

最后一段代码是在你已经通过身份验证后调用 web 服务

if(String.Compare(gplMyPrincapal, "DOMAIN\Admin", True) = 0) Then
    'enter code here
Else
    Msgbox("Cannot Connect", "Error")
End If

不确定这是否会有所帮助,但值得一试

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    • 2011-05-23
    • 2014-07-15
    • 2017-02-19
    • 2019-11-07
    相关资源
    最近更新 更多