基本上我将一个随机行从文本框分成4个部分:代理,代理ip,如果用户提供了用户名和密码,那么它们就成了第3和第4部分。
如果用户没有提供代理凭证,并且只提供了代理IP和端口,我不希望使用代理凭据。 这是我的代码:
If SwitchButton2.Value = True Then randomline = RandomNumbers.Next(proxybox.Lines.Length) Dim s As String = proxybox.Lines(randomline) RandomProxy = Split(s, ":", , CompareMethod.Text) request.Proxy = New WebProxy(RandomProxy(0).ToString & ":" & RandomProxy(1).ToString) Try If RandomProxy(2) And RandomProxy(3) IsNot Nothing Then request.UseDefaultCredentials = False request.Proxy.Credentials = New System.Net.NetworkCredential(RandomProxy(2).ToString, RandomProxy(3).ToString) End If Catch MessageBox.Show("no credentials") End Try End If我在这里的If语句有问题:
Try If RandomProxy(2) And RandomProxy(3) IsNot Nothing Then request.UseDefaultCredentials = False request.Proxy.Credentials = New System.Net.NetworkCredential(RandomProxy(2).ToString, RandomProxy(3).ToString) End If Catch MessageBox.Show("no credentials") End Try我每次都会抛出异常,我做错了什么? 我只希望在提供代理凭证时使用它们,如果没有则继续使用。
Basically I am splitting up a random line from a text box into 4 parts: a proxy, the proxy ip, and if the user has supplied a username and password those become parts 3 and 4.
I don't want the proxy credentials to be used if the user has not supplied them, and has only supplied the proxy ip and port. Here is my code:
If SwitchButton2.Value = True Then randomline = RandomNumbers.Next(proxybox.Lines.Length) Dim s As String = proxybox.Lines(randomline) RandomProxy = Split(s, ":", , CompareMethod.Text) request.Proxy = New WebProxy(RandomProxy(0).ToString & ":" & RandomProxy(1).ToString) Try If RandomProxy(2) And RandomProxy(3) IsNot Nothing Then request.UseDefaultCredentials = False request.Proxy.Credentials = New System.Net.NetworkCredential(RandomProxy(2).ToString, RandomProxy(3).ToString) End If Catch MessageBox.Show("no credentials") End Try End IfI am having trouble with the If statment here:
Try If RandomProxy(2) And RandomProxy(3) IsNot Nothing Then request.UseDefaultCredentials = False request.Proxy.Credentials = New System.Net.NetworkCredential(RandomProxy(2).ToString, RandomProxy(3).ToString) End If Catch MessageBox.Show("no credentials") End TryI am getting the exception thrown everytime, what am i doing wrong with it? I only want the proxy credentials to be used if they are supplied and continue on if not.
最满意答案
您需要分别测试每个值:
If RandomProxy(2) IsNot Nothing AndAlso RandomProxy(3) IsNot Nothing Then您还应该在尝试使用它们之前测试数组中是否存在足够的元素:
If RandomProxy IsNot Nothing AndAlso RandomProxy.Length >= 4 AndAlso RandomProxy(2) IsNot Nothing AndAlso RandomProxy(3) IsNot Nothing Then最后
You need to test each of the values separately:
If RandomProxy(2) IsNot Nothing AndAlso RandomProxy(3) IsNot Nothing ThenYou should also test to see if enough elements exist in the array prior to attempting to use them:
If RandomProxy IsNot Nothing AndAlso RandomProxy.Length >= 4 AndAlso RandomProxy(2) IsNot Nothing AndAlso RandomProxy(3) IsNot Nothing Thenand finally
更多推荐
发布评论