1)利用Ajax获取天气预报
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
var xmlhttp;
var url="http://php.weather.sina.com.cn/search.php?city=%E6%B7%B1%E5%9C%B3";
function createXmlHttp()
{
if(window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
}
function GetWeartherData()
{
createXmlHttp();
xmlhttp.open("GET",url,true);
xmlhttp.onreadystatechange=changeState
xmlhttp.send(null);
}
function changeState()
{
if(xmlhttp.readystate==4&&xmlhttp.status==200)
{
//var data=bytes2BSTR(xmlhttp.responseBody);
// var start=data.indexOf("天气状况 begin")+15;
// var end=data.indexOf("天气状况 end");
// var weartherData=data.substring(start,end);
//document.getElementById("wearther").innerHTML=weartherData;
var data = bytes2BSTR(xmlhttp.responseBody);
var start = data.indexOf("天气状况 begin") + 15;
var end = data.indexOf("天气状况 end");
var weartherData = data.substring(start, end);
document.getElementById("wearther").innerHTML = weartherData;
var obj = document.getElementById("Weather3DBlk");
var objarr = obj.childNodes;
var todayobj = objarr[0];
var arrDiv = todayobj.getElementsByTagName("div");
todayobj.removeChild(arrDiv[arrDiv.length - 1]);
document.getElementById("wearther").innerHTML = todayobj.innerHTML;
}
}
</script>
<script language="vbscript" type="text/vbscript">
function bytes2BSTR(vIn)
dim strReturn,i,ThisCharCode,innerCode,Hight8,Low8,NextCharCode
strReturn=""
for i=1 to LenB(vIn)
ThisCharCode=AscB(MidB(vIn,i,1))
if ThisCharCode<&H80 Then
strReturn=strReturn & Chr(ThisCharCode)
else
NextCharCode=AscB(MidB(vIn,i+1,1))
strReturn=strReturn&Chr(CLng(ThisCharCode)*&H100+CInt(NextCharCode))
i=i+1
end if
next
bytes2BSTR=strReturn
end function
</script>
</head>
<body οnlοad="GetWeartherData();">
<form id="form1" runat="server">
<div id="wearther">
<asp:TextBox ID="TextBox2" runat="server" Text=""></asp:TextBox>
</div>
</form>
</body>
2)后台获取天气预报
WebClient client = new WebClient();
Stream response = client.OpenRead("要获取的地址");
StreamReader sr = new StreamReader(response, System.Text.Encoding.Default);
string content = string.Empty;
TextBox2.Text = sr.ReadToEnd(); //textbox2为接收的元素