0

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}

Mark Rotteveel
  • 90,369
  • 161
  • 124
  • 175
Fox
  • 1,208
  • 2
  • 8
  • 19
  • 1
    What status code does it return? – Peter Csala Feb 09 '21 at 12:03
  • Hello Peter {"data":null} – Fox Feb 09 '21 at 12:08
  • 1
    What is the value of the following: `((HttpWebResponse)wr).StatusCode`? – Peter Csala Feb 09 '21 at 12:10
  • Took from example https://stackoverflow.com/questions/9825846/curl-call-in-c-sharp – Fox Feb 09 '21 at 12:12
  • 1
    Yes, I understand but could you please extract the status code from the response object. Unless this information we can't go futher. – Peter Csala Feb 09 '21 at 12:18
  • StatusCode = OK – Fox Feb 09 '21 at 12:25
  • please try to change the encoding from ascii to `Encoding.GetEncoding("ISO-8859-1")` [Related SO](https://stackoverflow.com/a/13956730/13268855) – Peter Csala Feb 09 '21 at 12:31
  • It is also strange for me why do set the `Authorization` header and the `Credentials` as well. One of them should be enough. – Peter Csala Feb 09 '21 at 12:34
  • Did any of the suggested modifications work for you? – Peter Csala Feb 10 '21 at 07:45
  • Unfortunately, so far everything is without result, I created a proxmox event on the forum, as soon as there is a solution I will definitely publish. – Fox Feb 10 '21 at 09:49
  • 1
    Have you tried to [import the curl command](https://www.shipengine.com/docs/curl/) into postman. If it works as well then you can ask postman to [generate a C# client](https://learning.postman.com/docs/sending-requests/generate-code-snippets/). It will use RestSharp instead of `WebRequest` or `HttpClient` – Peter Csala Feb 10 '21 at 10:29

1 Answers1

0

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();
        }
Fox
  • 1,208
  • 2
  • 8
  • 19
  • could you please include the links what I have mentioned in the comments? Then we can delete most of the comments for your question to keep the topic clean. – Peter Csala Feb 10 '21 at 12:00