2012-04-04 88 views
3

我是新來的asp.net,在ASP.NET中從URL獲取數據

我想從asp.net上的url獲取數據。 &需要將數據存儲到字符串中。

如果想這是我的網址THN我想獲取字符串這個URL數據,

http://www.islamicfinder.org/prayer_service.php?country=bahrain&city=manama&state=02&zipcode=&latitude=26.2361&longitude=50.5831&timezone=3.00&HanfiShafi=1&pmethod=4&fajrTwilight1=&fajrTwilight2=&ishaTwilight=0&ishaInterval=0&dhuhrInterval=1&maghribInterval=1&dayLight=0&simpleFormat=xml 

回答

5

試試這個

string url = "http://www.islamicfinder.org/prayer_service.php?country=bahrain&city=manama&state=02&zipcode=&latitude=26.2361&longitude=50.5831&timezone=3.00&HanfiShafi=1&pmethod=4&fajrTwilight1=&fajrTwilight2=&ishaTwilight=0&ishaInterval=0&dhuhrInterval=1&maghribInterval=1&dayLight=0&simpleFormat=xml"; 
      var webClient = new WebClient(); 
      string data = webClient.DownloadString(url); 
+0

我想將這些數據存儲到我的表中,我應該怎麼做? – Krunal 2012-04-04 08:28:06

+0

創建此表並存儲使用它的實體框架 – 2012-04-04 08:30:33

+1

@krunal它取決於很多事情。你使用的是哪個數據庫?什麼數據訪問技術? – scartag 2012-04-04 08:30:49

2

WebClient是這種事情有用(scartag的回答表明了這是簡單的),但對於更高級的選項,您應該看下面的WebRequest類:

// Create a request for the URL.   
WebRequest request = WebRequest.Create ("http://www.contoso.com/default.html"); 

// If required by the server, set the credentials. 
request.Credentials = CredentialCache.DefaultCredentials; 

// Get the response. 
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

// Display the status. 
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); 

// Read the content. 
string responseFromServer = reader.ReadToEnd(); 

// Display the content. 
Console.WriteLine (responseFromServer); 

// Cleanup the streams and the response. 
reader.Close(); 
dataStream.Close(); 
response.Close();