c# 获取网页中指定的字符串信息的实例代码
c# 获取网页中指定的字符串信息的实例代码
发布时间:2016-12-28 来源:查字典编辑
摘要:复制代码代码如下:privatevoidbutton2_Click(objectsender,EventArgse){//Createare...

复制代码 代码如下:

private void button2_Click(object sender, EventArgs e)

{

// Create a request for the URL.

WebRequest request = WebRequest.Create("http://www.baidu.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();

}

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新C#教程学习
热门C#教程学习
编程开发子分类