我正在尝试使用以下代码
发布使用Web客户端从用户收到的消息包括:
using System;
using System.Net;
using System.Threading;
using Newtonsoft.Json;
using Microsoft.Bot.Builder.Azure;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
代码:
URIBase = new Uri("https://somedomain.com/");
var builder = new UriBuilder($"{UriBase}/analytics/message?");
using (WebClient webclient = new WebClient())
{
//Set the encoding to UTF8
webclient.Encoding = System.Text.Encoding.UTF8;
webclient.Headers.Add("Content-Type", "application/json");
var postBody = $"{{\"message\": \"{this.answer}\", \"key\": \"7F02D18E-88E7-486D-B51F-550118491CB1\"}}";
webclient.UploadString(builder.Uri, postBody);
}
在编译过程中,我收到以下错误:
2017-09-08T15:06:55.842 D:\home\site\wwwroot\messages\RootDialog.csx(98,17): error CS0103: The name 'URIBase' does not exist in the current context
2017-09-08T15:06:55.842 D:\home\site\wwwroot\messages\RootDialog.csx(99,49): error CS0103: The name 'UriBase' does not exist in the current context
2017-09-08T15:06:55.842 D:\home\site\wwwroot\messages\RootDialog.csx(113,17): error CS0103: The name 'webclient' does not exist in the current context
2017-09-08T15:06:55.842 D:\home\site\wwwroot\messages\RootDialog.csx(114,38): error CS0103: The name 'webclient' does not exist in the current context
2017-09-08T15:06:55.842 D:\home\site\wwwroot\messages\RootDialog.csx(114,74): error CS0103: The name 'postBody' does not exist in the current context
2017-09-08T15:06:55.842 D:\home\site\wwwroot\messages\RootDialog.csx(115,17): error CS0103: The name 'chatresponse' does not exist in the current context
2017-09-08T15:06:55.842 D:\home\site\wwwroot\messages\RootDialog.csx(115,32): error CS0103: The name 'JObject' does not exist in the current context
从错误中看来它找不到WebClient
和UriBase
,但我认为它们在System.Net
,所以我不确定发生了什么。我在其他帖子中读过使用WebClient
的人,所以似乎有办法。谁能看到我做错了什么?
答案 0 :(得分:1)
URIBase
需要一种类型而且你有拼写错误。将这两行更改为:
var uriBase = new Uri("https://somedomain.com/");
var builder = new UriBuilder($"{uriBase}/analytics/message?");
请注意var
上的uriBase
,允许变量假定其已分配的类型。您也可以明确声明变量类型,如下所示:
Uri uriBase = new Uri("https://somedomain.com/");
由于这个原因,可能发生了其余的错误。