C# + REST API + HttpClient + 请求跟踪器 [已关闭]

问题描述 投票:0回答:1

我正在 C# 上测试 REST API + HttpClient。 https://rt-wiki.bestpractical.com/wiki/REST 但我收到错误。 请问你能帮帮我吗。 我的测试代码有什么问题吗?

我认为问题在于

client.SendAsync(request)

来自控制台的错误代码:

System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[System.Net.Http.HttpResponseMessage,System.Net.Http.HttpClient+d__70]

public class TicketSyncronizer
{
    public int ticketId = 0;
    public class RT_Ticket
    {
        public string SendRequest(int ticketId)
        {
            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://rt.site.com/REST/1.0/");
                //client.TransportSettings.Cookies = new CookieContainer();
                var request = new HttpRequestMessage(HttpMethod.Get, "/ticket/" + ticketId.ToString() + "/show");

                Console.WriteLine("request" + request.ToString());
                var formData = new List <KeyValuePair<string, string>>();
                formData.Add(new KeyValuePair<string, string>("user", "user1"));  
                formData.Add(new KeyValuePair<string, string>("pass", "pass2"));
                request.Content = new FormUrlEncodedContent(formData);
                Console.WriteLine("request + l and p" + request.ToString());
                var response = client.SendAsync(request);
                  return "";
            
            }
        }
    }
static void Main()
{
    //int setticketId = 9134141;
    RT_Ticket checkticket = new RT_Ticket();
    checkticket.SendRequest(9134141);
    Console.WriteLine("Hello World!");
}
}
c# dotnet-httpclient
1个回答
1
投票

我猜你想做的是:

var response = await client.SendAsync(request);

然后将方法签名更改为:

public async Task<string> SendRequest(int ticketId)
© www.soinside.com 2019 - 2024. All rights reserved.