c# 获取网页中指定字符串的代码一例

发布时间:2020-03-23编辑:脚本学堂
介绍一个用C#获取网页中指定字符串的代码,主要是C#中indexof方法的应用,有需要的朋友,可以参考下。

代码如下:
 

复制代码 代码示例:

private void button2_Click(object sender, EventArgs e)
{
// Create a request for the URL.
WebRequest request = WebRequest.Create("http://www.jb200.com/");
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Display the status.
MessageBox.Show(response.StatusDescription);
Console.WriteLine(response.StatusDescription);
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream, Encoding.Default);
// Read the content.
string responseFromServer = reader.ReadToEnd();
//截取数据
int i = responseFromServer.IndexOf("京");
string dataBid = responseFromServer.Substring(i, 12);

// Display the content.
MessageBox.Show(dataBid);
Console.WriteLine(responseFromServer);
// Cleanup the streams and the response.
reader.Close();
dataStream.Close();
response.Close();
}