天气预报调用API

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using Xfrog.Net;
using System.Diagnostics;
using System.Web;
  
//----------------------------------
// 全国天气预报调用示例代码 - 聚合数据
// 在线接口文档:https://www.juhe.cn/docs/39
// 代码中JsonObject类下载地址:http://download.csdn.net/download/gcm3206021155665/7458439
//----------------------------------
  
namespaceConsoleAPI
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            stringappkey = "*******************";//配置您申请的appkey
  
              
            //1.根据城市名/id查询天气
            stringurl1 = "http://v.juhe.cn/weather/index";
  
            varparameters1 = new Dictionary<string, string>();
  
            parameters1.Add("cityname", "");//城市名或城市ID,如:"苏州",需要utf8 urlencode
            parameters1.Add("dtype", "");//返回数据格式:json或xml,默认json
            parameters1.Add("format", "");//未来6天预报(future)两种返回格式,1或2,默认1
            parameters1.Add("key", appkey);//你申请的key
  
            stringresult1 = sendPost(url1, parameters1, "get");
  
            JsonObject newObj1 =new JsonObject(result1);
            String errorCode1 = newObj1["error_code"].Value;
  
            if(errorCode1 == "0")
            {
                Debug.WriteLine("成功");
                Debug.WriteLine(newObj1);
            }
            else
            {
                //Debug.WriteLine("失败");
                Debug.WriteLine(newObj1["error_code"].Value+":"+newObj1["reason"].Value);
            }
  
  
            //2.天气种类及标识列表
            stringurl2 = "http://v.juhe.cn/weather/uni";
  
            varparameters2 = new Dictionary<string, string>();
  
            parameters2.Add("key", appkey);//你申请的key
            parameters2.Add("dtype", "");//返回数据的格式,xml或json,默认json
  
            stringresult2 = sendPost(url2, parameters2, "get");
  
            JsonObject newObj2 =new JsonObject(result2);
            String errorCode2 = newObj2["error_code"].Value;
  
            if(errorCode2 == "0")
            {
                Debug.WriteLine("成功");
                Debug.WriteLine(newObj2);
            }
            else
            {
                //Debug.WriteLine("失败");
                Debug.WriteLine(newObj2["error_code"].Value+":"+newObj2["reason"].Value);
            }
  
  
            //3.根据IP查询天气
            stringurl3 = "http://v.juhe.cn/weather/ip";
  
            varparameters3 = new Dictionary<string, string>();
  
            parameters3.Add("ip", "");//ip地址,如:58.215.185.154
            parameters3.Add("dtype", "");//返回数据格式:json或xml,默认json
            parameters3.Add("format", "");//未来6天预报(future)两种返回格式,1或2,默认1
            parameters3.Add("key", appkey);//你申请的key
  
            stringresult3 = sendPost(url3, parameters3, "get");
  
            JsonObject newObj3 =new JsonObject(result3);
            String errorCode3 = newObj3["error_code"].Value;
  
            if(errorCode3 == "0")
            {
                Debug.WriteLine("成功");
                Debug.WriteLine(newObj3);
            }
            else
            {
                //Debug.WriteLine("失败");
                Debug.WriteLine(newObj3["error_code"].Value+":"+newObj3["reason"].Value);
            }
  
  
            //4.根据GPS坐标查询天气
            stringurl4 = "http://v.juhe.cn/weather/geo";
  
            varparameters4 = new Dictionary<string, string>();
  
            parameters4.Add("lon", "");//经度,如:116.39277
            parameters4.Add("lat", "");//纬度,如:39.933748
            parameters4.Add("format", "");//未来6天预报(future)两种返回格式,1或2,默认1
            parameters4.Add("dtype", "");//返回数据格式:json或xml,默认json
            parameters4.Add("key", appkey);//你申请的key
  
            stringresult4 = sendPost(url4, parameters4, "get");
  
            JsonObject newObj4 =new JsonObject(result4);
            String errorCode4 = newObj4["error_code"].Value;
  
            if(errorCode4 == "0")
            {
                Debug.WriteLine("成功");
                Debug.WriteLine(newObj4);
            }
            else
            {
                //Debug.WriteLine("失败");
                Debug.WriteLine(newObj4["error_code"].Value+":"+newObj4["reason"].Value);
            }
  
  
            //5.城市天气三小时预报
            stringurl5 = "http://v.juhe.cn/weather/forecast3h";
  
            varparameters5 = new Dictionary<string, string>();
  
            parameters5.Add("cityname", "");//城市名,如:"苏州"
            parameters5.Add("dtype", "");//返回数据格式:json或xml,默认json
            parameters5.Add("key", appkey);//你申请的key
  
            stringresult5 = sendPost(url5, parameters5, "get");
  
            JsonObject newObj5 =new JsonObject(result5);
            String errorCode5 = newObj5["error_code"].Value;
  
            if(errorCode5 == "0")
            {
                Debug.WriteLine("成功");
                Debug.WriteLine(newObj5);
            }
            else
            {
                //Debug.WriteLine("失败");
                Debug.WriteLine(newObj5["error_code"].Value+":"+newObj5["reason"].Value);
            }
  
  
            //6.支持城市列表
            stringurl6 = "http://v.juhe.cn/weather/citys";
  
            varparameters6 = new Dictionary<string, string>();
  
            parameters6.Add("dtype", "");//返回数据格式:json或xml,默认json
            parameters6.Add("key", appkey);//你申请的key
  
            stringresult6 = sendPost(url6, parameters6, "get");
  
            JsonObject newObj6 =new JsonObject(result6);
            String errorCode6 = newObj6["error_code"].Value;
  
            if(errorCode6 == "0")
            {
                Debug.WriteLine("成功");
                Debug.WriteLine(newObj6);
            }
            else
            {
                //Debug.WriteLine("失败");
                Debug.WriteLine(newObj6["error_code"].Value+":"+newObj6["reason"].Value);
            }
  
  
        }
  
        /// <summary>
        /// Http (GET/POST)
        /// </summary>
        /// <param name="url">请求URL</param>
        /// <param name="parameters">请求参数</param>
        /// <param name="method">请求方法</param>
        /// <returns>响应内容</returns>
        staticstring sendPost(stringurl, IDictionary<string,string> parameters,string method)
        {
            if(method.ToLower() == "post")
            {
                HttpWebRequest req =null;
                HttpWebResponse rsp =null;
                System.IO.Stream reqStream =null;
                try
                {
                    req = (HttpWebRequest)WebRequest.Create(url);
                    req.Method = method;
                    req.KeepAlive =false;
                    req.ProtocolVersion = HttpVersion.Version10;
                    req.Timeout = 5000;
                    req.ContentType ="application/x-www-form-urlencoded;charset=utf-8";
                    byte[] postData = Encoding.UTF8.GetBytes(BuildQuery(parameters,"utf8"));
                    reqStream = req.GetRequestStream();
                    reqStream.Write(postData, 0, postData.Length);
                    rsp = (HttpWebResponse)req.GetResponse();
                    Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
                    returnGetResponseAsString(rsp, encoding);
                }
                catch(Exception ex)
                {
                    returnex.Message;
                }
                finally
                {
                    if(reqStream != null) reqStream.Close();
                    if(rsp != null) rsp.Close();
                }
            }
            else
            {
                //创建请求
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url +"?" + BuildQuery(parameters,"utf8"));
  
                //GET请求
                request.Method ="GET";
                request.ReadWriteTimeout = 5000;
                request.ContentType ="text/html;charset=UTF-8";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader =new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
  
                //返回内容
                stringretString = myStreamReader.ReadToEnd();
                returnretString;
            }
        }
  
        /// <summary>
        /// 组装普通文本请求参数。
        /// </summary>
        /// <param name="parameters">Key-Value形式请求参数字典</param>
        /// <returns>URL编码后的请求数据</returns>
        staticstring BuildQuery(IDictionary<string,string> parameters,string encode)
        {
            StringBuilder postData =new StringBuilder();
            boolhasParam = false;
            IEnumerator<KeyValuePair<string,string>> dem = parameters.GetEnumerator();
            while(dem.MoveNext())
            {
                stringname = dem.Current.Key;
                stringvalue = dem.Current.Value;
                // 忽略参数名或参数值为空的参数
                if(!string.IsNullOrEmpty(name))//&& !string.IsNullOrEmpty(value)
                {
                    if(hasParam)
                    {
                        postData.Append("&");
                    }
                    postData.Append(name);
                    postData.Append("=");
                    if(encode == "gb2312")
                    {
                        postData.Append(HttpUtility.UrlEncode(value, Encoding.GetEncoding("gb2312")));
                    }
                    elseif (encode == "utf8")
                    {
                        postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));
                    }
                    else
                    {
                        postData.Append(value);
                    }
                    hasParam =true;
                }
            }
            returnpostData.ToString();
        }
  
        /// <summary>
        /// 把响应流转换为文本。
        /// </summary>
        /// <param name="rsp">响应流对象</param>
        /// <param name="encoding">编码方式</param>
        /// <returns>响应文本</returns>
        staticstring GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
        {
            System.IO.Stream stream =null;
            StreamReader reader =null;
            try
            {
                // 以字符流的方式读取HTTP响应
                stream = rsp.GetResponseStream();
                reader =new StreamReader(stream, encoding);
                returnreader.ReadToEnd();
            }
            finally
            {
                // 释放资源
                if(reader != null) reader.Close();
                if(stream != null) stream.Close();
                if(rsp != null) rsp.Close();
            }
        }
    }
}


