首页 新闻 赞助 找找看

MVC中HTTP请求方式放到.Net core中出错

0
悬赏园豆:20 [已解决问题] 解决于 2017-12-01 10:27

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错误

 

请问大佬们这个是为什么,应该怎么弄。感恩!!!

笑叹、的主页 笑叹、 | 初学一级 | 园豆:34
提问于:2017-11-30 15:37
< >
分享
最佳答案
0

加上 mediaType 试试

var stringContent = new StringContent(
                    JsonConvert.SerializeObject(value),
                    Encoding.UTF8,
                    "application/json");
收获园豆:20
dudu | 高人七级 |园豆:31075 | 2017-11-30 16:51
   Message msg = new Message() { text = message };
                    StringContent cont = new StringContent(JsonConvert.SerializeObject(msg),Encoding.UTF8,"application/json");

下面的加这个方法后可以了,感恩!!!

笑叹、 | 园豆:34 (初学一级) | 2017-12-01 10:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册