【发布时间】:2020-08-30 07:45:23
【问题描述】:
我正在尝试通过使用 VBA 的 MS Access 数据库应用程序调用和使用 ASP.NET Web 服务。 Web 服务本身似乎是可操作的,因为我能够使用单独的 ASP.NET Web 应用程序成功使用它。但是,我很难尝试在 Access 数据库中获取 VBA 代码以从 Web 服务返回正确的数据字符串。
我已经搜索了许多关于这个主题的不同论坛,但我似乎无法找到我需要的信息才能让它按照我需要的方式工作。也就是说,我对使用 Web 服务很陌生。
这是我在我的 MS Access 数据库中使用的代码:
Private Sub Command0_Click()
InvokeWebService ("http://localhost:51075/WebService1.asmx?HelloWorld")
End Sub
Public Function InvokeWebService(ByVal strUrlCommand As String) As String
Dim HttpReq As Object
Dim strWebCode As String
Dim fOk As Boolean
' Routine that calls the web site
Set HttpReq = CreateObject("MSXML2.XMLHTTP")
HttpReq.Open "GET", strUrlCommand, False
On Error Resume Next
HttpReq.send
fOk = (Err.Number = 0)
If fOk Then
strWebCode = HttpReq.responseText
Else
strWebCode = "Err"
End If
Set HttpReq = Nothing
InvokeWebService = strWebCode
End Function
这是我用于网络服务的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace Webservice
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello to the World";
}
}
}
当我在 Access 数据库中执行代码时,我希望将字符串“Hello to the world”返回给我。但是,这是我运行 VBA 代码时实际返回的内容(返回到 VBA 变量“InvokeWebService”)。我感谢任何关于我做错了什么的建议。
<html>
<head><link rel="alternate" type="text/xml" href="/WebService1.asmx?disco" />
<style type="text/css">
BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; }
#content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; }
A:link { color: #336699; font-weight: bold; text-decoration: underline; }
A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; }
A:active { color: #336699; font-weight: bold; text-decoration: underline; }
A:hover { color: cc3300; font-weight: bold; text-decoration: underline; }
P { color: #000000; margin-top: 0px; margin-bottom: 12px; font-family: Verdana; }
pre { background-color: #e5e5cc; padding: 5px; font-family: Courier New; font-size: x-small; margin-top: -5px; border: 1px #f0f0e0 solid; }
td { color: #000000; font-family: Verdana; font-size: .7em; }
h2 { font-size: 1.5em; font-weight: bold; margin-top: 25px; margin-bottom: 10px; border-top: 1px solid #003366; margin-left: -15px; color: #003366; }
h3 { font-size: 1.1em; color: #000000; margin-left: -15px; margin-top: 10px; margin-bottom: 10px; }
ul { margin-top: 10px; margin-left: 20px; }
ol { margin-top: 10px; margin-left: 20px; }
li { margin-top: 10px; color: #000000; }
font.value { color: darkblue; font: bold; }
font.key { color: darkgreen; font: bold; }
font.error { color: darkred; font: bold; }
.heading1 { color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal; background-color: #003366; margin-top: 0px; margin-bottom: 0px; margin-left: -30px; padding-top: 10px; padding-bottom: 3px; padding-left: 15px; width: 105%; }
.button { background-color: #dcdcdc; font-family: Verdana; font-size: 1em; border-top: #cccccc 1px solid; border-bottom: #666666 1px solid; border-left: #cccccc 1px solid; border-right: #666666 1px solid; }
.frmheader { color: #000000; background: #dcdcdc; font-family: Verdana; font-size: .7em; font-weight: normal; border-bottom: 1px solid #dcdcdc; padding-top: 2px; padding-bottom: 2px; }
.frmtext { font-family: Verdana; font-size: .7em; margin-top: 8px; margin-bottom: 0px; margin-left: 32px; }
.frmInput { font-family: Verdana; font-size: 1em; }
.intro { margin-left: -15px; }
</style>
<title>
WebService1 Web Service
</title></head>
<body>
<div id="content">
<p class="heading1">WebService1</p><br>
<span>
<p class="intro">The following operations are supported. For a formal definition, please review the <a href="WebService1.asmx?WSDL">Service Description</a>. </p>
<ul>
<li>
<a href="WebService1.asmx?op=HelloWorld">HelloWorld</a>
</li>
<p>
</ul>
</span>
<span>
</span>
</body>
</html>
是的,我意识到我可以在我的 Access 数据库中创建和引用一个 .NET DLL 来让它工作。但是,我需要避免对 .NET 的依赖,并且希望避免必须维护和分发额外的 DLL 文件的问题;假设我可以在没有它的情况下完成这项任务。
【问题讨论】:
-
如果您打开浏览器访问该 URL,您会看到什么? “我可以使用单独的 ASP.NET Web 应用程序成功使用它” - 你能分享执行此操作的客户端代码吗?
-
...或试试
http://localhost:51075/WebService1.asmx?op=HelloWorld -
当我在浏览器中加载 URL“localhost:51075/WebService1.asmx”时,会出现 Web 服务描述页面。所以,那部分有效。客户端代码是我在上面发布的第一个代码块(用于 Microsoft Access)。
-
对不起,我的意思是工作的网络浏览器客户端代码(假设它是从浏览器调用的)
-
不是这个网址:localhost:51075/WebService1.asmx/HelloWorld。你有一个 ”?”。当你点击网页时,你应该得到相当干净的 xml 作为返回结果。
标签: .net vba web-services ms-access web