C#页面截取函数

来源:互联网 发布:如何抓取app数据 编辑:程序博客网 时间:2024/05/29 14:49

    protected string GetPage(string Url,string Parameter,string StartTxt,string EndTxt)
    {
        HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create(Url);
        oRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
        string strPost = Parameter; ;
        byte[] somebyte = Encoding.Default.GetBytes(strPost);
        oRequest.Method = "POST";
        oRequest.ContentLength = somebyte.Length;
        oRequest.ContentType = "application/x-www-form-urlencoded";

        StreamWriter oStreamWriter = new StreamWriter(oRequest.GetRequestStream(), System.Text.Encoding.GetEncoding("GB2312"));
        oStreamWriter.Write(strPost);
        oStreamWriter.Close();
        HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse();

        StreamReader sr = new StreamReader(oResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));
        string sResultContents = sr.ReadToEnd();
        sr.Close();
        oResponse.Close();

        //截取所需要的部分
        int start, stop;
        start = sResultContents.IndexOf(StartTxt);
        stop = sResultContents.IndexOf(EndTxt, start) + EndTxt.Length;
        string temp = sResultContents.Substring(start, stop - start);
        return temp;
    }