【问题标题】:Consuming an ASP.NET Webservice from an Access Application using VBA使用 VBA 从 Access 应用程序中使用 ASP.NET Web 服务
【发布时间】: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


【解决方案1】:

如果您不能只输入 URL 并且网络浏览器不会吐出漂亮的 hello world xml?

这是一个配置问题 - 真的很讨厌!

查看这篇文章:

Request format is unrecognized for URL unexpectedly ending in

因此,您需要将其添加到您的网络配置中:

<configuration>
   <system.web>
    <webServices>
     <protocols>
       <add name="HttpGet"/>
       <add name="HttpPost"/>
     </protocols>
   </webServices>
  </system.web>

所以,你可以在这里使用 SOAP 或休息。

这个网址应该可以工作:

我首先要确保在浏览器中输入一个简单的 URL 可以正常工作并吐出那个 xml。

好的,那么您必须输入什么 REST url 才能正常工作?

点击调用按钮。你得到这个:

注意上面的网址。

所以,在 VBA 中,我们可以:

Sub GetWeb()

  Dim strURL     As String
  strURL = "https://localhost:44392/WebService1.asmx/HelloWorld"

  Dim xDOM       As New MSXML2.DOMDocument60

  xDOM.async = False
  xDOM.Load (strURL)

  Debug.Print xDOM.Text


End Sub

上面应该输出Hello World。但是在您测试/尝试/运行 VBA 代码之前?

确保您可以在网络上输入相同的 URL,它应该会输出上述 XML。 (注意:由于某种原因,如果没有上述 web.config 设置,通过 .net 工作的“测试”,但只是输入 URL 不起作用 - 所以你需要上面的 web 配置。

【讨论】:

  • 非常感谢阿尔伯特!那钉了它。为了其他人的利益,我会详细说明。我使用 Albert 提供的 MS Access 调用示例让它工作。我还在我的 web.config 文件中添加了 HttpGet 和 HttpPost 标签(它们丢失了)。
  • 按照 Albert 的指示,我测试了 URL:localhost:51075/WebService1.asmx?op=HelloWorld ..以及它应该的数据(总是有效,但我之前可能不清楚回答后续问题)。
  • 注意:如果您在测试时从 VB.NET 运行 Web 服务,您有 3 个不同版本的 web.config,您也需要注意。我认为它们是这样工作的(但我仍在学习 .NET Web 的东西): A. web.config:这是从 IIS 运行 Web 服务时使用的。 B. web.debug.config:这是在调试模式下运行 .net 项目时使用的配置。 C. web.release.config:这是在发布模式下运行 .net 项目时使用的配置。
  • 以后读到这里的人可能想用参数来做这件事。我会使用上面的代码示例并让它工作到这一步。然后,检查此 URL 以了解如何传递参数:stackoverflow.com/questions/11189451/… 我很想看到一个关于如何通过引用传递变量的示例。如果有人有关于如何从 Access 执行此操作的链接或示例,那就太棒了。还不确定是否可以完成。再次感谢大家的帮助。
  • 另外,如果你的 web 服务像我一样是用 C# 编写的,你需要记住,来自你的消费者应用程序的方法调用是区分大小写的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-01
  • 2016-03-28
  • 2013-08-28
  • 1970-01-01
  • 2014-02-24
  • 1970-01-01
相关资源
最近更新 更多