C# 索引超出了数组错误的范围

C# 索引超出了数组错误的范围,c#,C#,我目前正在用我的输入文本框制作代理检查器。我得到了C#错误:索引超出了代理的数组边界(123.34.123.45:8080) 我的代码将是… string occoultProxy = "123.34.123.45:8080"; WebProxy proxy = new WebProxy(occoultProxy.Split(':')[0], Convert.ToInt32(occoultProxy.Split(':')[1])); //Error at this line // WebProx

我目前正在用我的输入文本框制作代理检查器。我得到了C#错误:索引超出了代理的数组边界(123.34.123.45:8080)

我的代码将是…

string occoultProxy = "123.34.123.45:8080";
WebProxy proxy = new WebProxy(occoultProxy.Split(':')[0], Convert.ToInt32(occoultProxy.Split(':')[1])); //Error at this line
// WebProxy(string Host, int Port)

更新

我尝试了其他代码,但仍然有错误代码。请帮忙

 string[] address = occoultProxy.Split(new[] { ':' });
 MessageBox.Show(address[0].ToString());
 MessageBox.Show(address[1].ToString());
 WebProxy proxyHTTP = new WebProxy(address[0], Convert.ToInt32(address[1]));
输出

123.34.345.23 <!-- Some Proxy here, seems good here -->
IndexOutOfRangeException was unhandled( Index was outside the bounds of the array.)
123.34.345.23
未处理IndexOutOfRangeException(索引超出了数组的边界。)

这对你有用吗

        var occoultProxy = "123.34.123.45:8080";
        var parts = occoultProxy.Split(':');

        if (parts.Length == 2)
        {
            var proxy = new WebProxy(parts[0], Convert.ToInt32(parts[1]));
        }
        else
        {
            throw new AnExceptionToHandleInYourUi();
        }

这对你有用吗

        var occoultProxy = "123.34.123.45:8080";
        var parts = occoultProxy.Split(':');

        if (parts.Length == 2)
        {
            var proxy = new WebProxy(parts[0], Convert.ToInt32(parts[1]));
        }
        else
        {
            throw new AnExceptionToHandleInYourUi();
        }
String.Split()不接受单个字符作为参数。看

尝试将代码更改为

string[] address = occoultProxy.Split(new[] {':'});
WebProxy proxy = new WebProxy(address[0], Convert.ToInt32(address[1]));
String.Split()不接受单个字符作为参数。看

尝试将代码更改为

string[] address = occoultProxy.Split(new[] {':'});
WebProxy proxy = new WebProxy(address[0], Convert.ToInt32(address[1]));

您的输入不能有端口段,您可以处理此问题:

WebProxy proxy = new WebProxy(new Uri("http://" + occoultProxy)); 

您的输入不能有端口段,您可以处理此问题:

WebProxy proxy = new WebProxy(new Uri("http://" + occoultProxy)); 

如果您正在尝试将本地代理与本地ip连接,请检查occoultProxy的值是否为“::1”。

如果您正在尝试将本地代理与本地ip连接,请检查occoultProxy的值是否为“::1”。

它对我有效。§获取occoultProxy是否有问题?我猜
occoultProxy
的值是一个没有端口号的IP。我尝试了您的示例,它运行时没有问题。您确定传递给语句的字符串始终包含分号吗?因为您允许用户输入地址,所以应该检查字符串中的分号。这对我很有用。§获取occoultProxy是否有问题?我猜
occoultProxy
的值是一个没有端口号的IP。我尝试了您的示例,它运行时没有问题。您确定传递给语句的字符串始终包含分号吗?因为您允许用户输入地址,所以您应该检查字符串中的分号。或者将端口设置为80,或者将端口设置为80有一个带有
params char[]
参数的重载<代码>公共字符串[]拆分(参数字符[]分隔符)。你可以像调用
occoultProxy.Split(':')
occoultProxy.Split(':','.')
一样使用任意数量的参数来调用它。是的,在发布后意识到了这一点。我想我应该先运行代码。不过我一直使用新的[]{'}语法。有一个重载包含
params char[]
参数<代码>公共字符串[]拆分(参数字符[]分隔符)。你可以像调用
occoultProxy.Split(':')
occoultProxy.Split(':','.')
一样使用任意数量的参数来调用它。是的,在发布后意识到了这一点。我想我应该先运行代码。不过,我一直使用新的[]{''}语法。