Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys
I have problome to convert curl request to c # request; i use RestSharp and use GET request easily but for authorization have problome.
How do i make a curl request in c# in windows or

i want to make web request with this parameters and it should receive a valid response

Request:

curl -u "publicKey:secretKey" https://MyWebsite.com/api/balance


How to do similarly in c#???

What I have tried:

C#
public async Task<dynamic> Execute(RestRequest request, bool requireAuthentication = true)
        {
            if (requireAuthentication && !IsAuthorized)
                throw new Exception("AccessTokenInvalid");

            var client = new RestClient(Url);

            if (requireAuthentication)
            {
                //request.AddParameter("apikey", _apiKey);
                //request.AddParameter("secretKey", _secretKey);
                request.Credentials = new NetworkCredential(_apiKey, _secretKey);
            }

            var response = await client.GetResponseAsync(request);

            if (response.ErrorException != null)
            {
                const string message = "Error retrieving response.  Check inner details for more info.";
                var exception = new ApplicationException(message, response.ErrorException);
                throw exception;
            }

            return new ApiResponse { Content = response.Content };
        }
Posted
Updated 7-Apr-18 20:10pm

1 solution

Here is a piece of a PowerShell script, which is similar to C#, that calls a REST API.
I only got it working after adding an empty domain:
$username = "User"
$password = "Password"
$domain = ""

$wc = New-Object System.Net.WebClient
$wc.Credentials = new-object System.Net.NetworkCredential($username, $password, $domain)
$wc.DownloadFile($downloadUrl, $downloadFile)
The full script you can find in: Fiddling around with the TeamCity REST API[^]

About Async: beware that immediately after the await your method will return to the calling method and that the request will not be finished yet, you might have to do some waiting there ...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900