MVC中代码
using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; using System.Web; namespace Deepleo.Web { public class MSBot { public async static Task<string> PostMessage(string message) { HttpClient client; HttpResponseMessage response; bool IsReplyReceived = false; string ReceivedString = null; client = new HttpClient(); client.BaseAddress = new Uri("https://directline.botframework.com/api/conversations/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("BotConnector", "uc0nyMvM0NI.cwA.wYs.B8F1M7cEBm9StsTDG8pmuOjhnxeCJd2LdvNlKVfBgro"); // for leon client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("BotConnector", "McfcUTJYJTo.cwA.mfY.nuvLSsBQYETBygIfHUCIIHOpBz-LnAUWEGf-QMOFxR8"); // for doudou //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("BotConnector", "iCoMHD3whk8.cwA.50s.vlUvoUn7IjjRuHkSeNZQ2dOIHHTOftz07bAJRF2WJy8"); response = await client.GetAsync("/api/tokens/"); if (response.IsSuccessStatusCode) { var conversation = new Conversation(); response = await client.PostAsJsonAsync("/api/conversations/", conversation); //response = await client.PostAsync("/api/conversations/", null); if (response.IsSuccessStatusCode) { Conversation ConversationInfo = response.Content.ReadAsAsync(typeof(Conversation)).Result as Conversation; string conversationUrl = ConversationInfo.conversationId + "/messages/"; Message msg = new Message() { text = message }; response = await client.PostAsJsonAsync(conversationUrl, msg); if (response.IsSuccessStatusCode) { response = await client.GetAsync(conversationUrl); if (response.IsSuccessStatusCode) { MessageSet BotMessage = response.Content.ReadAsAsync(typeof(MessageSet)).Result as MessageSet; ReceivedString = BotMessage.messages[1].text; IsReplyReceived = true; } } } } return ReceivedString; } } public class Conversation { public string conversationId { get; set; } public string token { get; set; } public string eTag { get; set; } } public class MessageSet { public Message[] messages { get; set; } public string watermark { get; set; } public string eTag { get; set; } } public class Message { public string id { get; set; } public string conversationId { get; set; } public DateTime created { get; set; } public string from { get; set; } public string text { get; set; } public string channelData { get; set; } public string[] images { get; set; } public Attachment[] attachments { get; set; } public string eTag { get; set; } } public class Attachment { public string url { get; set; } public string contentType { get; set; } } }
将这个类放到.Net core中
这里出现错误,然后添加Microsoft.AspNet.WebApi.Client这个Nuget包
http请求时提示400错误
Conversation ConversationInfo = response.Content.ReadAsAsync(typeof(Conversation)).Result as Conversation; string conversationUrl = ConversationInfo.conversationId + "/messages/"; Message msg = new Message() { text = message }; response = await client.PostAsJsonAsync(conversationUrl, msg);//出错位置
将代码修改为
public async static Task<string> PostMessage1() { HttpClient client; HttpResponseMessage response; bool IsReplyReceived = false; string ReceivedString = null; client = new HttpClient(); client.BaseAddress = new Uri("https://directline.botframework.com/api/conversations/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("BotConnector", "McfcUTJYJTo.cwA.5EE.wf-n-XYX9Rc5V0laWVRuHagi2jPmkTprd1Q1ufA0TKA"); response = await client.GetAsync("/api/tokens/"); if (response.IsSuccessStatusCode) { var conversation = new Conversation(); StringContent content = new StringContent(JsonConvert.SerializeObject(conversation)); response = await client.PostAsync("/api/conversations/", content); //response = await client.PostAsync("/api/conversations/", null); if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); Conversation ConversationInfo = JsonConvert.DeserializeObject<Conversation>(json); string conversationUrl = ConversationInfo.conversationId + "/messages/"; Message msg = new Message() { text = message }; StringContent cont = new StringContent(JsonConvert.SerializeObject(msg)); response = await client.PostAsync(conversationUrl, cont); if (response.IsSuccessStatusCode) { response = await client.GetAsync(conversationUrl); if (response.IsSuccessStatusCode) { string str = await response.Content.ReadAsStringAsync(); MessageSet BotMessage = JsonConvert.DeserializeObject<MessageSet>(str); ReceivedString = BotMessage.messages[1].text; IsReplyReceived = true; } } } } return ReceivedString; }
http请求时提示500错误
请问大佬们这个是为什么,应该怎么弄。感恩!!!
加上 mediaType 试试
var stringContent = new StringContent( JsonConvert.SerializeObject(value), Encoding.UTF8, "application/json");
Message msg = new Message() { text = message }; StringContent cont = new StringContent(JsonConvert.SerializeObject(msg),Encoding.UTF8,"application/json");
下面的加这个方法后可以了,感恩!!!