我写的一个webservice,就是调
http://appnews.qq.com/cgi-bin/news_qq_search?city=cityname
![](/Images/OutliningIndicators/ContractedBlock.gif)
[OK]GetWeatherDataSet天气预报#region[OK]GetWeatherDataSet天气预报
![](/Images/OutliningIndicators/InBlock.gif)
[WebMethod(Description="天气预报")]
![](/Images/OutliningIndicators/InBlock.gif)
public DataSet GetWeatherDataSet(string cityName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
string url=@"http://appnews.qq.com/cgi-bin/news_qq_search";
string weatherData="";
![](/Images/OutliningIndicators/InBlock.gif)
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
weatherData=GetPage(url,cityName).Replace(" ","").Trim();
}
catch(Exception)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
throw new Exception("对不起,没有这个城市的天气信息!");
}
![](/Images/OutliningIndicators/InBlock.gif)
//System.Diagnostics.Trace.WriteLine(tommorow);
//System.Diagnostics.Trace.WriteLine(weatherData);
![](/Images/OutliningIndicators/InBlock.gif)
WeatherDataSet weatherDs=new WeatherDataSet();
weatherDs.WeatherDS.AddWeatherDSRow(GetWeatherRow(ref
weatherDs,weatherData,cityName));
return weatherDs;
}
![](/Images/OutliningIndicators/InBlock.gif)
private WeatherDataSet.WeatherDSRow GetWeatherRow(ref WeatherDataSet
weatherDs,string weatherData,string cityName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
WeatherDataSet.WeatherDSRow
weatherRow=weatherDs.WeatherDS.NewWeatherDSRow();
weatherRow.CityName=weatherData.Substring(weatherData.IndexOf("●")+1,cityName.Length);
![](/Images/OutliningIndicators/InBlock.gif)
weatherRow.Date1=DateTime.Now.ToLongDateString();
![](/Images/OutliningIndicators/InBlock.gif)
weatherRow.Weather1=weatherData.Substring(weatherData.IndexOf("天气")+"天气".Length,weatherData.IndexOf("气温")-(weatherData.IndexOf("天气")+"天气".Length));
![](/Images/OutliningIndicators/InBlock.gif)
weatherRow.Temp1=weatherData.Substring(weatherData.IndexOf("气温")+"气温".Length,weatherData.IndexOf("风力")-(weatherData.IndexOf("气温")+"气温".Length)).Replace("℃-","℃/");
![](/Images/OutliningIndicators/InBlock.gif)
weatherRow.WindPower1=weatherData.Substring(weatherData.IndexOf("风力")+"风力".Length,weatherData.IndexOf(tommorow)-(weatherData.IndexOf("风力")+"风力".Length));
![](/Images/OutliningIndicators/InBlock.gif)
weatherRow.Date2=DateTime.Today.AddDays(1).ToLongDateString();
![](/Images/OutliningIndicators/InBlock.gif)
weatherRow.Weather2=weatherData.Substring(weatherData.LastIndexOf("天气")+"天气".Length,weatherData.LastIndexOf("气温")-(weatherData.LastIndexOf("天气")+"天气".Length));
![](/Images/OutliningIndicators/InBlock.gif)
weatherRow.Temp2=weatherData.Substring(weatherData.LastIndexOf("气温")+"气温".Length,weatherData.LastIndexOf("风力")-(weatherData.LastIndexOf("气温")+"气温".Length)).Replace("℃-","℃/");
![](/Images/OutliningIndicators/InBlock.gif)
weatherRow.WindPower2=weatherData.Substring(weatherData.LastIndexOf("风力")+"风力".Length);
![](/Images/OutliningIndicators/InBlock.gif)
return weatherRow;
}
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
GetPageString获取QQ的天气服务#region GetPageString获取QQ的天气服务
![](/Images/OutliningIndicators/InBlock.gif)
//privatestringxx="";
![](/Images/OutliningIndicators/InBlock.gif)
[WebMethod(Description="天气预报")]
![](/Images/OutliningIndicators/InBlock.gif)
public string GetPageString(string cityName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
string url=@"http://appnews.qq.com/cgi-bin/news_qq_search";
return GetPage(url,cityName);
}
![](/Images/OutliningIndicators/InBlock.gif)
private static string GetPage(string url,string cityName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
HttpWebResponse res=null;
string strResult="";
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
string
postData="city="+HttpUtility.UrlEncode(cityName,System.Text.Encoding.GetEncoding("GB2312"));
HttpWebRequest req=(HttpWebRequest)WebRequest.Create(url);
req.Method="POST";
req.KeepAlive=true;
req.ContentType="application/x-www-form-urlencoded";
StringBuilder UrlEncoded=new StringBuilder();
byte[]SomeBytes=Encoding.ASCII.GetBytes(postData);
req.ContentLength=SomeBytes.Length;
Stream newStream=req.GetRequestStream();
newStream.Write(SomeBytes,0,SomeBytes.Length);
newStream.Close();
![](/Images/OutliningIndicators/InBlock.gif)
//获得流内容
![](/Images/OutliningIndicators/InBlock.gif)
res=(HttpWebResponse)req.GetResponse();
System.IO.Stream s=res.GetResponseStream();
StreamReader reader=new StreamReader(s,System.Text.Encoding.Default);
strResult=reader.ReadToEnd();
}
catch(Exception e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
strResult=e.ToString();
}
finally
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(res!=null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
res.Close();
}
}
![](/Images/OutliningIndicators/InBlock.gif)
strResult=strResult.Remove(0,strResult.IndexOf("●"));
![](/Images/OutliningIndicators/InBlock.gif)
if(cityName!="北京")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
strResult=strResult.Remove(strResult.IndexOf("北京"),strResult.Length-strResult.IndexOf("北京"));
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
strResult=strResult.Remove(strResult.LastIndexOf("北京"),strResult.Length-strResult.LastIndexOf("北京"));
}
![](/Images/OutliningIndicators/InBlock.gif)
strResult=strResult.Trim();
while(strResult.IndexOf(@"<")!=-1)//<>
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
strResult=strResult.Remove(strResult.IndexOf(@"<"),strResult.IndexOf(@">")-strResult.IndexOf(@"<")+1);
}
![](/Images/OutliningIndicators/InBlock.gif)
while(strResult.IndexOf(@" ")!=-1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
strResult=strResult.Replace(" ","");
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string x=Encoding.UTF8.GetString(new Byte[]
{10});
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string y=Encoding.UTF8.GetString(new Byte[]
{9});
![](/Images/OutliningIndicators/InBlock.gif)
while(strResult.IndexOf(x)!=-1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
strResult=strResult.Replace(x,"");
}
![](/Images/OutliningIndicators/InBlock.gif)
while(strResult.IndexOf(y)!=-1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
strResult=strResult.Replace(y,"");
}
return strResult;
}
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/None.gif)
--
被最牛的软件公司牵着鼻子走是我们这些牛的荣幸
移动开发,美丽新世界。今天你还在做传统开发么?
╭╮︵︵╭╮.╔═════════╗.
〔 ∩∩ 〕.‖╭使╮╭名╮╭扬╮‖.
〔ミ e ミ〕.‖.╲╱..╲╱..╲╱.‖.
(︵灬 灬︵〕╚═════════╝.
真实姓名:童振华
"Tom猫" <Tom@discussions.microsoft.com> 写入消息
news:9673E89B-566D-4CE9-86D3-DDD78E5A0862@microsoft.com...
> 我想在页面上放个实时的天气预报,就是从某个大网站取得天气信息然后放在自己的页面显示,那位大虾能给个原程序或者指点一下,不胜感激