![](/img/trans.png)
[英]How to connect to a Azure File Store from a .NET Core 3.1 console app in VS2019
[英]Trying to call simple POST API from console App in VS2019
我正在尝试调用简单的POST API
从控制台应用程序创建Zendesk
票证。 我在VS2019
中创建了 C# 核心控制台应用程序并粘贴了应该创建新票证的简单代码。 代码在其他应用程序中工作,但在控制台应用程序中,应用程序只是从调试中注销...
电话永远不会...
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://testhelp.zendesk.com/api/v2/tickets.json"))
{
try
{
var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("test@test.com:testPassword"));
request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");
request.Content = new StringContent("{\"ticket\": {\"subject\": \"My first Ticket!\", \"comment\": { \"body\": \"The ticket is from API.\" }}}");
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpResponseMessage response = await httpClient.SendAsync(request).ConfigureAwait(false);
string content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
Console.WriteLine(response);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
我错过了什么?
在某些情况下,异步调用是冷淡的情妇。
一个不错的选择是使您的主程序异步,从而能够等待任务。 这里的问题与 GUI 有关,如果这是您离开命令行时实际拥有的东西,我不确定它与 UI 线程的配合情况如何,但这是示例。 哪个更优雅。
static async Task Main(string[] args)
{
var TicketTask = await createTicket();
}
async static Task<string> createTicket()
{
var content = "unknown error";
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://testhelp.zendesk.com/api/v2/tickets.json"))
{
try
{
var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("test@test.com:testPassword"));
request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");
request.Content = new StringContent("{\"ticket\": {\"subject\": \"My first Ticket!\", \"comment\": { \"body\": \"The ticket is from API.\" }}}");
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpResponseMessage response = await httpClient.SendAsync(request).ConfigureAwait(false);
content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
//Console.WriteLine(response);
}
catch (Exception ex)
{
content = ex.Message;
}
}
}
return content;
}
快速简单的解决方案(我不建议用于生产)是将主循环中的内容作为全局变量进行监视,在成功或失败时将其填充。
static string content = "";
static void Main(string[] args)
{
var loopcount = 0;
var t = new Task(createTicket);
t.Start();
while(content == "" && loopcount < 50000)
{
System.Threading.Thread.Sleep(100);
loopcount++;
}
}
async static void createTicket()
{
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://testhelp.zendesk.com/api/v2/tickets.json"))
{
try
{
var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("test@test.com:testPassword"));
request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");
request.Content = new StringContent("{\"ticket\": {\"subject\": \"My first Ticket!\", \"comment\": { \"body\": \"The ticket is from API.\" }}}");
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpResponseMessage response = await httpClient.SendAsync(request).ConfigureAwait(false);
content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
//Console.WriteLine(response);
}
catch (Exception ex)
{
content = ex.mesage;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.