【问题标题】:Argument out of range exception error in C#C#中的参数超出范围异常错误
【发布时间】:2012-07-08 18:03:12
【问题描述】:

我的方法是这样的。 (Grabbed from here)

private void inetConvert() {
    byte[] buf = new byte[1024];
    string result;
    string xeString = String.Format("http://www.xe.com/ucc/convert.cgi?Amount=1&From={0}&To={1}", srcCurrency, dstCurrency);
    System.Net.WebRequest wreq = System.Net.WebRequest.Create(new Uri(xeString));
    System.Net.WebResponse wresp = wreq.GetResponse();
    Stream respstr = wresp.GetResponseStream();
    int read = respstr.Read(buf, 0, 10240); // Error
    result = Encoding.ASCII.GetString(buf, 0, read); 
    curRateLbl.Text= result;
}

问题是,当应用程序执行这个应用程序在挂起大约 4-5 秒后得到这个屏幕

我错过了什么?

【问题讨论】:

标签: c# winforms networking


【解决方案1】:

缓冲区的大小是 1024,但您告诉 Read 它最多可以将 10240(大小的十倍)字节放入缓冲区。如文件所述,它抛出是因为

偏移量和计数之和大于缓冲区长度。

【讨论】:

    【解决方案2】:

    最后你有一个额外的 0。应该是

      int read = respstr.Read(buf, 0, 1024); // Error 
    

    这就是您在应用程序中使用常量的原因,以避免那些胖乎乎的手指错误。

    private void inetConvert() {  
        private const BUFFER_SIZE = 1024;
        byte[] buf = new byte[BUFFER_SIZE];  
        string result;  
        string xeString = String.Format("http://www.xe.com/ucc/convert.cgi?Amount=1&From={0}&To={1}", srcCurrency, dstCurrency);  
        System.Net.WebRequest wreq = System.Net.WebRequest.Create(new Uri(xeString));  
    
        // VERY IMPORTANT TO CLEAN UP RESOURCES FROM ANY OBJECT THAT IMPLEMENTS IDisposable
    
        using(System.Net.WebResponse wresp = wreq.GetResponse()) 
        using(Stream respstr = wresp.GetResponseStream())
        {
          int read = respstr.Read(buf, 0, BUFFER_SIZE); // Error  
          result = Encoding.ASCII.GetString(buf, 0, read);   
          curRateLbl.Text= result;  
        }
    }  
    

    另请注意,您没有正确关闭 Stream 对象。您可能会考虑使用 using 语句来帮助管理流中的资源。

    但是...我会这样做。

    private void inetConvert() 
    {   
        string xeString= String.Format("http://www.xe.com/ucc/convert.cgi?Amount=1&From={0}&To={1}", srcCurrency, dstCurrency);  
    
        System.Net.WebRequest wreq = System.Net.WebRequest.Create(new Uri(xeString));  
    
        // VERY IMPORTANT TO CLEAN UP RESOURCES FROM ANY OBJECT THAT IMPLEMENTS IDisposable
    
        using(System.Net.WebResponse wresp = wreq.GetResponse()) 
        using (Stream stream = response.GetResponseStream()) 
        { 
            StreamReader reader = new StreamReader(stream, Encoding.UTF8); 
            curRateLbl.Text = reader.ReadToEnd(); 
        } 
    }  
    

    【讨论】:

    • 没有。阅读源博客。它们是 2 个不同的变量 int read = respstr.Read(buf, 0, BUFFER_SIZE);
    • 什么是buf,什么是size?他们需要多少?
    • @epic_syntax - 它们可以是你需要的任何大小,但在这种情况下它们应该是相同的,因为你试图填满整个数组。
    • @ChrisGessler:这里没有必要使用 const。只需在Read 调用中使用buf.Length。请注意,如果您使用常量,最好将其命名为BufferSize - C# 中的常量通常不是 SHOUTY_CASE。
    • @JonSkeet - 我完全同意,感谢您提及。我使用 const 来说明胖手指,这可能在 OPs 应用程序的其他部分中证明是有用的。另请注意,const 来自用户提供的链接中的原始来源。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多