写一个http post请求到在localhost上运行的bot框架

时间:2018-04-30 05:35:05

标签: c# botframework

我在http://localhost:3978/api/messages上运行了一个机器人。 我可以使用http post请求到bot的消息传递端点,而不是使用模拟器调试它吗? 如果是这样,我该怎么做呢?

我正在使用c#microsoft bot框架,我是这个应用程序的新手。 我不想使用任何渠道或DirectLine api,只需使用Httpclient。

2 个答案:

答案 0 :(得分:2)

您可以使用类似下面的代码使用C#执行此操作。请注意,您必须通过为您的需求设置适当的属性来构造要发送的Activity,这不包含在此代码中。

//make a call to get an auth token
string token;
using (var client = new WebClient())
{
    var values = new NameValueCollection();
    values["grant_type"] = "client_credentials";
    values["client_id"] = "YOUR APP ID";
    values["client_secret"] = "NcOXRwb51joibEfzUuNE04u";
    values["scope"] = "YOUR APP ID/.default";

    var response =
        client.UploadValues("https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token", values);

    var responseString = Encoding.Default.GetString(response);
    var result = JsonConvert.DeserializeObject<ResponseObject>(responseString);
    token = result.access_token;
}

//you will need to adjust this value for your project.
//this example for a proxy project so the service url here is
//just an arbitrary endpoint I was using to send activities to
activity.ServiceUrl = "http://localhost:4643/api/return";    

var jsonActivityAltered = JsonConvert.SerializeObject(activity);

using (var client = new WebClient())
{
    client.Headers.Add("Content-Type", "application/json");
    client.Headers.Add("Authorization", $"Bearer {token}");

    try
    {
        var btmResponse = client.UploadString("http://localhost:3971/api/messages", jsonActivityAltered);
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        throw;
    }
}

答案 1 :(得分:-1)

你尝试过像邮差这样的东西吗? (它免费且易于使用)

https://www.getpostman.com/

您也可以在邮递员中编写脚本

否则您只需在浏览器中转到API的端点

即可

http://localhost:3978/api/

我看到你提到你想制作一个控制台应用程序。

你可以这样做。我建议使用邮递员。

以下是使用Bearer令牌发送文件以及一些查询字符串数据和身份验证的示例。

对不起它可能不准确。如果有

,必须从某些代码示例中复制粘贴/删除
        using (HttpClient client = new HttpClient())
        {
            JObject jsonModel = new JObject();
            client.BaseAddress = new Uri("http://localhost:3978/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AuthToken);

                using (var multipartFormDataContent = new MultipartFormDataContent())
                {
                    var values = new[]
                    {
                        new KeyValuePair<string, string>("firstname", lastname),
                        new KeyValuePair<string, string>("lastname", lastname),
                        new KeyValuePair<string, string>("payloadFile", FileName)
                    };

                    foreach (var keyValuePair in values)
                    {
                        multipartFormDataContent.Add(new StringContent(keyValuePair.Value),
                        String.Format("\"{0}\"", keyValuePair.Key));
                    }

                    ByteArrayContent fileContent = new ByteArrayContent(File.ReadAllBytes(HttpContext.Current.Server.MapPath("~/uploads/output/" + FileName)));
                    string FullxmlString = File.ReadAllText(Path.Combine(HttpContext.Current.Server.MapPath("~/uploads/output/" + FileName)));


                    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("payloadFile") { FileName = "payloadFile" };
                    multipartFormDataContent.Add(fileContent);

                    HttpResponseMessage response = client.PostAsync("api/message", multipartFormDataContent).Result;
                    string returnString = response.Content.ToString();

                    using (HttpContent content = response.Content)
                    {
                        string res = "";
                        Task<string> result = content.ReadAsStringAsync();
                        res = result.Result;
                    }
                }
       }