简单问题。网站接口我怎么调用? 例如给了我一个网站接口,http://m.weather.com.cn/data/101010100.html。显示北京的天气的。我想把它做成天气预报。怎么让软件一打开就自动登陆这个网站把数据包拿下来??软件 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 一般是斗webservice 或者api 吧,给你返回结果,你直接显示就可以了 这个就是网站给的api吧我就是想要将这个先弄到我的电脑里,然后将数据拿出来做个小的看天气的应用。能够稍微写清楚点吗?是调用webservice这个类的意思? webclient直接downstring,返回的是json串,找个解析json的类去处理,下载一个 Newtonsoft.Json.dll string reply = client.DownloadString (address);这边的address必须是IP地址吗? http://119.75.220.12这种ip也不行。。我只要能够显示网页上的数据到我的电脑里我就能处理了。只是我弄不下来。 MSDN上说的参数address包含要下载的 URI 的 String。如果 BaseAddress 属性不是空字符串 (""),且 address 不包含绝对 URI,则 address 必须是相对 URI,此 URI 与 BaseAddress 组合在一起构成所请求数据的绝对 URI。如果 QueryString 属性不是空字符串,则将它追加到 address。我这个 string result=web.DownloadString(http://m.weather.com.cn/data/101010100.html);就是绝对URI但是C#说我现在是http://这个:是无效的 用httpwebrequest获取源码,然后json解析结果就ok了 //天气信息来自 中国天气网 !//很早以前写过一段//**************************************************** protected void Page_Load(object sender, EventArgs e) { string str = GetResponseStr();//获得json天气信息 //对json格式的信息重新整理一下,因为我们要用.net自带的方法获得相关信息 str = str.Remove(str.LastIndexOf("}"), 1); str = str.Remove(0, 15); //使用JavaScriptSerializer对象来解析数据 JavaScriptSerializer serializer = new JavaScriptSerializer(); Dictionary<string, object> json = (Dictionary<string, object>)serializer.DeserializeObject(str); object str_city; //-- 城市 -- city object str_weather; //-- 天气 -- weather1 object str_img_id; //-- 天气图标编号 -- img1 object str_temp; //-- 温度 -- temp1 object str_fl; //-- 风力 --fl1 object str_UV; //-- 紫外线 -- index_uv object str_cy; //-- 穿衣指数 -- index object str_xc; //-- 洗车指数 -- index_xc object str_CO; //-- 舒适指数 -- index_co object str_Time; //-- 时间 -- date_y object str_week; //-- 星期 -- week if (json.TryGetValue("city", out str_city)) { Response.Write (str_city.ToString());//城市 } if (json.TryGetValue("weather1", out str_weather)) { //Response.Write(str_weather.ToString());//天气 lab_Tianqi.Text = str_weather.ToString(); } if (json.TryGetValue("temp1", out str_temp)) { //Response.Write(str_temp.ToString());//温度 lab_wendu.Text = str_temp.ToString(); } if (json.TryGetValue("fl1", out str_fl)) { //Response.Write(str_fl.ToString());//风力 lab_Fengli.Text = str_fl.ToString(); } if (json.TryGetValue("index_uv", out str_UV)) { //Response.Write(str_UV.ToString());//紫外线 lab_UV.Text = str_UV.ToString(); } //if (json.TryGetValue("index", out str_cy)) //{ // Response.Write(str_cy.ToString());//穿衣 //} if (json.TryGetValue("index_xc", out str_xc)) { //Response.Write(str_xc.ToString());//洗车 lab_Xc.Text = str_xc.ToString(); } if (json.TryGetValue("index_co", out str_CO)) { //Response.Write(str_CO.ToString());//舒适 lab_Co.Text = str_CO.ToString(); } if (json.TryGetValue("date_y", out str_Time)) { // Response.Write(str_Time.ToString());//时间 lab_Time.Text = str_Time.ToString(); } if (json.TryGetValue("week", out str_week)) { //Response.Write(str_week.ToString());//星期 lab_Week.Text = str_week.ToString(); } if (json.TryGetValue("img1", out str_img_id)) { //Response.Write(str_img_id.ToString());//图片编号 img.Src = "http://m.weather.com.cn/img/b" + str_img_id + ".gif"; img.Width = 50; img.Height = 50; } } /// <summary> /// 获得天气的信息(json格式) /// </summary> /// <returns></returns> private string GetResponseStr() { string str = "http://m.weather.com.cn/data/101010100.html"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(str); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream stream = response.GetResponseStream();//获得回应的数据流 //将数据流转成 String string result = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd(); return result; } HttpWebRequest web1=new HttpWebRequest(); string ing=web1.GetLifetimeService(http://m.weather.com.cn/data/101010100.html);是调用这个方法吗?还是说:不对啊。能给一行代码不。 谢谢,好全。我用下面这个代码出现了乱码: string result=web.DownloadString(address); Console.WriteLine(result);乱码如下:{"weatherinfo":{"city":"鍖椾含","city_en":"beijing","date_y":"2013骞?鏈?9鏃?,"date":"","week":"鏄熸湡涓?,"fchh":"08","cityid":"101010100","temp1":"34鈩儈22鈩?,"temp2":"32鈩儈22鈩?,"temp3":"30鈩儈21鈩?,"temp4":"26鈩儈20鈩?,"temp5":"28鈩儈21鈩?,"temp6":"30鈩儈22鈩?,"tempF1":"93.2鈩墌71.6鈩?,"tempF2":"89.6鈩墌71.6鈩?,"tempF3":"86鈩墌69.8鈩?,"tempF4":"78.8鈩墌68鈩?,"tempF5":"82.4鈩墌69.8鈩?,"tempF6":"86鈩墌71.6鈩?,"weather1":"鏅?,"weather2":"澶氫簯","weather3":"澶氫簯杞槾","weather4":"闆烽樀闆ㄨ浆鏅?,"weather5":"鏅磋浆澶氫簯","weather6":"澶氫簯杞槾","img1":"0","img2":"99","img3":"1","img4":"99","img5":"1","img6":"2","img7":"4","img8":"0","img9":"0","img10":"1","img11":"1","img12":"2","img_single":"0","img_title1":"鏅?,"img_title2":"鏅?,"img_title3":"澶氫簯","img_title4":"澶氫簯","img_title5":"澶氫簯","img_title6":"闃?,"img_title7":"闆烽樀闆?,"img_title8":"鏅?,"img_title9":"鏅?,"img_title10":"澶氫簯","img_title11":"澶氫簯","img_title12":"闃?,"img_title_single":"鏅?,"wind1":"寰","wind2":"寰","wind3":"寰","wind4":"寰","wind5":"寰","wind6":"寰","fx1":"寰","fx2":"寰","fl1":"灏忎簬3绾?,"fl2":"灏忎簬3绾?,"fl3":"灏忎簬3绾?,"fl4":"灏忎簬3绾?,"fl5":"灏忎簬3绾?,"fl6":"灏忎簬3绾?,"index":"鐐庣儹","index_d":"澶╂皵鐐庣儹锛屽缓璁潃鐭~銆佺煭瑁欍€佺煭瑁ゃ€佽杽鍨婽鎭よ~绛夋竻鍑夊瀛f湇瑁呫€?,"index48":"鐐庣儹","index48_d":"澶╂皵鐐庣儹锛屽缓璁潃鐭~銆佺煭瑁欍€佺煭瑁ゃ€佽杽鍨婽鎭よ~绛夋竻鍑夊瀛f湇瑁呫€?,"index_uv":"寰堝己","index48_uv":"涓瓑","index_xc":"閫傚疁","index_tr":"杈冮€傚疁","index_co":"寰堜笉鑸掗€?,"st1":"34","st2":"23","st3":"32","st4":"22","st5":"30","st6":"20","index_cl":"閫傚疁","index_ls":"鏋侀€傚疁","index_ag":"涓嶆槗鍙?}}这个乱码有什么方法让他不是乱码吗 public static string GetUrltoHtml(string Url,string type) { try { System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url); // Get the response instance. System.Net.WebResponse wResp = wReq.GetResponse(); System.IO.Stream respStream = wResp.GetResponseStream(); // Dim reader As StreamReader = New StreamReader(respStream) using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.GetEncoding(type))) { return reader.ReadToEnd(); } } catch (System.Exception ex) { //errorMsg = ex.Message; } return ""; } byte[] 保留需要的空格 问题 C#连接数据库时提示未指定的错误 求了!!!!!! 怎么读取listbox控件里选定的值 form 很急的问题,微软树的问题,(在线等急...解决马上放分.........) 求助,如何在主窗体的旁边显示richtextbox控件 C# list find怎么使用 unchecked()相关语法的意思 求救!求救!求救!!!!HELP ME!!!!!!!! 大神们,救命啊。 TFS没签入点了撤销更改了。 windows消息机制
能够稍微写清楚点吗?是调用webservice这个类的意思?
string reply = client.DownloadString (address);这边的address必须是IP地址吗?
参数
address
包含要下载的 URI 的 String。
如果 BaseAddress 属性不是空字符串 (""),且 address 不包含绝对 URI,则 address 必须是相对 URI,此 URI 与 BaseAddress 组合在一起构成所请求数据的绝对 URI。如果 QueryString 属性不是空字符串,则将它追加到 address。
我这个 string result=web.DownloadString(http://m.weather.com.cn/data/101010100.html);就是绝对URI但是C#说我现在是http://这个:是无效的
//很早以前写过一段//**************************************************** protected void Page_Load(object sender, EventArgs e)
{ string str = GetResponseStr();//获得json天气信息
//对json格式的信息重新整理一下,因为我们要用.net自带的方法获得相关信息
str = str.Remove(str.LastIndexOf("}"), 1);
str = str.Remove(0, 15); //使用JavaScriptSerializer对象来解析数据
JavaScriptSerializer serializer = new JavaScriptSerializer();
Dictionary<string, object> json = (Dictionary<string, object>)serializer.DeserializeObject(str); object str_city; //-- 城市 -- city
object str_weather; //-- 天气 -- weather1
object str_img_id; //-- 天气图标编号 -- img1
object str_temp; //-- 温度 -- temp1
object str_fl; //-- 风力 --fl1
object str_UV; //-- 紫外线 -- index_uv
object str_cy; //-- 穿衣指数 -- index
object str_xc; //-- 洗车指数 -- index_xc
object str_CO; //-- 舒适指数 -- index_co
object str_Time; //-- 时间 -- date_y
object str_week; //-- 星期 -- week if (json.TryGetValue("city", out str_city))
{
Response.Write (str_city.ToString());//城市
}
if (json.TryGetValue("weather1", out str_weather))
{
//Response.Write(str_weather.ToString());//天气
lab_Tianqi.Text = str_weather.ToString();
}
if (json.TryGetValue("temp1", out str_temp))
{
//Response.Write(str_temp.ToString());//温度
lab_wendu.Text = str_temp.ToString();
}
if (json.TryGetValue("fl1", out str_fl))
{
//Response.Write(str_fl.ToString());//风力
lab_Fengli.Text = str_fl.ToString();
}
if (json.TryGetValue("index_uv", out str_UV))
{
//Response.Write(str_UV.ToString());//紫外线
lab_UV.Text = str_UV.ToString();
}
//if (json.TryGetValue("index", out str_cy))
//{
// Response.Write(str_cy.ToString());//穿衣
//}
if (json.TryGetValue("index_xc", out str_xc))
{
//Response.Write(str_xc.ToString());//洗车
lab_Xc.Text = str_xc.ToString();
}
if (json.TryGetValue("index_co", out str_CO))
{
//Response.Write(str_CO.ToString());//舒适
lab_Co.Text = str_CO.ToString();
} if (json.TryGetValue("date_y", out str_Time))
{
// Response.Write(str_Time.ToString());//时间
lab_Time.Text = str_Time.ToString();
}
if (json.TryGetValue("week", out str_week))
{
//Response.Write(str_week.ToString());//星期
lab_Week.Text = str_week.ToString();
} if (json.TryGetValue("img1", out str_img_id))
{
//Response.Write(str_img_id.ToString());//图片编号
img.Src = "http://m.weather.com.cn/img/b" + str_img_id + ".gif";
img.Width = 50;
img.Height = 50;
} } /// <summary>
/// 获得天气的信息(json格式)
/// </summary>
/// <returns></returns>
private string GetResponseStr()
{
string str = "http://m.weather.com.cn/data/101010100.html";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(str);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();//获得回应的数据流
//将数据流转成 String
string result = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd();
return result;
}
string ing=web1.GetLifetimeService(http://m.weather.com.cn/data/101010100.html);是调用这个方法吗?还是说:不对啊。能给一行代码不。
我用下面这个代码出现了乱码:
string result=web.DownloadString(address);
Console.WriteLine(result);
乱码如下:
{"weatherinfo":{"city":"鍖椾含","city_en":"beijing","date_y":"2013骞?鏈?9鏃?,"date":"","week":"鏄熸湡涓?,"fchh":"08","cityid":"101010100","temp1":"34鈩儈22鈩?,"temp2":"32鈩儈22鈩?,"temp3":"30鈩儈21鈩?,"temp4":"26鈩儈20鈩?,"temp5":"28鈩儈21鈩?,"temp6":"30鈩儈22鈩?,"tempF1":"93.2鈩墌71.6鈩?,"tempF2":"89.6鈩墌71.6鈩?,"tempF3":"86鈩墌69.8鈩?,"tempF4":"78.8鈩墌68鈩?,"tempF5":"82.4鈩墌69.8鈩?,"tempF6":"86鈩墌71.6鈩?,"weather1":"鏅?,"weather2":"澶氫簯","weather3":"澶氫簯杞槾","weather4":"闆烽樀闆ㄨ浆鏅?,"weather5":"鏅磋浆澶氫簯","weather6":"澶氫簯杞槾","img1":"0","img2":"99","img3":"1","img4":"99","img5":"1","img6":"2","img7":"4","img8":"0","img9":"0","img10":"1","img11":"1","img12":"2","img_single":"0","img_title1":"鏅?,"img_title2":"鏅?,"img_title3":"澶氫簯","img_title4":"澶氫簯","img_title5":"澶氫簯","img_title6":"闃?,"img_title7":"闆烽樀闆?,"img_title8":"鏅?,"img_title9":"鏅?,"img_title10":"澶氫簯","img_title11":"澶氫簯","img_title12":"闃?,"img_title_single":"鏅?,"wind1":"寰","wind2":"寰","wind3":"寰","wind4":"寰","wind5":"寰","wind6":"寰","fx1":"寰","fx2":"寰","fl1":"灏忎簬3绾?,"fl2":"灏忎簬3绾?,"fl3":"灏忎簬3绾?,"fl4":"灏忎簬3绾?,"fl5":"灏忎簬3绾?,"fl6":"灏忎簬3绾?,"index":"鐐庣儹","index_d":"澶╂皵鐐庣儹锛屽缓璁潃鐭~銆佺煭瑁欍€佺煭瑁ゃ€佽杽鍨婽鎭よ~绛夋竻鍑夊瀛f湇瑁呫€?,"index48":"鐐庣儹","index48_d":"澶╂皵鐐庣儹锛屽缓璁潃鐭~銆佺煭瑁欍€佺煭瑁ゃ€佽杽鍨婽鎭よ~绛夋竻鍑夊瀛f湇瑁呫€?,"index_uv":"寰堝己","index48_uv":"涓瓑","index_xc":"閫傚疁","index_tr":"杈冮€傚疁","index_co":"寰堜笉鑸掗€?,"st1":"34","st2":"23","st3":"32","st4":"22","st5":"30","st6":"20","index_cl":"閫傚疁","index_ls":"鏋侀€傚疁","index_ag":"涓嶆槗鍙?}}
这个乱码有什么方法让他不是乱码吗
public static string GetUrltoHtml(string Url,string type)
{
try
{
System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
// Get the response instance.
System.Net.WebResponse wResp = wReq.GetResponse();
System.IO.Stream respStream = wResp.GetResponseStream();
// Dim reader As StreamReader = New StreamReader(respStream)
using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.GetEncoding(type)))
{
return reader.ReadToEnd();
}
}
catch (System.Exception ex)
{
//errorMsg = ex.Message;
}
return "";
}