我想从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>";
}
答案 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>";
}