通过 HttpClient 将 ObjectContent 发布到 TelegramApi reutns statusCode 400
posting ObjectContent via HttpClient to TelegramApi reutns statusCode 400
我正在开发 Telegram Bot 以使其与我们自己的应用程序同步...
我首先去了 DHC chrome 扩展,它类似于 RestClient 扩展,并测试了我应该从 Telegram 服务器发送和接收数据的方式。其次,我想在 C# 中完成,我已经实现了 getUpdate,我从 Telegram 接收传入消息。现在我正处于即将离任的阶段,我一直收到状态码 400...
谁能说说为什么?怎么办?
可以找到机器人参考 in here...
public class SendMessage<TReplyMarkup>
{
/// <summary>
/// Integer Or String
/// </summary>
public string ChatId { get; set; }
public string Text { get; set; }
public string ParseMode { get; set; }
public bool? DisableWebPagePreview { get; set; }
public bool? DisableNotification { get; set; }
public long? ReplyToMessageId { get; set; }
public TReplyMarkup ReplyMarkup { get; set; }
}
public void Process(Update update)
{
SendMessage<Object> newMessage = new SendMessage<object>();
newMessage.Text = "Posting Message";
newMessage.ChatId = update.Message.Chat.Id.ToString();
HttpClient hc = new HttpClient();
HttpContent requestContent = new ObjectContent(typeof(SendMessage<object>), newMessage, new JsonMediaTypeFormatter
{
SerializerSettings = new JsonSerializerSettings
{
ContractResolver = new CustomPropertyNamesContractResolver
{
Case = IdentifierCase.UnderscoreSeparator
}
},
SupportedEncodings = { Encoding.UTF8 }
});
var responseMessage = hc.PostAsJsonAsync("https://api.telegram.org/bot" + AppSetting.Token + "/sendMessage", requestContent).Result;
//parse, see if is ok...
}
这是我的对象状态:
我要发送的内容:
requestContent.ReadAsStringAsync()
Id = 77, Status = RanToCompletion, Method = "{null}", Result = "{\"chat_id\":\"55815152\",\"text\":\"Posting Message\",\"parse_mode\":null,\"disable_web_page_preview\":null,\"disable_notification\":null,\"reply_to_message_id\":null,\"reply_markup\":null}"
AsyncState: null
CancellationPending: false
CreationOptions: None
Exception: null
Id: 77
Result: "{\"chat_id\":\"55815152\",\"text\":\"Posting Message\",\"parse_mode\":null,\"disable_web_page_preview\":null,\"disable_notification\":null,\"reply_to_message_id\":null,\"reply_markup\":null}"
Status: RanToCompletion
我收到的内容:
responseMessage.Content.ReadAsStringAsync()
Id = 80, Status = RanToCompletion, Method = "{null}", Result = "{\"ok\":false,\"error_code\":400,\"description\":\"Bad Request: Message text is empty\"}"
AsyncState: null
CancellationPending: false
CreationOptions: None
Exception: null
Id: 80
Result: "{\"ok\":false,\"error_code\":400,\"description\":\"Bad Request: Message text is empty\"}"
Status: RanToCompletion
编辑
我发现问题出在可选字段上,如果我将 null 或空传递给它们,电报 api 仍然会寻找它,并抛出错误,如果我根本不发送它们,它将工作。现在如何告诉 contractResolver,忽略空字段...
经过一些其他测试,和一个用另一种语言实现它的朋友交谈,我发现可选参数不能为 null 也不能为空,所以我搜索找到一种不发送它们的方法,很明显我没有使用 dynamic 并创建所有模型并定义它们,我不能简单地省略这些属性的发送,所以经过一番搜索,我发现了一个我想不到的非常简单的方法...
答案如下:
NullValueHandling = NullValueHandling.Ignore
NullValueHandling
是 JsonSerializerSettings
class 的 属性,我这样使用它:
HttpContent requestContent = new ObjectContent(typeof(SendMessage<object>), newMessage,
new JsonMediaTypeFormatter
{
SerializerSettings = new JsonSerializerSettings
{
ContractResolver = new CustomPropertyNamesContractResolver
{
Case = IdentifierCase.UnderscoreSeparator
},
NullValueHandling = NullValueHandling.Ignore
},
SupportedEncodings = {Encoding.UTF8}
});
我正在开发 Telegram Bot 以使其与我们自己的应用程序同步... 我首先去了 DHC chrome 扩展,它类似于 RestClient 扩展,并测试了我应该从 Telegram 服务器发送和接收数据的方式。其次,我想在 C# 中完成,我已经实现了 getUpdate,我从 Telegram 接收传入消息。现在我正处于即将离任的阶段,我一直收到状态码 400...
谁能说说为什么?怎么办?
可以找到机器人参考 in here...
public class SendMessage<TReplyMarkup>
{
/// <summary>
/// Integer Or String
/// </summary>
public string ChatId { get; set; }
public string Text { get; set; }
public string ParseMode { get; set; }
public bool? DisableWebPagePreview { get; set; }
public bool? DisableNotification { get; set; }
public long? ReplyToMessageId { get; set; }
public TReplyMarkup ReplyMarkup { get; set; }
}
public void Process(Update update)
{
SendMessage<Object> newMessage = new SendMessage<object>();
newMessage.Text = "Posting Message";
newMessage.ChatId = update.Message.Chat.Id.ToString();
HttpClient hc = new HttpClient();
HttpContent requestContent = new ObjectContent(typeof(SendMessage<object>), newMessage, new JsonMediaTypeFormatter
{
SerializerSettings = new JsonSerializerSettings
{
ContractResolver = new CustomPropertyNamesContractResolver
{
Case = IdentifierCase.UnderscoreSeparator
}
},
SupportedEncodings = { Encoding.UTF8 }
});
var responseMessage = hc.PostAsJsonAsync("https://api.telegram.org/bot" + AppSetting.Token + "/sendMessage", requestContent).Result;
//parse, see if is ok...
}
这是我的对象状态:
我要发送的内容:
requestContent.ReadAsStringAsync()
Id = 77, Status = RanToCompletion, Method = "{null}", Result = "{\"chat_id\":\"55815152\",\"text\":\"Posting Message\",\"parse_mode\":null,\"disable_web_page_preview\":null,\"disable_notification\":null,\"reply_to_message_id\":null,\"reply_markup\":null}"
AsyncState: null
CancellationPending: false
CreationOptions: None
Exception: null
Id: 77
Result: "{\"chat_id\":\"55815152\",\"text\":\"Posting Message\",\"parse_mode\":null,\"disable_web_page_preview\":null,\"disable_notification\":null,\"reply_to_message_id\":null,\"reply_markup\":null}"
Status: RanToCompletion
我收到的内容:
responseMessage.Content.ReadAsStringAsync()
Id = 80, Status = RanToCompletion, Method = "{null}", Result = "{\"ok\":false,\"error_code\":400,\"description\":\"Bad Request: Message text is empty\"}"
AsyncState: null
CancellationPending: false
CreationOptions: None
Exception: null
Id: 80
Result: "{\"ok\":false,\"error_code\":400,\"description\":\"Bad Request: Message text is empty\"}"
Status: RanToCompletion
编辑
我发现问题出在可选字段上,如果我将 null 或空传递给它们,电报 api 仍然会寻找它,并抛出错误,如果我根本不发送它们,它将工作。现在如何告诉 contractResolver,忽略空字段...
经过一些其他测试,和一个用另一种语言实现它的朋友交谈,我发现可选参数不能为 null 也不能为空,所以我搜索找到一种不发送它们的方法,很明显我没有使用 dynamic 并创建所有模型并定义它们,我不能简单地省略这些属性的发送,所以经过一番搜索,我发现了一个我想不到的非常简单的方法...
答案如下:
NullValueHandling = NullValueHandling.Ignore
NullValueHandling
是 JsonSerializerSettings
class 的 属性,我这样使用它:
HttpContent requestContent = new ObjectContent(typeof(SendMessage<object>), newMessage,
new JsonMediaTypeFormatter
{
SerializerSettings = new JsonSerializerSettings
{
ContractResolver = new CustomPropertyNamesContractResolver
{
Case = IdentifierCase.UnderscoreSeparator
},
NullValueHandling = NullValueHandling.Ignore
},
SupportedEncodings = {Encoding.UTF8}
});