【发布时间】:2015-09-20 01:08:12
【问题描述】:
using System;
using System.IO;
using System.Net;
using System.Text;
/// <summary>
/// Fetches a Web Page
/// </summary>
class WebFetch
{
static void Main(string[] args)
{
// used to build entire input
StringBuilder sb = new StringBuilder();
// used on each read operation
byte[] buf = new byte[8192];
// prepare the web page we will be asking for
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create("http://www.nbcwashington.com/weather/school-closings/");
// execute the request
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();
// we will read data via the response stream
Stream resStream = response.GetResponseStream();
string tempString = null;
int count = 0;
do
{
// fill the buffer with data
count = resStream.Read(buf, 0, buf.Length);
// make sure we read some data
if (count != 0)
{
// translate from bytes to ASCII text
tempString = Encoding.ASCII.GetString(buf, 0, count);
// continue building the string
sb.Append(tempString);
}
}
while (count > 0); // any more data to read?
// print out page source
Console.WriteLine(sb.ToString());
Console.Clear ();
string output = sb.ToString();
string searchForThis = "Open";
int firstCharacter = output.IndexOf(searchForThis);
Console.WriteLine(output.IndexOf(searchForThis));
Console.ReadLine();
}
}
我正在编写一些将访问网站的代码,获取一些文本,然后将其粘贴到控制台 vie HttpWebResponse 中,但是当我使用 IndexOf() 时,它会返回 5 个数字,例如“22452”。是的,我知道这段代码可能很草率,但我才刚刚开始。发生这种情况的任何原因?
【问题讨论】:
标签: c# console httpwebrequest httpwebresponse