C
C#2y ago
Capple

✅ Moving from HttpWebRequest to HttpClient

Hello, I'm in need of help refactoring from HttpWebRequest(obsolete) to HttpClient. I've tried to do it for hours but can't get it to work. Could someone please help me out? Here's my currently obsolete code
var request = (HttpWebRequest)WebRequest.Create("https://127.0.0.1:" + port + url);

request.PreAuthenticate = true;
request.ContentType = "application/json";
request.Method = type; request.Headers.Add("Authorization", "Basic " + token);

var httpResponse =(HttpWebResponse)request.GetResponse();

using (var
streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
return streamReader.ReadToEnd();
}
var request = (HttpWebRequest)WebRequest.Create("https://127.0.0.1:" + port + url);

request.PreAuthenticate = true;
request.ContentType = "application/json";
request.Method = type; request.Headers.Add("Authorization", "Basic " + token);

var httpResponse =(HttpWebResponse)request.GetResponse();

using (var
streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
return streamReader.ReadToEnd();
}
4 Replies
RDasher
RDasher2y ago
Two things to note: >HttpClient methods are asynchronous >You need to define a client first
async Task MakeTestGetRequest(){
//Create Client, this can be reused if you want it too
HttpClient client = new HttpClient();

//Create the Request
using (var requestMessage = HttpRequestMessage(HttpMethod.Get, "https://your.site.com"))
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", your_token);
await httpClient.GetAsync(requestMessage);
}
}
async Task MakeTestGetRequest(){
//Create Client, this can be reused if you want it too
HttpClient client = new HttpClient();

//Create the Request
using (var requestMessage = HttpRequestMessage(HttpMethod.Get, "https://your.site.com"))
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", your_token);
await httpClient.GetAsync(requestMessage);
}
}
MSDN: https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=net-7.0
HttpClient Class (System.Net.Http)
Provides a class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI.
RDasher
RDasher2y ago
Also, since you seem to have a generic method for requests, I think you might need multiple methods for different Requests (GET, POST, PATCH, etc) I don't think there is a way to generalize it
RDasher
RDasher2y ago
You can also see this stackoverflow answer, as a good example of an HttpClient request: https://stackoverflow.com/questions/10679214/how-do-you-set-the-content-type-header-for-an-httpclient-request
Stack Overflow
How do you set the Content-Type header for an HttpClient request?
I'm trying to set the Content-Type header of an HttpClient object as required by an API I am calling. I tried setting the Content-Type like below: using (var httpClient = new HttpClient()) {
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server