Visual Studio 报告并非所有代码路径都返回值,即使它们返回值

发布于 2024-09-04 01:51:11 字数 2335 浏览 10 评论 0原文

我正在用 NETMF C# 编写一个 API,其中包含发送 HTTP 请求的函数。对于熟悉 NETMF 的人来说,这是“webClient”示例的经过大量修改的版本,它是一个简单的应用程序,演示如何提交 HTTP 请求并接收响应。在示例中,它只是打印响应并返回 void,。然而,在我的版本中,我需要它来返回 HTTP 响应。

由于某种原因,Visual Studio 报告并非所有代码路径都返回值,尽管据我所知,它们确实返回值。

这是我的代码...

  /// <summary>
  /// This is a modified webClient
  /// </summary>
  /// <param name="url"></param>
  private string httpRequest(string url)
  {
   // Create an HTTP Web request.
   HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;

   // Set request.KeepAlive to use a persistent connection. 
   request.KeepAlive = true;

   // Get a response from the server.
    WebResponse resp = request.GetResponse();

   // Get the network response stream to read the page data.
   if (resp != null)
   {
    Stream respStream = resp.GetResponseStream();
    string page = "";
    byte[] byteData = new byte[4096];
    char[] charData = new char[4096];
    int bytesRead = 0;
    Decoder UTF8decoder = System.Text.Encoding.UTF8.GetDecoder();
    int totalBytes = 0;

    // allow 5 seconds for reading the stream
    respStream.ReadTimeout = 5000;

    // If we know the content length, read exactly that amount of 
    // data; otherwise, read until there is nothing left to read.
    if (resp.ContentLength != -1)
    {
     for (int dataRem = (int)resp.ContentLength; dataRem > 0; )
     {
      Thread.Sleep(500);
      bytesRead = respStream.Read(byteData, 0, byteData.Length);

      if (bytesRead == 0)
       throw new Exception("Data laes than expected");

      dataRem -= bytesRead;

      // Convert from bytes to chars, and add to the page 
      // string.
      int byteUsed, charUsed;
      bool completed = false;
      totalBytes += bytesRead;
      UTF8decoder.Convert(byteData, 0, bytesRead, charData, 0,
       bytesRead, true, out byteUsed, out charUsed,
       out completed);
      page = page + new String(charData, 0, charUsed);
     }

     page = new String(System.Text.Encoding.UTF8.GetChars(byteData));
    }
    else
     throw new Exception("No content-Length reported");

    // Close the response stream.  For Keep-Alive streams, the 
    // stream will remain open and will be pushed into the unused 
    // stream list.
    resp.Close();
    return page;
   }
  }

有什么想法吗?谢谢...

I have an API in NETMF C# that I am writing that includes a function to send an HTTP request. For those who are familiar with NETMF, this is a heavily modified version of the "webClient" example, which a simple application that demonstrates how to submit an HTTP request, and recive a response. In the sample, it simply prints the response and returns void,. In my version, however, I need it to return the HTTP response.

For some reason, Visual Studio reports that not all code paths return a value, even though, as far as I can tell, they do.

Here is my code...

  /// <summary>
  /// This is a modified webClient
  /// </summary>
  /// <param name="url"></param>
  private string httpRequest(string url)
  {
   // Create an HTTP Web request.
   HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;

   // Set request.KeepAlive to use a persistent connection. 
   request.KeepAlive = true;

   // Get a response from the server.
    WebResponse resp = request.GetResponse();

   // Get the network response stream to read the page data.
   if (resp != null)
   {
    Stream respStream = resp.GetResponseStream();
    string page = "";
    byte[] byteData = new byte[4096];
    char[] charData = new char[4096];
    int bytesRead = 0;
    Decoder UTF8decoder = System.Text.Encoding.UTF8.GetDecoder();
    int totalBytes = 0;

    // allow 5 seconds for reading the stream
    respStream.ReadTimeout = 5000;

    // If we know the content length, read exactly that amount of 
    // data; otherwise, read until there is nothing left to read.
    if (resp.ContentLength != -1)
    {
     for (int dataRem = (int)resp.ContentLength; dataRem > 0; )
     {
      Thread.Sleep(500);
      bytesRead = respStream.Read(byteData, 0, byteData.Length);

      if (bytesRead == 0)
       throw new Exception("Data laes than expected");

      dataRem -= bytesRead;

      // Convert from bytes to chars, and add to the page 
      // string.
      int byteUsed, charUsed;
      bool completed = false;
      totalBytes += bytesRead;
      UTF8decoder.Convert(byteData, 0, bytesRead, charData, 0,
       bytesRead, true, out byteUsed, out charUsed,
       out completed);
      page = page + new String(charData, 0, charUsed);
     }

     page = new String(System.Text.Encoding.UTF8.GetChars(byteData));
    }
    else
     throw new Exception("No content-Length reported");

    // Close the response stream.  For Keep-Alive streams, the 
    // stream will remain open and will be pushed into the unused 
    // stream list.
    resp.Close();
    return page;
   }
  }

Any ideas? Thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

朦胧时间 2024-09-11 01:51:11

显然,如果 (resp == null) 那么你仍然需要返回一些东西......

Clearly, if (resp == null) then you still need to return something...

玩世 2024-09-11 01:51:11

如果 resp != null 测试失败,该方法将结束而不返回任何内容。您需要在此处返回某些内容或抛出响应流为空的异常。

在我看来,如果缩进的空格多于 1 个,就会更加更加清晰。

If the resp != null test fails, the method ends without returning anything. You need to either return something here or throw an exception that the response stream was null.

In my opinion, this would be a lot clearer to see if you indented a bit more than 1 space.

我早已燃尽 2024-09-11 01:51:11

据我所知,如果 resp == null 页面不会返回任何内容

As far as I can tell, If resp == null the page isn't returning anything

千と千尋 2024-09-11 01:51:11

对于 resp == null 的情况,您没有 return

You don't have a return on the case that resp == null.

┈┾☆殇 2024-09-11 01:51:11

page = new String(System.Text.Encoding.UTF8.GetChars(byteData));

page = new String(System.Text.Encoding.UTF8.GetChars(byteData));之后

---HERE ---

    }
    else
     throw new Exception("No content-Length rep

After the page = new String(System.Text.Encoding.UTF8.GetChars(byteData));

page = new String(System.Text.Encoding.UTF8.GetChars(byteData));

---HERE ---

    }
    else
     throw new Exception("No content-Length rep
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文