Inside Activate
Development
Use of HttpClient in Activate
1 min
httpclient has superseded httpwebrequest and should be used in all new projects requiring an http client see attached script for an example of recommended usage dos use provisioningsystem objectcache to reuse a single instance take advantage of the baseaddress property, remember that this is persistent per instance read httpclient guidelines for net net | microsoft learn don'ts create and dispose with each request as each instance contains a connection pool example script //@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 system text json; using innovation activate; using innovation activate utilities; class script scriptbase { 	string baseurl = "https //jsonplaceholder typicode com"; 	string getpostsurl = "posts/1"; 	string updatepostsurl = "posts"; 	public void main() 	{ 	 runmodernexample(); 	 // runlegacyexample(); 	} 	public void runmodernexample() 	{ 	 // create the http client, or get it from the cache 	 httpclient http = provisioningsystem objectcache getorcreate\<httpclient>("my connection", 	 () => 	 { 	 var handler = new httpclienthandler(); 	 //set auth settings here 	 //handler credentials = ; 	 return new httpclient(handler) { baseaddress = new uri( baseurl) }; 	 }); 	 // call our endpoint 	 httpresponsemessage response = asyncutils execsync(() => http getasync( getpostsurl)); 	 if (!response issuccessstatuscode) { return; } // do something if the response is not successful 	 // read the response to a string 	 string jsonstring = asyncutils execsync(() => response content readasstringasync()); 	 trace writeline(jsonstring); 	 // deserialise the string to an object 	 mycustomresponseobject jsonobject = jsonserializer deserialize\<mycustomresponseobject>(jsonstring, new jsonserializeroptions { propertynamecaseinsensitive = true }); 	 trace writeline($"the user id is {jsonobject userid}"); 	 // update the object 	 jsonobject title = $"updated {jsonobject title}"; 	 // sent the updated object to the save enpoint 	 var body = new stringcontent(jsonserializer serialize(jsonobject), system text encoding utf8, "application/json"); 	 httpresponsemessage postresponse = asyncutils execsync(() => http postasync( updatepostsurl, body)); 	 trace writeline($"the updated status code was {postresponse statuscode}"); 	} 	public void runlegacyexample() 	{ 	 httpwebrequest request = webrequest create( baseurl + getpostsurl) 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); 	 } 	 } 	} } public class mycustomresponseobject { 	public int userid { get; set; } 	public int id { get; set; } 	public string title { get; set; } 	public string body { get; set; } }