如何从网页获取数据?

时间:2010-07-28 15:44:53

标签: c# asp.net httpwebrequest xmlhttprequest

我想从this页面获取数据并将其插入到我的mssql数据库中。如何用asp.net c#读取这些数据? SehisID是1到81之间的值。

修改 我的代码如下。

for (int i = 1; i <= 81; i++)
{
    HttpWebRequest rqst = (HttpWebRequest)WebRequest.Create("http://www.milliyet.com.tr/Secim2009/api/belediyelist.ashx?sehirid=" + i);
    rqst.Method = "POST";
    rqst.ContentType = "text/xml";
    rqst.ContentLength = 0;
    rqst.Timeout = 3000;

    HttpWebResponse rspns = (HttpWebResponse)rqst.GetResponse();
    form1.InnerHtml += rspns.ToString() + "<br>";
}

2 个答案:

答案 0 :(得分:9)

WebClient是从网页获取字符串的简便方法:

WebClient client = new WebClient();
String downloadedString = client.DownloadString("http://www.milliyet.com.tr/Secim2009/api/belediyelist.ashx?sehirid=81");

答案 1 :(得分:1)

下一个代码也很有效:

        for (int i = 1; i <= 81; i++)
        {
            var rqst = (HttpWebRequest)WebRequest.Create("http://www.milliyet.com.tr/Secim2009/api/belediyelist.ashx?sehirid=" + i);
            rqst.Method = "POST";
            rqst.ContentType = "text/xml";
            rqst.ContentLength = 0;
            rqst.Timeout = 3000;

            var rspns = (HttpWebResponse)rqst.GetResponse();
            var reader = new StreamReader(rspns.GetResponseStream());
            form1.InnerHtml += reader.ReadToEnd() + "<br>";
        }