如何从 Acumatica 内部发出外部 HTTP 请求
How to make an external HTTP request from within Acumatica
我想通过“输入销售订单”屏幕上的自定义操作发出 GET 请求 (SO301000)。我们有一个单独的系统用于向客户发送确认电子邮件。客户服务将使用该操作来手动触发电子邮件。
我试过使用 HttpClient class,但它告诉我 "The type or namespace name 'HttpClient' could not be found (are you missing a using directive or an assembly reference?)"。我正在引用 System.Net、System.Net.Http 和 System.Net.Http.Headers 命名空间,所以我想知道 System.Net.Http 程序集是否未被引用Acumatica?
是否有更好的方法来发出外部请求?
不幸的是,Acumatica 没有引用 System.Net.Http 程序集。也就是说,无法在自定义的 C# 代码文件中使用 HttpClient class。
另一种选择是创建一个扩展库,它将引用 System.Net.Http 程序集并将 dll 包含到自定义中而不是 C# 代码文件中。有关扩展库的更多信息,请查看 Acumatica Customization Guide
为了扩展 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;
}
}
}
我想通过“输入销售订单”屏幕上的自定义操作发出 GET 请求 (SO301000)。我们有一个单独的系统用于向客户发送确认电子邮件。客户服务将使用该操作来手动触发电子邮件。
我试过使用 HttpClient class,但它告诉我 "The type or namespace name 'HttpClient' could not be found (are you missing a using directive or an assembly reference?)"。我正在引用 System.Net、System.Net.Http 和 System.Net.Http.Headers 命名空间,所以我想知道 System.Net.Http 程序集是否未被引用Acumatica?
是否有更好的方法来发出外部请求?
不幸的是,Acumatica 没有引用 System.Net.Http 程序集。也就是说,无法在自定义的 C# 代码文件中使用 HttpClient class。
另一种选择是创建一个扩展库,它将引用 System.Net.Http 程序集并将 dll 包含到自定义中而不是 C# 代码文件中。有关扩展库的更多信息,请查看 Acumatica Customization Guide
为了扩展 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;
}
}
}