HttpresponseMessage risposta = await client.PostAsync(new Ur



我试图连接到检查点api与一些c#代码,我键入所有的数据(用户名和密码json格式和编译Uri),当我做调用它只是结束执行没有异常

下面是代码

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字符串管理是相当糟糕的,但当我测试的东西,它帮助我。

代码到此结束risposta = await client.PostAsync(new Uri(uri), queryString);

对原因有什么建议吗?

建议使用Newtonsoft。Json来序列化你的字符串数据到Json正确,它是100%更好。关于过早结束,我认为调用' capicall '的方法不是以异步方式进行的,所以检查这个方法是否异步创建,并在同一行' capicall '之前有等待操作符。

最新更新