我遇到基本代码问题:
var objReader = new StreamReader(ofDialog.FileName);
while (objReader.Peek() >= 0)
{
Helpers.returnMessage(objReader.ReadLine());
// trim the url to root
var uri = new Uri(objReader.ReadLine());
var host = uri.Host;
}
我正在加载.txt文件的网址,尝试使用uri.host修剪为root但我收到错误:
值不能为null。参数名称:UriString
但如果我硬编码一个网址: var uri = new Uri(“https://stackoverflow.com/questions/ask”);
它工作正常,似乎是我从.txt文件加载。
任何帮助将不胜感激。
完整代码:
private void btnInput_Click(object sender, EventArgs e)
{
// ofDialog settings
ofDialog.Filter = @"TXT Files|*.txt";
ofDialog.Title = @"Select your source backlink file...";
ofDialog.FileName = "URLs.txt";
// is cancel pressed?
if (ofDialog.ShowDialog() == DialogResult.Cancel)
return;
try
{
var objReader = new StreamReader(ofDialog.FileName);
while (objReader.Peek() >= 0)
{
//Helpers.returnMessage(objReader.ReadLine());
// trim the url to root
var x = objReader.ReadLine();
Helpers.returnMessage(x);
// trim the url to root
var uri = new Uri(x);
var host = uri.Host;
//Helpers.returnMessage(host);
// extract urls here
var wc = new WebClient();
var html = wc.DownloadString(objReader.ReadLine());
// 1. Find all matches in file
var m1 = Regex.Matches(html, @"(<a.*?>.*?</a>)",
RegexOptions.Singleline);
// 2. Loop over each match
foreach (Match m in m1)
{
var value = m.Groups[1].Value;
string href;
// 3. Get href attribute
var m2 = Regex.Match(value, @"href=\""(.*?)\""",
RegexOptions.Singleline);
if (m2.Success)
{
href = m2.Groups[1].Value;
}
else
{
continue;
}
// add to the results
if (href.StartsWith("http"))
{
//Helpers.returnMessage(href);
if (!href.Contains(host))
{
// add urls to the listview
var lvi = new ListViewItem(href);
listViewMain.Items.Add(lvi);
}
}
}
}
}
catch (Exception ex)
{
Helpers.returnMessage(ex.Message);
}
}
returnMessage()字面上只返回一个消息框弹出窗口。
答案 0 :(得分:1)
试试这个:
var objReader = new StreamReader(ofDialog.FileName);
while (objReader.Peek() >= 0)
{
string x = objReader.ReadLine();
Helpers.returnMessage(x);
// trim the url to root
var uri = new Uri(x);
var host = uri.Host;
}