c#HttpResponseMessage引发异常HttpRequestException:... ---> WebException:无法解析远程名称:...的"www.thexlr.com" [英] c# HttpResponseMessage throws exception HttpRequestException: ... ---> WebException: The remote name could not be resolved: 'www.thexlr.com' at ...
问题描述
我不愿意构建爬网程序,以将列表中有问题(重定向,不喜欢,网站已关闭...)的网站从列表中分离出来.我添加了reachtextbox来存储URL列表,并添加了另一个RTB以显示URL-S的HTML代码.我需要重写一些属于的信息 来解决url-s问题,并在列表中跳过它以抓取其他url-s.一些网站将Exception作为爱人抛出,并停止了繁琐的过程:
I tires to build crawler that separate websites with problems as (redirected, not fond, website is down ...) from list. I added reachtextbox to store url list and another rtb to display html code for url-s. I need to rewrite some informations that belong to problem url-s and skip it in list to crawl other url-s. Some websites throws Exception as belove and stops crawelling process:
System.Net.Http.HttpRequestException:发送请求时出错. ---> System.Net.WebException:无法解析远程名称:HttpClientHandler.GetResponseCallback(IAsyncResult)的HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)处的"www.thexlr.com" ar)...
System.Net.Http.HttpRequestException: An error accurred while sending the request. ---> System.Net.WebException: The remote name could not be resolved: 'www.thexlr.com' at HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at HttpClientHandler.GetResponseCallback(IAsyncResult ar) ...
我不知道如何处理有问题的网站.有人有什么主意吗?
I do not know how to deal with problem websites. Do someone have any idea?
我的代码:
HttpClient clientKontrKat;
HttpResponseMessage responseKontrKat;
#region start button
public async void buttonSpustitKontroluStranok_Click(object sender, EventArgs e)
{
richTextBoxKontrolaUrlWebObsah.Text = "";
richTextBoxKontrolaUrlVnutorneLinky.Text = "";
StringBuilder sb = new StringBuilder();
if (dataGridView1.Rows.Count > -1)
{
if (categoryComboBox3.Items.Count > -1 && categoryComboBox3.SelectedItem != null && statusComboBox1.Items.Count > -1 && statusComboBox1.SelectedItem != null)
{
string categItemCoPrehliadnut = categoryComboBox3.SelectedValue.ToString();
string statusItemCoOznacit = statusComboBox1.SelectedValue.ToString();
foreach (DataGridViewRow item in dataGridView1.Rows)
{
string ColumnCategItemsCoPrehliadnut = item.Cells[2].Value.ToString();
if (categItemCoPrehliadnut == ColumnCategItemsCoPrehliadnut)
{
string urlCoPrehliadnut = item.Cells[0].Value.ToString();
sb.Append(urlCoPrehliadnut + Environment.NewLine);
}
}
}
}
richTextBoxKontrolaUrlVnutorneLinky.Text = sb.ToString();
richTextBoxKontrolaUrlVnutorneLinky.Text = string.Join(Environment.NewLine, richTextBoxKontrolaUrlVnutorneLinky.Lines.Distinct());
#region Crawler
int i = 0;
while (i < richTextBoxKontrolaUrlVnutorneLinky.Lines.Length - 1)
{
try
{
buttonSpustitKontroluStranok.Enabled = false;
string lineUrl = richTextBoxKontrolaUrlVnutorneLinky.Lines[i].ToString();
HttpClientHandler handler = new HttpClientHandler();
handler.AllowAutoRedirect = false;
clientKontrKat = new HttpClient(handler);
clientKontrKat.MaxResponseContentBufferSize = 256000;
clientKontrKat.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
responseKontrKat = await clientKontrKat.GetAsync(lineUrl);
//responseKontrKat.EnsureSuccessStatusCode();
string responseBody = await responseKontrKat.Content.ReadAsStringAsync();
// Above three lines can be replaced with new helper method in following line
// string body = await client.GetStringAsync(uri);
labelKontrolaWebRespPopis.Text = "Web response popis: " + responseKontrKat.StatusCode;
if (responseKontrKat.IsSuccessStatusCode)
{
richTextBoxKontrolaUrlWebObsah.Text = "";
richTextBoxKontrolaUrlWebObsah.Text = responseBody;
}
else
{
// problems handling here
//if (richTextBoxKontrolaUrlVnutorneLinky.Lines.Contains(lineUrl))
//{
//MessageBox.Show("stop, " + lineUrl + " i: " + i);
richTextBoxUrlsToCrawl.SelectionStart = i;
richTextBoxUrlsToCrawl.SelectionLength = richTextBoxUrlsToCrawl.Text.IndexOf("\n", 0) + 1;
richTextBoxUrlsToCrawl.SelectedText = "";
//}
}
i++;
int pocetLiniekNaPrehliadnutie = richTextBoxKontrolaUrlVnutorneLinky.Lines.Count() - 1;
labelPocetStranok.Text = "Pocet prehliadnutych stranok:" + i + " z " + pocetLiniekNaPrehliadnutie;
// // MessageBox.Show("stop, " + lineUrl);
// }
//}
}
catch (WebException ex)
{
//if (ex.Status == WebExceptionStatus.ProtocolError)
//{
// if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.NotFound)
// {
// // handle the 404 here
// }
//}
//else
if (ex.Status == WebExceptionStatus.NameResolutionFailure)
{
// handle name resolution failure
MessageBox.Show("stop, ");
}
}
catch (Exception ExKontStranok)
{
//if (responseKontrKat.IsSuccessStatusCode)
//{
// return;
//}
//if (responseKontrKat.StatusCode == HttpStatusCode.NotFound)
//if (ExKontStranok.GetBaseException().InnerException(WebException )==WebExceptionStatus .NameResolutionFailure)
//{
MessageBox.Show("stop, ");
richTextBoxUrlsToCrawl.SelectionStart = i;
richTextBoxUrlsToCrawl.SelectionLength = richTextBoxUrlsToCrawl.Text.IndexOf("\n", 0) + 1;
richTextBoxUrlsToCrawl.SelectedText = "";
//}
MessageBox.Show("chyba:\n" + ExKontStranok.ToString());
}
finally
{
buttonSpustitKontroluStranok.Enabled = true;
}
}
#endregion
MessageBox.Show("Urobene!");
}
#endregion
#region stop button
private void buttonZastavitKontroluStranok_Click(object sender, EventArgs e)
{
clientKontrKat.CancelPendingRequests();
responseKontrKat.Dispose();
}
#endregion
感谢您的帮助.
推荐答案
Jurajm,
Hi Jurajm,
您的问题听起来与WinForm general无关,所以我将其移至此处.您将获得有关可以在哪里发布的一些建议.
Your issue sounds not related to WinForm general so I moved it here. You will get some advice about where you can post it.
此致
这篇关于c#HttpResponseMessage引发异常HttpRequestException:... ---> WebException:无法解析远程名称:...的"www.thexlr.com"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!