Inside Activate
Development
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 //@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); } } } }