I'm trying to connect to CheckPoint APIs with some c# code, i type in all the data(username and password in json format and compile the Uri) and when i do the call it just ends the execution without exceptions
Here's the code
public async Task<HttpResponseMessage> CPAPICall()
{
HttpClient client = new HttpClient();
Console.Write("username: ");
String uname = Console.ReadLine();
Console.Write("password: ");
String pwd = Console.ReadLine();
String json = "{\n" +
"\"user\":" + "\"" + uname + "\",\n" +
"\"password\"" + "\"" + pwd + "\",\n" +
"}";
StringContent queryString = new StringContent(json, Encoding.UTF8, "application/json");
Console.WriteLine("Inserisci l'IP e la porta della management CP a cui vuoi connetterti (Esempio 192.168.1.1:443, se non si inserisce la porta quella di default è 443):");
String IpAddr = Console.ReadLine();
String[] IpAddrEPort = new String[2];
HttpResponseMessage risposta = new HttpResponseMessage();
if (IpAddr.Contains(":"))
{
IpAddrEPort = IpAddr.Split(':');
}
else
{
IpAddrEPort[0] = IpAddr;
IpAddrEPort[1] = "443";
}
String uri = "https://" + IpAddr + "/web_api/login";
try
{
risposta = await client.PostAsync(new Uri(uri), queryString);
}
catch (HttpListenerException e)
{
Console.WriteLine(e);
Console.ReadKey();
}
String risp = risposta.StatusCode.ToString();
Console.WriteLine(risp);
Console.ReadKey();
return risposta;
}
json string management is pretty bad, but while i'm testing things it helps me.
code ends here risposta = await client.PostAsync(new Uri(uri), queryString);
Any suggestion on the cause?
I would suggest using Newtonsoft.Json to serialize your strings data into a JSON correctly, it's 100% better. About the premature end, I think the method that is calling the 'CPAPICall' is not doing it in an asynchronous way, so check if this method was created asynchronously and have the await operator before the 'CPAPICall' on the same line.