我正在尝试使用WebRequest.GetResponse()来请求网页;并将该响应转换为chararray,因此我可以对数组进行排序并获取页面上的任何HREF标记。问题是,在我的代码中,响应变为“System.Net.HttpWebRequest”,而不是应该从页面检索的HTML。
获取char数组的代码:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlTextBox.Text);
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.44 Safari/534.7";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
bytearray = encoding.GetBytes(Convert.ToString(response));
chararray = encoding.GetChars(bytearray);
搜索链接的代码(注释以进行故障排除):
for (int i = 0; i < chararray.Length; i++)
{
// Get all HREFs
if (i < 500 & chararray[i] == 'h' & chararray[i + 1] == 'r' & chararray[i + 2] == 'e' & chararray[i + 3] == '=' & chararray[i + 4] == '"')
{
for (int tempi = 0; bytearray[i + 4 + tempi] != '"';)
{
tempstring = tempstring + chararray[i + 4 + tempi].ToString();
}
urlarray[urlarray.Length + 1] = tempstring;
i = i + 4;
}
}
scrapeLink1.Text = urlarray[1];
如果我错过了某些内容,或者需要更多信息,请与我们联系。
答案 0 :(得分:1)
您必须先阅读 stream 的响应。
HttpWebRequest request = WebRequest.Create(urlTextBox.Text) as HttpWebRequest;
if (request != null)
{
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.44 Safari/534.7";
using (HttpWebRepsonse response = request.GetResponse() as HttpWebResponse)
using (StreamReader rdr = new StreamReader(response.GetResponseStream())
{
string result = rdr.ReadToEnd();
}
}