0
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)

3
  • 1
    This could be a good starting point HttpClient Commented Feb 27, 2019 at 16:30
  • I this this is what you want: stackoverflow.com/a/37751250/2057653 Commented Feb 27, 2019 at 16:31
  • I tried it but I could not get how to do auth=('email','password') this part. Commented Feb 27, 2019 at 16:36

2 Answers 2

0

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);
});
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.