Can't match the C# code to the Unix command CURL is correct, responce is OK, but C# is {"data":null}
:
curl -k -d "username=test@pve&password=User11" https://ip:8006/api2/json/access/ticket
C#
string url = "https://ip:8006/api2/json/access/ticket";
WebRequest myReq = WebRequest.Create(url);
string username = "test@pve";
string password = "User11";
string usernamePassword = username + ":" + password;
CredentialCache mycache = new CredentialCache();
mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
myReq.Credentials = mycache;
myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
WebResponse wr = myReq.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();
Console.WriteLine(content);
Console.ReadLine();
Response:
{"data":null}
Thanks Peter Csala , helped a lot, I used RestSharp
static void Main(string[] args)
{
ServicePointManager.ServerCertificateValidationCallback = (senderX, certificate, chain, sslPolicyErrors) => { return true; };
Run();
}
public static void Run() {
var client = new RestClient("https://ip:8006/api2/json/access/ticket");
var request = new RestRequest(Method.POST);
request.AddParameter("username", "test@pve");
request.AddParameter("password", "password");
IRestResponse response = client.Execute(request);
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Include
};
dynamic stuff = JsonConvert.DeserializeObject(response.Content, settings);
var array = stuff.data;
string ticket = array.ticket.ToString();
string CSRFPreventionToken = array.CSRFPreventionToken.ToString();
//Call Nodes, VM,
var clients = new RestClient("https://ip:8006/api2/json/nodes/pve01/qemu");
client.Timeout = 10000;
var requests = new RestRequest(Method.GET);
requests.AddCookie("PVEAuthCookie", ticket);
IRestResponse responses = clients.Execute(requests);
var setting = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
};
dynamic stuffs = JsonConvert.DeserializeObject(responses.Content, setting);
Console.WriteLine(stuffs);
Console.ReadLine();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.