【问题标题】:Visual Studio reports that not all code path return a value, even though they doVisual Studio 报告并非所有代码路径都返回一个值,即使它们确实如此
【发布时间】:2010-06-01 02:26:21
【问题描述】:

我在 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;
   }
  }

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

【问题讨论】:

  • 顺便说一句,您是否在 VS 选项中将 Tabs 设置为 4 个空格?
  • 是的,为什么? .. ... (char limt..)
  • 哦,是的,我明白了。 VS 设置为硬标签,我猜 SO 没有正确解析。

标签: c# .net http .net-micro-framework


【解决方案1】:

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

【讨论】:

  • 啊,谢谢。我把我的支架搞砸了,没有注意到。我会在 6 分钟内接受..
【解决方案2】:

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

在我看来,如果缩进多于 1 个空格,这会更清楚地很多

【讨论】:

  • 我缩进到硬标签。由于某种原因,SO 没有正确解析它们。
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    resp == null 的情况下,您没有return

    【讨论】:

      【解决方案5】:

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-07
        • 2017-07-14
        • 1970-01-01
        • 1970-01-01
        • 2011-12-17
        • 1970-01-01
        相关资源
        最近更新 更多