Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
飞利浦色调c#/json/http_C#_Json - Fatal编程技术网

飞利浦色调c#/json/http

飞利浦色调c#/json/http,c#,json,C#,Json,所以我正在准备另一个教程request.GetRequestStreamAsync()应该是request.GetRequestStream()(根据教程)我假设它不工作或出现错误。因为您确实没有问任何问题,但是您可以尝试将JSON发送/接收放入try/catch中。捕获即将返回的WebException var request = (HttpWebRequest)WebRequest.Create("http://192.168.1.3/api/139f12ce32a30c473368dbe2

所以我正在准备另一个教程
request.GetRequestStreamAsync()应该是request.GetRequestStream()
(根据教程)我假设它不工作或出现错误。因为您确实没有问任何问题,但是您可以尝试将JSON发送/接收放入try/catch中。捕获即将返回的WebException

var request = (HttpWebRequest)WebRequest.Create("http://192.168.1.3/api/139f12ce32a30c473368dbe25f6586b/lights/1/state");
request.ContentType = "application/json";
request.Method = "POST";
try {
   using (var streamWriter = new StreamWriter(request.GetRequestStreamAsync()))
    {
        string json = "{\"on\":\false}";

        streamWriter.Write(json);
        streamWriter.Flush();
    }
}
catch (WebException)
{ 
    //error handling
}
编辑:确保您有对以下内容的引用:

  • 系统.Net
  • System.Net.Http
  • System.Net.Http.WebRequest

我假设它不工作或出现错误。因为您确实没有问任何问题,但是您可以尝试将JSON发送/接收放入try/catch中。捕获即将返回的WebException

var request = (HttpWebRequest)WebRequest.Create("http://192.168.1.3/api/139f12ce32a30c473368dbe25f6586b/lights/1/state");
request.ContentType = "application/json";
request.Method = "POST";
try {
   using (var streamWriter = new StreamWriter(request.GetRequestStreamAsync()))
    {
        string json = "{\"on\":\false}";

        streamWriter.Write(json);
        streamWriter.Flush();
    }
}
catch (WebException)
{ 
    //error handling
}
编辑:确保您有对以下内容的引用:

  • 系统.Net
  • System.Net.Http
  • System.Net.Http.WebRequest

    • 在你的帖子中没有问题,但是

      .getRequestStream() is not a known function in Visual Studio.
      
      它不是,而是请求。GetRequestStream()是

      您将在代码中这样使用:

          using (var streamWriter = new StreamWriter(request.GetRequestStream())
          {
      
          }
      
      如果要使用
      GetRequestStreamAsync()


      但是您的方法必须具有
      async
      关键字。由于您正在从重写调用
      GetRequestStreamAsync()
      ,并且您无法重写非异步方法并使其异步,因此您基本上无法从该方法执行
      GetRequestStreamAsync()
      (除非您从该方法调用另一个
      异步的方法)。

      在您的帖子中没有问题,但是

      .getRequestStream() is not a known function in Visual Studio.
      
      它不是,而是请求。GetRequestStream()是

      您将在代码中这样使用:

          using (var streamWriter = new StreamWriter(request.GetRequestStream())
          {
      
          }
      
      如果要使用
      GetRequestStreamAsync()



      但是您的方法必须具有
      async
      关键字。由于您正在从重写调用
      GetRequestStreamAsync()
      ,并且您无法重写非异步方法并使其异步,因此您基本上无法从该方法执行
      GetRequestStreamAsync()
      (除非您从该方法调用另一个
      async
      ).

      添加问题和更多详细信息:我遇到以下错误:“HttpWebRequest”不包含“GetRequestStream”的定义,所以我是否缺少引用?我已经有System.Net作为参考。而且,我是作为windows通用应用程序来做这件事的,我不知道这是否有什么不同。感谢您的任何输入结果表明,Universal Window应用程序不支持“System.Net.Http.WebRequest”@VictorProcure经过更多搜索后,我想我需要使用System.Net.Requests来完成我正在尝试的操作。感谢您的帮助。添加问题和更多详细信息:我遇到以下错误:“HttpWebRequest”不包含“GetRequestStream”的定义,所以我是否缺少引用?我已经有System.Net作为参考。而且,我是作为windows通用应用程序来做这件事的,我不知道这是否有什么不同。感谢您的任何输入结果表明,Universal Window应用程序不支持“System.Net.Http.WebRequest”@VictorProcure经过更多搜索后,我想我需要使用System.Net.Requests来完成我正在尝试的操作。感谢您的帮助。感谢您的输入,try/catch只是跳过了.GetRequestStream,因为它是一个无效的引用。这是我没有指定的错误。我已使用需要添加的引用进行了更新,以使其正常工作,因此System.Net.Http.WebRequest无效。我原以为使用System.Net会引用下面的所有内容。那么我必须更新我的系统.Net引用还是什么?我找不到任何地方可以下载引用并导入它。再一次-对C#来说是个超级新手,所以我现在在这里。在visual studio中的references位置下添加引用。右键单击引用->添加引用。应该在assembliesThank的输入下,try/catch只是跳过.GetRequestStream,因为它是一个无效的引用。这是我没有指定的错误。我已使用需要添加的引用进行了更新,以使其正常工作,因此System.Net.Http.WebRequest无效。我原以为使用System.Net会引用下面的所有内容。那么我必须更新我的系统.Net引用还是什么?我找不到任何地方可以下载引用并导入它。再一次-对C#来说是个超级新手,所以我现在在这里。在visual studio中的references位置下添加引用。右键单击引用->添加引用。应该在assembliesrequest下。GetRequestStream对我不起作用。好像我没有正确的参考资料。但是GetRequestStreamAsync是一个有效的引用—但是正如您所指出的,对于我正在尝试的工作也不起作用。感谢inputI对async的实际工作做了更多的了解,并成功地使其工作。我还意识到System.Net.Http具有我所需的所有方法,并且是该系统。Net.Http.Webrequests在通用windows应用程序中不可用,这就是我无法使用它的原因。request.GetRequestStream对我不起作用。好像我没有正确的参考资料。但是GetRequestStreamAsync是一个有效的引用—但是正如您所指出的,对于我正在尝试的工作也不起作用。感谢inputI对async的实际工作做了更多的了解,并成功地使其工作。我还意识到System.Net.Http具有我所需的所有方法,并且是该系统。Net.Http.Webrequests在通用windows应用程序中不可用,这就是我无法使用它的原因。