当我点击谷歌对话流的端点但相同的谷歌对话流端点在邮递员中工作时,在 c# 中出现错误

希特什·安沙尼

我在 PostMan 中使用这个端点:-

https://api.dialogflow.com/v1/query?v=20150910

我发送的这个 JSON 字符串:-

{"query":"flights from NYC to Vadodara","sessionId":"6b9d4676-2a71-4c64-a562-8c08f198c623","lang":"pt-BR","resetContexts":false}

我正在邮递员中设置内容类型和授权。

所有这些东西都在邮递员中完美运行,但问题是当我使用 c# 代码点击此端点时,它给了我这个错误:-

我得到的错误:-

{
  "id": "7b0ac743-58ea-4d61-a41d-a299f086a816",
  "timestamp": "2018-06-04T15:30:25.873Z",
  "lang": "en",
  "status": {
    "code": 400,
    "errorType": "bad_request",
    "errorDetails": "Invalid request content type, expecting \"multipart/form-data\" or \"application/json; charset\u003dutf-8."
  }
}

这是我的代码:

using ApiAi.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpClient http = new HttpClient();

            http.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header
            http.DefaultRequestHeaders.Add("ContentType", "application/json; charset=utf-8");
            http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "xxx");

            var response = http.PostAsync("https://api.dialogflow.com/v1/query?v=20150910", new StringContent(new Program().getSessionID(new ConfigModel { AccesTokenClient = "xxx" }, "flights from NYC to Vadodara"))).Result.Content.ReadAsStringAsync().Result;
        }
        public string getSessionID(ConfigModel config, string message)
        {
            var requestData = new RequestModel
            {
                query = message,
                sessionId = (config.SessionId ?? Guid.NewGuid()).ToString(),
                lang = "pt-BR"
            };
            return JsonConvert.SerializeObject(requestData);
        }
    }
    public class RequestModel
    {
        public string query { get; set; }
        public string sessionId { get; set; }
        public string lang { get; set; }
        public bool resetContexts { get; set; }
    }
}








//  
// Copyright (c) 2017 Nick Rimmer. All rights reserved.  
// Licensed under the MIT License. See LICENSE file in the project root for full license information.  
//

using ApiAi.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ApiAi.Models
{
    /// <summary>
    /// Services configuration
    /// </summary>
    public class ConfigModel
    {
        #region magic
        internal static string 
            BaseUrl = @"https://api.dialogflow.com/v1",
            VersionCode = @"20150910";

        #endregion

        /// <summary>
        /// Each API request requires authentication to identify the agent that is responsible for making the request. Authentication is provided through an access token.
        /// The developer access token is used for managing entities and intents.
        /// </summary>
        public string AccesTokenDeveloper { get; set; }

        /// <summary>
        /// Each API request requires authentication to identify the agent that is responsible for making the request. Authentication is provided through an access token.
        /// The client access token is used for making queries.
        /// </summary>
        public string AccesTokenClient { get; set; }

        /// <summary>
        /// Specifed language in your Api.ai agent
        /// </summary>
        public LanguagesEnum Language { get; set; }

        /// <summary>
        /// Timezone requests parameter
        /// </summary>
        public string TimeZone { get; set; } = System.TimeZone.CurrentTimeZone.StandardName;

        /// <summary>
        /// Session ID for request
        /// </summary>
        public object SessionId { get; set; } = Guid.NewGuid();
    }
}

这是我正在使用的代码。提前致谢。

斯特凡

的StringContent本身具有重载设置内容类型:

var response = http.PostAsync(yourUrl, 
                              new StringContent("your json string",
                                                 Encoding.UTF8, "application/json"))
                   .Result.Content.ReadAsStringAsync().Result;

默认媒体类型设置为: text/plain

Ps:您可能希望将这些拆分为更具可读性的函数。它有助于调试。

文档对此非常含糊,但正如您在此处看到的那样StringContent它有自己的Headers属性。我的猜测是它会覆盖您的请求标头。

反编译程序集会显示媒体类型的默认值,如您所见text/plain

/// <summary>Creates a new instance of the <see cref="T:System.Net.Http.StringContent" /> class.</summary>
/// <param name="content">The content used to initialize the <see cref="T:System.Net.Http.StringContent" />.</param>
[__DynamicallyInvokable]
public StringContent(string content)
  : this(content, (Encoding) null, (string) null)
{
}

/// <summary>Creates a new instance of the <see cref="T:System.Net.Http.StringContent" /> class.</summary>
/// <param name="content">The content used to initialize the <see cref="T:System.Net.Http.StringContent" />.</param>
/// <param name="encoding">The encoding to use for the content.</param>
/// <param name="mediaType">The media type to use for the content.</param>
[__DynamicallyInvokable]
public StringContent(string content, Encoding encoding, string mediaType)
  : base(StringContent.GetContentByteArray(content, encoding))
{
  this.Headers.ContentType = new MediaTypeHeaderValue(mediaType == null ? "text/plain" : mediaType)
  {
    CharSet = encoding == null ? HttpContent.DefaultStringEncoding.WebName : encoding.WebName
  };
}

