【问题标题】:How to split a proxy in C#如何在 C# 中拆分代理
【发布时间】:2017-07-30 14:03:34
【问题描述】:

我正在尝试拆分格式为“HOST:PORT”的代理,然后继续在请求中使用该端口。因此我需要一个字符串和一个int。我试过这个:

string text = ProxyList.ToString();
string[] array = text.Split(':');
string host = array[0].ToString();
int portParse = Int32.Parse(array[1]);
this.Checker(host, portParse);
string text2 = ProxyList[i];
this.Checker(host, portParse);

我不断收到此错误:System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

在这一行:`int portParse = Int32.Parse(array[1]);

感谢布鲁诺 LM

【问题讨论】:

  • ProxyList变量的类型和内容是什么?
  • 在调试器中运行代码。看看值是多少。我猜text 不包含您认为它包含的内容。
  • 代理列表又名文本包含 4 个代理
  • 67.207.95.138:8080 98.172.91.132:8080 67.207.95.138:8080 98.172.91.131:8080 1 个重复和 1 个假代理。
  • text的内容是什么?使用调试器查看或将其输出到控制台。变量中真的有 : 吗?

标签: c#


【解决方案1】:

如果ProxyList 是您提到的文本,那么我认为它可以这样声明:

string ProxyList = "67.207.95.138:8080 98.172.91.132:8080 67.207.95.138:8080 98.172.91.131:8080";

通过 string[] array = text.Split(':'); 你得到

8080 98.172.91.132

如果你只想要一个端口,那么你需要先按空格分割。

string ProxyList = "67.207.95.138:8080 98.172.91.132:8080 67.207.95.138:8080 98.172.91.131:8080";

// split by space
string[] proxies = ProxyList.Split(' ');

// get first host:port 67.207.95.138:8080
string text = proxies[0];

// get the port 8080 (split of the first entry)
string[] array = text.Split(':');

string host = array[0].ToString();
int portParse = Int32.Parse(array[1]); // 8080

你可以用每个代理执行某事是这样的:

string input = "67.207.95.138:8080 98.172.91.132:8080 67.207.95.138:8080 98.172.91.131:8080";

var proxies = input.Split(' ')
    .Select(ip => new Uri($"http://{ip}"))
    .Distinct(); // remove duplicates

foreach (var proxy in proxies)
{
    var ip = proxy.Host;
    var port = proxy.Port;

    // do something
}

【讨论】:

  • 我想解析列表框中的所有代理。但是当我尝试时,我继续收到此错误。 System.IndexOutOfRangeException: '索引超出了数组的范围。'
猜你喜欢
  • 2014-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多