user3107806
user3107806

Reputation: 29

Index was outside the bounds of the array error

I'm currently making proxy checker with my input Textbox. I'm getting C# error:Index was outside the bounds of the array from proxy(123.34.123.45:8080).

My codes will be...

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)

UPDATED

I had tried another codes, but still have error code. Please help.

 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]));

Output

123.34.345.23 <!-- Some Proxy here, seems good here -->
IndexOutOfRangeException was unhandled( Index was outside the bounds of the array.)

Upvotes: 0

Views: 901

Answers (3)

selametonur
selametonur

Reputation: 132

If you are trying to connect local proxy with your local ip,please check if occoultProxy's value is "::1".

Upvotes: 0

Alex K.
Alex K.

Reputation: 175956

Your input must not have a port segment, you can deal with this:

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

Upvotes: 5

bas
bas

Reputation: 14982

Would this work for you?

        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();
        }

Upvotes: 1

Related Questions