PDA

View Full Version : bảng dự báo thời tiết cập nhật tự động ??



thocon
13-04-2006, 11:13
Có thể lấy bảng dự báo thời tiết cập nhật tự động để đưa vào website được ko mấy bác? hướng dẫn tui với!!!

nguoivodanh1003
19-04-2006, 09:56
Cách làm như sau:
- Read 1 trang có thông tin dự báo thời tiết như yahoo weather, copy all html code của nó.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;

private void Write2File()
{

StreamWriter sw = new StreamWriter(@"C:\test.txt");

// Prepare web request...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(@"http://weather.yahoo.com/forecast/VMXX0007.html");
// We use POST ( we can also use GET )
myRequest.Method = "POST";

// Set the content type to a FORM
myRequest.ContentType ="application/x-www-form-urlencoded";

// Get request stream
Stream newStream = myRequest.GetRequestStream();

// Close stream
newStream.Close();


// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
HttpWebResponse myHttpWebResponse= (HttpWebResponse)myRequest.GetResponse();

// Display the contents of the page to the console.
Stream streamResponse=myHttpWebResponse.GetResponseStream ();

// Get stream object
StreamReader streamRead = new StreamReader( streamResponse );

Char[] readBuffer = new Char[256];

// Read from buffer
int count = streamRead.Read( readBuffer, 0, 256 );

while (count > 0)
{
// get string
String resultData = new String( readBuffer, 0, count);

// Write the data
sw.WriteLine(resultData);

// Read from buffer
count = streamRead.Read( readBuffer, 0, 256);
}

// Release the response object resources.
streamRead.Close();
streamResponse.Close();
sw.Close();

// Close response
myHttpWebResponse.Close();
}
- Tìm thông tin mình cần như là nhiệt độ, ..., sau đó tìm cách trim no ra.
private string GetTemp()
{
string strReturn = "";

//read the file
StreamReader sr = new StreamReader(@"C:\test.txt");
string s = sr.ReadToEnd();
sr.Close();

//look for value
int i = s.indexof("Currently:");
if (i >= 0)
{
i = s.indexof(@"<b>", i);
if (i >= 0)
{
j = s.indexof(@"</b>", i);
if (j >= 0)
{
strReturn = s.substring(i + 3, j - i - 3);
}
}
}

return strReturn;
}
- Sau đó viết cái form co timer, định kỳ read the website and trim infor vá díplay in the form.