public string GetHttpResponse(string url, int timeout = 60000)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
request.UserAgent = null;
request.Timeout = timeout;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
//通过JavaScriptSerializer获取json的单个值
JavaScriptSerializer Jss = new JavaScriptSerializer();
Dictionary<string, object> DicText = (Dictionary<string, object>)Jss.DeserializeObject(retString);
string city = DicText["city"].ToString();
string tip = DicText["air_tips"].ToString();
this.tianqi.Text = city + tip;
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string tqid = this.tqid.Text;
string miyao = "u03CST0U";
string url = "https://v0.yiketianqi.com/api?unescape=1&version=v61&appid=" + tqid + "&appsecret=" + miyao;
int timeout = 60000;
GetHttpResponse(url, timeout);
}
© 版权声明
THE END
暂无评论内容