PS:你也可以试试:

//since utf-8 is the default.
http.DefaultRequestHeaders.Add("ContentType", "application/json");

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在#Include指令中使用C文件时,C程序中出现错误(多个定义错误)

来自分类Dev

如何防止在C中出现段错误(当接受错误类型的参数时)

来自分类Dev

在C中相同打印功能的第二次调用中出现错误

来自分类Dev

尝试引用char *中的第一个元素时,C中出现分段错误

来自分类Dev

谁能告诉我为什么我在此C代码中出现分段错误?

来自分类Dev

我的C ++代码中出现错误Adress Sanitizer errer不断弹出

来自分类Dev

我的C ++代码中出现错误Adress Sanitizer errer不断弹出

来自分类Dev

尝试导入头文件并使用它时在C中出现分段错误

来自分类Dev

尝试导入头文件并使用它时在C中出现分段错误

来自分类Dev

查找哈希表的值时,while循环中出现C ++分段错误

来自分类Dev

c linux中出现分段错误,但在Windows中没有

来自分类Dev

为什么在C中出现此段错误?

来自分类Dev

Visual Studio 2015中出现错误C2784,C2672和C2664

来自分类Dev

在我的PHP / SQL脚本中出现错误?

来自分类Dev

我的练习中出现JavaScript验证错误

来自分类Dev

我的LSTM中出现不可广播的错误

来自分类Dev

我的SharedPreferences项目中出现错误

来自分类Dev

Karatsuba算法-我的实现中出现错误

来自分类Dev

我的代码和beautifulsoup 中出现错误

来自分类Dev

更新/下载时ftp中出现错误

来自分类Dev

更新/下载时ftp中出现错误

来自分类Dev

C ++模板:VS2017中出现C2143错误,但在VS2013中编译

来自分类Dev

C ++:使用类中的静态函数从main创建pthread时,在Solaris Studio中出现警告(时间错误)

来自分类Dev

在C中出现错误“分段错误(核心已转储)”

来自分类Dev

为什么数据类型转换错误仅在C ++中出现而不在C中出现?

来自分类Dev

从C调用Kotlin回调函数时,如何避免在Kotlin Native中出现错误“ staticCFunction必须接受未绑定的...”?

来自分类Dev

在某些类中添加“ fno-objc-arc”后,xcode 5.0中出现C语言错误

来自分类Dev

当我尝试删除产品时,Magento中出现错误

来自分类Dev

当我从其他文件夹中编译我的C ++程序时,在可执行文件中出现分段错误

Related 相关文章

  1. 1

    在#Include指令中使用C文件时,C程序中出现错误(多个定义错误)

  2. 2

    如何防止在C中出现段错误(当接受错误类型的参数时)

  3. 3

    在C中相同打印功能的第二次调用中出现错误

  4. 4

    尝试引用char *中的第一个元素时,C中出现分段错误

  5. 5

    谁能告诉我为什么我在此C代码中出现分段错误?

  6. 6

    我的C ++代码中出现错误Adress Sanitizer errer不断弹出

  7. 7

    我的C ++代码中出现错误Adress Sanitizer errer不断弹出

  8. 8

    尝试导入头文件并使用它时在C中出现分段错误

  9. 9

    尝试导入头文件并使用它时在C中出现分段错误

  10. 10

    查找哈希表的值时,while循环中出现C ++分段错误

  11. 11

    c linux中出现分段错误,但在Windows中没有

  12. 12

    为什么在C中出现此段错误?

  13. 13

    Visual Studio 2015中出现错误C2784,C2672和C2664

  14. 14

    在我的PHP / SQL脚本中出现错误?

  15. 15

    我的练习中出现JavaScript验证错误

  16. 16

    我的LSTM中出现不可广播的错误

  17. 17

    我的SharedPreferences项目中出现错误

  18. 18

    Karatsuba算法-我的实现中出现错误

  19. 19

    我的代码和beautifulsoup 中出现错误

  20. 20

    更新/下载时ftp中出现错误

  21. 21

    更新/下载时ftp中出现错误

  22. 22

    C ++模板:VS2017中出现C2143错误,但在VS2013中编译

  23. 23

    C ++:使用类中的静态函数从main创建pthread时,在Solaris Studio中出现警告(时间错误)

  24. 24

    在C中出现错误“分段错误(核心已转储)”

  25. 25

    为什么数据类型转换错误仅在C ++中出现而不在C中出现?

  26. 26

    从C调用Kotlin回调函数时,如何避免在Kotlin Native中出现错误“ staticCFunction必须接受未绑定的...”?

  27. 27

    在某些类中添加“ fno-objc-arc”后,xcode 5.0中出现C语言错误

  28. 28

    当我尝试删除产品时,Magento中出现错误

  29. 29

    当我从其他文件夹中编译我的C ++程序时,在可执行文件中出现分段错误

热门标签

归档