1
votes

I want to make a GET request from a custom Action on the Enter Sales Order screen (SO301000). We have a separate system we use for sending confirmation emails to customers. The Action will be used by customer service to manually trigger an email.

I've tried using the HttpClient class, but it tells me "The type or namespace name 'HttpClient' could not be found (are you missing a using directive or an assembly reference?)". I am referencing the System.Net, System.Net.Http, and System.Net.Http.Headers namespaces, so I'm wondering if the System.Net.Http assembly isn't referenced by Acumatica?

Is there a better way to make an external request?

2

2 Answers

1
votes

Unfortunately, the System.Net.Http assembly isn't referenced by Acumatica. That said, it won't be possible to utilize the HttpClient class in a C# code file placed into customization.

An alternative option though is to create an extension library, that will reference the System.Net.Http assembly and include the dll into customization instead of a C# code file. For more information about extension libraries, check Acumatica Customization Guide

0
votes

To expand on what RuslanDev suggests, here is code for that extension library:

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;
        }
    }
}