3

我是 asp.net 的新手,

我想从 asp.net 上的 url 获取数据。& 需要将数据存储到字符串中。

如果假设这是我的 URL,那么我想在字符串中获取这个 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
4

2 回答 2

6

尝试这个

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);
于 2012-04-04T08:22:14.283 回答
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 ();
于 2012-04-04T08:23:06.840 回答