asp.net 讀取 豆瓣信息
1.讀取方法
public static string SetUrlToXMLToFile(string url) {
var strXml = "";
var m_uri = new Uri(url);
HttpWebRequest rqst = (HttpWebRequest)WebRequest.Create(m_uri);
rqst.AllowAutoRedirect = true; rqst.ContentType = "xml";
rqst.MaximumAutomaticRedirections = 3;
rqst.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
rqst.KeepAlive = true; rqst.Timeout = 100000;
rqst.Method = "GET";
HttpWebResponse rsps = (HttpWebResponse)rqst.GetResponse();
Stream sm = rsps.GetResponseStream();
Encoding cding = System.Text.Encoding.Default;
if (sm.CanRead) { strXml = new StreamReader(sm, cding).ReadToEnd(); }
return strXml; }
2.返回dataset
public static DataSet CXmlToDataSet(string strXml) {
if (!string.IsNullOrEmpty(strXml))
{ try { DataSet ds = new DataSet();
ds.ReadXml(strXml); return ds; }
catch (Exception e) { throw e; }
finally { //释放资源 } } else { return null; } }
3.綁定數據
System.Data.DataSet oauthentry = HelpOauth.CXmlToDataSet(strXml);
4.代碼調用 string strXml = "http://api.douban.com/movie/subject/1985677";
strXml = HelpOauth.SetUrlToXMLToFile(strXml);
System.Data.DataSet oauthentry = HelpOauth.CXmlToDataSet(strXml); this.GridView1.DataSource = ds.Tables[0]; this.GridView1.DataBind();