C# HttpClient抛出意外ThrowForNonSuccess

C# HttpClient抛出意外ThrowForNonSuccess,c#,asp.net,async-await,botframework,direct-line-botframework,C#,Asp.net,Async Await,Botframework,Direct Line Botframework,具有以下与目标框架一起使用的代码.net core 2.2 目标框架4.5存在问题 public async Task<string> GetConversationIdAsync() { try { var client = new HttpClient(); var content = new StringContent(" ", Encoding.UTF8, "application/jso

具有以下与目标框架一起使用的代码.net core 2.2 目标框架4.5存在问题

public async Task<string> GetConversationIdAsync()
    {
        try
        {
            var client = new HttpClient();
            var content = new StringContent(" ", Encoding.UTF8, "application/json");
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization
                = new AuthenticationHeaderValue("Bearer", "XXXXXXXXXXXX");

            var response = await client.PostAsync(
                $"https://directline.botframework.com/v3/directline/conversations", content);///Error here

            var result = await response.Content.ReadAsStringAsync();
            return result;
        }
        catch (Exception ex)
        {

            throw;
        }

    }

但这也不起作用

您不能将内容类型设置为自定义标题,请改为:

webRequest.ContentType = "application/json";
此外,您需要在POST请求中包含内容长度:

string content = " ";
webRequest.ContentLength = content.Length;
之后,您需要写入数据:

  using (var streamWriter = new StreamWriter(webRequest.GetRequestStream()))
            {
                streamWriter.Write(content);
                streamWriter.Flush();
                streamWriter.Close();
            }
如果基础连接关闭,请确保初始化ServicePointManager,它将阻止基础连接关闭:

 public Form1()
        {
             ServicePointManager.Expect100Continue = true;
             ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
             InitializeComponent();
        }
最后我们有一个请求,看起来像这样:

System.Net.WebRequest webRequest = System.Net.WebRequest.Create("https://directline.botframework.com/v3/directline/conversations");
        webRequest.Method = "POST";
        string content = " ";
        webRequest.Headers.Add("Authorization", "Bearer XXXXXXXXXXXXXX");
        webRequest.ContentType = "application/json";
        webRequest.ContentLength = content.Length;
        using (var streamWriter = new StreamWriter(webRequest.GetRequestStream()))
        {
            streamWriter.Write(content);
            streamWriter.Flush();
            streamWriter.Close();
        }
        System.Net.WebResponse resp = webRequest.GetResponse();
        System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
        string result = sr.ReadToEnd().Trim();
        sr.Close();
        resp.Close();
您只需要使用正确的授权头,否则您将获得

(403)禁止


来自服务器的响应

您不能将内容类型设置为自定义标题,请改为:

webRequest.ContentType = "application/json";
此外,您需要在POST请求中包含内容长度:

string content = " ";
webRequest.ContentLength = content.Length;
之后,您需要写入数据:

  using (var streamWriter = new StreamWriter(webRequest.GetRequestStream()))
            {
                streamWriter.Write(content);
                streamWriter.Flush();
                streamWriter.Close();
            }
如果基础连接关闭,请确保初始化ServicePointManager,它将阻止基础连接关闭:

 public Form1()
        {
             ServicePointManager.Expect100Continue = true;
             ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
             InitializeComponent();
        }
最后我们有一个请求,看起来像这样:

System.Net.WebRequest webRequest = System.Net.WebRequest.Create("https://directline.botframework.com/v3/directline/conversations");
        webRequest.Method = "POST";
        string content = " ";
        webRequest.Headers.Add("Authorization", "Bearer XXXXXXXXXXXXXX");
        webRequest.ContentType = "application/json";
        webRequest.ContentLength = content.Length;
        using (var streamWriter = new StreamWriter(webRequest.GetRequestStream()))
        {
            streamWriter.Write(content);
            streamWriter.Flush();
            streamWriter.Close();
        }
        System.Net.WebResponse resp = webRequest.GetResponse();
        System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
        string result = sr.ReadToEnd().Trim();
        sr.Close();
        resp.Close();
您只需要使用正确的授权头,否则您将获得

(403)禁止


来自服务器的响应

因此,在添加了两行代码之后,它也开始使用framework 4.5

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

因此,在添加了两行代码之后,它也开始使用framework 4.5

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

我们需要整个堆栈跟踪-您只提供了第一位,这不足以看到问题所在。调用堆栈很好,但这里实际的异常(类型+消息)更有用我们需要整个堆栈跟踪-您只提供了第一位,这不足以看到问题所在。调用堆栈很好,但是在这里,实际的异常(类型+消息)会更有用是的,它有效,谢谢,它有效,谢谢