似乎无法获得这个“if

编程入门 行业动态 更新时间:2024-10-24 19:23:15
似乎无法获得这个“if-statement”的权利。(Can't seem to get this “if-statement” right. Vb.net)

基本上我将一个随机行从文本框分成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 If

I 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 Try

I 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 Then

You 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 Then

and finally

更多推荐

本文发布于:2023-07-05 08:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1035474.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!