How to convert HttpWebRequest to use HttpClient
1 min
HttpWebRequest is deprecated, so if opportunity arises this should be refactored using HttpClient instead. The solution below shows an example of how to do this.
Sample HTTP Client script in Activate
//@import System.Web;
//@import System.Linq.Expressions
//@import System.Private.Uri
//@import System.Net.Requests
//@import System.Net.WebHeaderCollection
//@import System.Collections.Specialized
//@import System.Text.Json
//@import System.Net.Http
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using Innovation.Activate;
using Innovation.Activate.Utilities;
class Script : ScriptBase
{
string _url = "https://jsonplaceholder.typicode.com/todos/1";
public void main()
{
RunModernExample();
// RunLegacyExample();
}
public HttpClient GetHttpClient()
{
HttpClient http = ProvisioningSystem.ObjectCache.GetOrCreate<HttpClient>("CONNECTOR_MYCONNECTION", //The HttpClient should be created once and reused for the same connection.
() =>
{
var handler = new HttpClientHandler();
//Set Auth settings here
//handler.Credentials = ;
return new HttpClient(handler);
});
return http;
}
public void RunModernExample()
{
HttpClient http = GetHttpClient();
string payload = "";
HttpRequestMessage m = new HttpRequestMessage(HttpMethod.Get, _url);
if (m.Method == HttpMethod.Post)
m.Content = new StringContent(payload.ToString(), System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage r = http.Send(m);
//Read the response
using (Stream stream = r.Content.ReadAsStream())
{
//Read the stream in the response
using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8))
{
//Save all of the response to a string
string result = reader.ReadToEnd();
Trace.WriteLine(result);
}
}
}
public void RunLegacyExample()
{
HttpWebRequest request = WebRequest.Create(_url) as HttpWebRequest;
request.Method = "GET";
//Send the request
WebResponse ws = request.GetResponse();
//Read the response
using (Stream stream = ws.GetResponseStream())
{
//Read the stream in the response
using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8))
{
//Save all of the response to a string
string result = reader.ReadToEnd();
Trace.WriteLine(result);
}
}
}
}//@import System.Web;
//@import System.Linq.Expressions
//@import System.Private.Uri
//@import System.Net.Requests
//@import System.Net.WebHeaderCollection
//@import System.Collections.Specialized
//@import System.Text.Json
//@import System.Net.Http
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using Innovation.Activate;
using Innovation.Activate.Utilities;
class Script : ScriptBase
{
string _url = "https://jsonplaceholder.typicode.com/todos/1";
public void main()
{
RunModernExample();
// RunLegacyExample();
}
public HttpClient GetHttpClient()
{
HttpClient http = ProvisioningSystem.ObjectCache.GetOrCreate<HttpClient>("CONNECTOR_MYCONNECTION", //The HttpClient should be created once and reused for the same connection.
() =>
{
var handler = new HttpClientHandler();
//Set Auth settings here
//handler.Credentials = ;
return new HttpClient(handler);
});
return http;
}
public void RunModernExample()
{
HttpClient http = GetHttpClient();
string payload = "";
HttpRequestMessage m = new HttpRequestMessage(HttpMethod.Get, _url);
if (m.Method == HttpMethod.Post)
m.Content = new StringContent(payload.ToString(), System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage r = http.Send(m);
//Read the response
using (Stream stream = r.Content.ReadAsStream())
{
//Read the stream in the response
using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8))
{
//Save all of the response to a string
string result = reader.ReadToEnd();
Trace.WriteLine(result);
}
}
}
public void RunLegacyExample()
{
HttpWebRequest request = WebRequest.Create(_url) as HttpWebRequest;
request.Method = "GET";
//Send the request
WebResponse ws = request.GetResponse();
//Read the response
using (Stream stream = ws.GetResponseStream())
{
//Read the stream in the response
using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8))
{
//Save all of the response to a string
string result = reader.ReadToEnd();
Trace.WriteLine(result);
}
}
}
}