Reputation: 411
I am trying to make a (C# .net 4.5 winforms) application that need to get Json from an API. I have been trying and googling for 2 days now with no luck!
Note: I do not want to use Async! Note: It does not have to be WebClient.
All i have to go on is this documented cURL request:
curl -k -d "username=<USERNAME>@<AUTHTYPE>&password=<PASS>" https://<URL>:<PORT>/api/json/access/ticket
{ "data": {
"CSRFPreventionToken":"4EEC61E2:lwk7od06fa1+DcPUwBTXCcndyAY",
"ticket":"User:4EEC61E2::rsKoApxDTLYPn6H3NNT6iP2mv...",
"username":"<USERNAME>"}
}
I am trying to use WebClient to get the Json response, but i keep getting:
Exception: "The remote server returned an error: (401) Unauthorized"
Here is my initial code:
string result = "";
var conn = "https://" + server.Hostname + ":" + server.Port + "/api2/json";
var query = conn + "/access/ticket";
var json = "";
try
{
// Accept Self-Signed SSL Certificate
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
// Get Json
using (WebClient client = new WebClient())
{
json = client.DownloadString(query);
result = json;
}
}
catch (Exception e)
{
var BrPoint = e.ToString();
}
return result;
Now i need to send Authentication with that request (like in the curl one) but how??
And when i have the Ticket, i need to make a cookie containing the ticket to send the next request.
Any help here would be greatly appreciated!
Upvotes: 1
Views: 1276
Reputation: 168
It looks like its because you're doing a Get request with the DownloadString() method call rather than a POST request using the UploadString() method call.
Try changing it to UploadString() and passing the following as the data parameter:
username=<USERNAME>@<AUTHTYPE>&password=<PASS>
Upvotes: 2