【问题标题】:How to make an external HTTP request from within Acumatica如何从 Acumatica 内部发出外部 HTTP 请求
【发布时间】:2019-11-05 06:15:05
【问题描述】:

我想通过“输入销售订单”屏幕 (SO301000) 上的自定义操作发出 GET 请求。我们有一个单独的系统用于向客户发送确认电子邮件。客服将使用该操作手动触发电子邮件。

我尝试使用 HttpClient 类,但它告诉我“找不到类型或命名空间名称 'HttpClient'(您是否缺少 using 指令或程序集引用?)”。我引用了 System.Net、System.Net.Http 和 System.Net.Http.Headers 命名空间,所以我想知道 Acumatica 是否没有引用 System.Net.Http 程序集?

有没有更好的方法来发出外部请求?

【问题讨论】:

    标签: acumatica


    【解决方案1】:

    很遗憾,Acumatica 没有引用 System.Net.Http 程序集。也就是说,无法在自定义的 C# 代码文件中使用 HttpClient 类。

    另一种选择是创建一个扩展库,它将引用 System.Net.Http 程序集并将 dll 包含到自定义中,而不是 C# 代码文件。有关扩展库的更多信息,请查看Acumatica Customization Guide

    【讨论】:

      【解决方案2】:

      为了扩展 RuslanDev 的建议,这里是该扩展库的代码:

      using System;
      using System.Collections.Generic;
      using System.IO;
      using System.Net;
      
      namespace MyApp
      {
          public static class Utility
          {
              private static WebRequest CreateRequest(string url, Dictionary headers)
              {
                  if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
                  {
                      WebRequest req = WebRequest.Create(url);
                      if (headers != null)
                      {
                          foreach (var header in headers)
                          {
                              if (!WebHeaderCollection.IsRestricted(header.Key))
                              {
                                  req.Headers.Add(header.Key, header.Value);
                              }
                          }
                      }
                      return req;
                  }
                  else
                  {
                      throw(new ArgumentException("Invalid URL provided.", "url"));
                  }
              }
              public static string MakeRequest(string url, Dictionary headers = null)
              {
                  WebResponse resp = CreateRequest(url, headers).GetResponse();
                  StreamReader reader = new StreamReader(resp.GetResponseStream());
                  string response = reader.ReadToEnd();
                  reader.Close();
                  resp.Close();
                  return response;
              }
              public static byte[] MakeRequestInBytes(string url, Dictionary headers = null)
              {
                  byte[] rb = null;
                  WebResponse resp = CreateRequest(url, headers).GetResponse();
                  using (BinaryReader br = new BinaryReader(resp.GetResponseStream()))
                  {
                      rb = br.ReadBytes((int)resp.ContentLength);
                      br.Close();
                  }
                  resp.Close();
                  return rb;
              }
          }
      }

      【讨论】:

        猜你喜欢
        • 2014-04-16
        • 1970-01-01
        • 1970-01-01
        • 2011-12-19
        • 1970-01-01
        • 1970-01-01
        • 2019-07-07
        • 1970-01-01
        • 2023-03-24
        相关资源
        最近更新 更多