【问题标题】:HttpWebResponse returns with 5 numbers when searching through a string搜索字符串时,HttpWebResponse 返回 5 个数字
【发布时间】: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


    【解决方案1】:

    根据MSDNIndexOf方法:

    报告指定的第一次出现的从零开始的索引 本例中的字符串。

    返回值类型:System.Int32

    如果找到该字符串,则 value 的从零开始的索引位置,或 -1 如果不是。如果 value 为 String.Empty,则返回值为 0。

    所以这个22452Open在输出字符串变量中第一次出现的索引位置。

    另外,如果可以使用 StreamReader,为什么还要使用 Stream? Stream 是一个抽象类,没有具体实现其有用的方法。

    你可以用这个:

    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    
    
    /// <summary>
    /// Fetches a Web Page
    /// </summary>
    class WebFetch
    {
        static void Main(string[] args)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.nbcwashington.com/weather/school-closings/");
    
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
            Stream rawStream = response.GetResponseStream();
    
            string resOutput = String.Empty();
    
            using (StreamReader reader = new StreamReader(rawStream))
            {
                resOutput = reader.ReadToEnd();
            }
    
            Console.WriteLine(resOutput);
            Console.Clear();
    
            string searchForThis = "Open";
            int firstCharacter = resOutput.IndexOf(searchForThis);
            Console.WriteLine(resOutput.IndexOf(searchForThis))
            Console.ReadLine();
            response.Close();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 2014-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多