1

需要开发 C# 应用程序来创建新的 RT 票证,但https://rt-wiki.bestpractical.com/wiki/REST#C.23上的 c# 示例代码不起作用。

我从不同的地方找到了信息,但没有一个真正有关于如何使用 C# HttpClient 创建新的 rt 票证的示例。

这是我的尝试:

private static async void HTTP_Create()
{
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri(baseURL);

        string ticketUri = string.Format("REST/1.0/ticket/new?user={0}&pass={1}", testUser.UserName, testUser.Password);

        // Doesn't work
        var TicketDataContent = new List<KeyValuePair<string, string>>();
        TicketDataContent.Add(new KeyValuePair<string, string>("id", "ticket/new"));
        TicketDataContent.Add(new KeyValuePair<string, string>("Queue", "queue name"));
        TicketDataContent.Add(new KeyValuePair<string, string>("Requestor", "name@email.com"));
        TicketDataContent.Add(new KeyValuePair<string, string>("Owner", "owner@email.com)"));
        TicketDataContent.Add(new KeyValuePair<string, string>("Subject", "Created by C#"));
        TicketDataContent.Add(new KeyValuePair<string, string>("Text", "Ticket body with messages related to this ticket"));

        var ticketContent = new MultipartFormDataContent();
        ticketContent.Add(new FormUrlEncodedContent(TicketDataContent), "content");
        var response = await client.PostAsync(baseURL + ticketUri, ticketContent);

        Console.WriteLine("Response StatusCode: " + response.StatusCode.ToString());

        var retContent = response.Content;
        var retMsg = await response.Content.ReadAsStringAsync();

        Console.WriteLine(retMsg);
}

我得到以下回复:

响应状态码:OK

RT/4.4.2 200 好

# 必需:id,队列

任何帮助表示赞赏。谢谢!

4

0 回答 0