课程通过实际项目融入常用开发技术架构,讲授风格独特,提供详细上课日志及答疑,赠送配套的项目架构源码注释详细清晰且表达通俗,均能直接在实际项目中应用,正真的物超所值,价格实惠任务作业:综合运用《C#/.Net企业级系统架构设计实战精讲教程》课程所学知识技能设计一个学生成绩管理系统的架构。要求:1.系统基于MVC的三层架构,各层单独建不同的解决方案文件夹。2.采用Model First开发方式,设计架构时只需要设计学生表(TbStudent)和课程表(TbCourse)。学生表必须有的字段是ID、stuName、age;课程表必须有的字段是ID、courseName、content。3.数据访问层采用Entity Framework或NHibernate来实现,必须封装对上述表的增删改查方法。4.必须依赖接口编程,也就是必须要有数据访问层的接口层、业务逻辑层的接口层等接口层。层层之间必须减少依赖,可以通过简单工厂或抽象工厂。5.至少采用简单工厂、抽象工厂、Spring.Net等技术中的2种来减少层与层之间的依赖等。6.封装出DbSession类,让它拥有所有Dal层实例和SaveChanges方法。7.设计出数据访问层及业务逻辑层主要类的T4模板,以便实体增加时自动生成相应的类。8.表现层要设计相关的控制器和视图来验证设计的系统架构代码的正确性,必须含有验证增删改查的方法。9.开发平台一定要是Visual Studio平台,采用C#开发语言,数据库为SQL Server。10.提交整个系统架构的源文件及生成的数据库文件。(注意: 作业需写在CSDN博客中,请把作业链接贴在评论区,老师会定期逐个批改~~)
Xfrog 3DS MAX PlugIn (07/04/2002) ======================================== Installation ------------------------------------------------- Copy the file xfrog.dlo into your plugins directory (e.g. c:\3dsmax42\plugins). Compatibility ------------------------------------------------- The PlugIn is compiled for discreet 3DS MAX 4.2+. It will not work with prior versions of 3DS MAX. Usage ------------------------------------------------- 1. Choose 'Import' from the 'File' Menu 2. Select 'Xfrog (*.XFR)' as File Import Filter and choose the Xfrog Model to import. Click OK. 4. The model and material should show up now. Feel free to modify the materials, changes made to the mesh itself will be overridden when the object is reevaluated. Options Panel ------------------------------------------------- The Animation Length Group lets you choose between the Animation Length preset in the file, a custom length to be specified, the length being scaled to fit the current animation length or to be ignored. For static models you should always change this setting to Ignore, this will prevent the model from being recreated when you move the frame slider. An existing Animation can be played back as a loop, swinging back and forth and only once. For faster redraws in viewport or for preview rendering you may also want to change the model quality in viewport and for rendering with the sliders at the bottom of the panel. If you need to access the geometry on a per component level you can do that by converting the object to an Editable Mesh, and choose Select By Material ID in Polygon Selection Mode. This will disable animation as the reference to the original Xfrog file is removed.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值