Curl C# Call using HttpClient

1k Views Asked by At

The Problem

I need to make a cUrl call using C# to the Betfair API. The call is to try gain login access using currently registered details. The method im trying to use is described in the API documentation as an Interactive Login using the API Endpoint. I have no experience using cUrl so very unsure how to make it work.

API Documentation page for the call i am trying to make.

https://api.developer.betfair.com/services/webapps/docs/display/1smk3cen4v3lu3yomq5qye0ni/Interactive+Login+-+API+Endpoint

Required Call

curl -k -i -H "Accept: application/json" -H "X-Application: <AppKey>" -X POST -d 'username=<username>&password=<password>' https://identitysso.betfair.com/api/login

Progress

In the past i have used HttpRequest in the following way.

HttpWebRequest request =(HttpWebRequest)WebRequest.Create("http://api.football-data.org/v1/soccerseasons/354/fixtures/?matchday=22");
request.Headers.Add("AUTHORIZATION", "Basic YTph");
request.ContentType = "text/html";
request.Method = "GET"; 
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader stream = new StreamReader(response.GetResponseStream());
string ResultAsText = stream.ReadToEnd().ToString();

From what i have read i should using HttpClient when making cUrl calls but as i have never done this before im finding it difficult to work it out.

the following code is what i have tried so far but it does not seem to work.

using System.Net.Http;

var client = new HttpClient();

// Create the HttpContent for the form to be posted.
var requestContent = new FormUrlEncodedContent(new [] {
new KeyValuePair<string, string>("text", "username=<username>&password=<password>"),
});

// Get the response.
HttpResponseMessage response = await client.PostAsync(
"https://identitysso.betfair.com/api/login",
requestContent);

// Get the response content.
HttpContent responseContent = response.Content;

// Get the stream of the content.
using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
{
// Write the output.
Console.WriteLine(await reader.ReadToEndAsync());
}

Any help would be greatly appreciated. Thanks in advance.

0

There are 0 best solutions below