import requests
requests.post('https://dathost.net/api/0.1/game-servers/54f55784ced9b10646653aa9/start',
auth=('[email protected]', 'secretPassword'))
How would one write this in C#? (NET Core)
import requests
requests.post('https://dathost.net/api/0.1/game-servers/54f55784ced9b10646653aa9/start',
auth=('[email protected]', 'secretPassword'))
How would one write this in C#? (NET Core)
Apart of the added comments, I would recommend using a library called RestSharp. You can easily find the nuget package and the code will be as easier as:
var client = new RestClient("https://dathost.net");
client.Authenticator = new HttpBasicAuthenticator("[email protected]", "secretPassword");
var request = new RestRequest("api/0.1/game-servers/{id}/start", Method.POST);
request.AddUrlSegment("id", "54f55784ced9b10646653aa9");
// execute the request
IRestResponse response = client.Execute(request);
var content = response.Content;
You can also do async requests:
client.ExecuteAsync(request, response => {
Console.WriteLine(response.Content);
});
You can do it with HttpClient.
Add usings top of your code first.
using System.Net.Http;
using System.Net.Http.Headers;
And run this code wherever you want.
var client = new HttpClient();
var byteArray = Encoding.ASCII.GetBytes("[email protected]:secretPassword");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var response = await client.PostAsync(
"https://dathost.net/api/0.1/game-servers/54f55784ced9b10646653aa9/start", null);
Hope this helps.