当前位置: 首页 > news >正文

网站建设dw实训总结陕西网站建设设计

网站建设dw实训总结,陕西网站建设设计,无为网站定制,做网站如何规避法律风险前言#xff1a;最近闲来无事#xff0c;看了网上豆瓣的第三方客户端#xff0c;手有点痒#xff0c;决定自己动手开发一个客户端#xff0c;比较了荔枝和喜马拉雅#xff0c;决定开发喜马拉雅的第三方客户端。 客户端使用了WPF开发。 1.抓取接口#xff1b; 首先得解决…前言最近闲来无事看了网上豆瓣的第三方客户端手有点痒决定自己动手开发一个客户端比较了荔枝和喜马拉雅决定开发喜马拉雅的第三方客户端。 客户端使用了WPF开发。 1.抓取接口 首先得解决接口数据的问题使用了手机端的喜马拉雅抓包看了接口。这里推荐使用fiddler2这个工具。从图中可以看到接口信息包括接口地址和参数的一些数据。 2.通过http获取接口数据和转换接口数据格式。 这里提供一个HttpWebRequestOpt类来获取接口数据。 using System; using System.Collections.Specialized; using System.IO; using System.Net; using System.Text;namespace XIMALAYA.PCDesktop.Untils {/// summary/// 数据操作类/// /summarypublic class HttpWebRequestOpt{/// summary/// /// /summarypublic string UserAgent { get; set; }/// summary/// cookie/// /summaryprivate CookieContainer Cookies { get; set; }private HttpWebRequestOpt(){//FileVersionInfo myFileVersion FileVersionInfo.GetVersionInfo(Path.Combine(Directory.GetCurrentDirectory(), XIMALAYA.PCDesktop.exe));this.Cookies new CookieContainer();//this.UserAgent string.Format(ting-ximalaya_v{0} name/ximalaya os/{1} osName/{2}, myFileVersion.FileVersion, OSInfo.Instance.OsInfo.VersionString, OSInfo.Instance.OsInfo.Platform.ToString());//this.Cookies.Add(new Cookie(4_token, 935d63fef280403904a8c0a5ee0dbe228f2d064, /, .ximalaya.com));}/// summary/// 添加cookie/// /summary/// param namecookie/parampublic void SetCookies(Cookie cookie){this.Cookies.Add(cookie);}/// summary/// 添加cookie/// /summary/// param namecookie/parampublic void SetCookies(string key, string val){this.Cookies.Add(new Cookie(key, val, /, .ximalaya.com));}/// summary/// 通过POST方式发送数据/// /summary/// param nameUrlurl/param/// param namepostDataStrPost数据/param/// returns/returnspublic string SendDataByPost(string Url, string postDataStr){HttpWebRequest request (HttpWebRequest)WebRequest.Create(Url);request.CookieContainer this.Cookies;request.Method POST;request.ContentType application/x-www-form-urlencoded;request.ContentLength postDataStr.Length;request.UserAgent this.UserAgent;Stream myRequestStream request.GetRequestStream();StreamWriter myStreamWriter new StreamWriter(myRequestStream, Encoding.GetEncoding(gb2312));myStreamWriter.Write(postDataStr);myStreamWriter.Close();HttpWebResponse response (HttpWebResponse)request.GetResponse();Stream myResponseStream response.GetResponseStream();StreamReader myStreamReader new StreamReader(myResponseStream, Encoding.GetEncoding(utf-8));string retString myStreamReader.ReadToEnd();myStreamReader.Close();myResponseStream.Close();return retString;}/// summary/// 通过GET方式发送数据/// /summary/// param nameUrlurl/param/// param namepostDataStrGET数据/param/// returns/returnspublic string SendDataByGET(string Url, string postDataStr){HttpWebRequest request (HttpWebRequest)WebRequest.Create(Url (postDataStr ? : ?) postDataStr);request.CookieContainer this.Cookies;request.Method GET;request.ContentType text/html;charsetUTF-8;request.UserAgent this.UserAgent;HttpWebResponse response (HttpWebResponse)request.GetResponse();Stream myResponseStream response.GetResponseStream();StreamReader myStreamReader new StreamReader(myResponseStream, Encoding.GetEncoding(utf-8));string retString myStreamReader.ReadToEnd();myStreamReader.Close();myResponseStream.Close();return retString;}/// summary/// 异步通过POST方式发送数据/// /summary/// param nameUrlurl/param/// param namepostDataStrGET数据/param/// param nameasync/parampublic void SendDataByPostAsyn(string Url, string postDataStr, AsyncCallback async){HttpWebRequest request (HttpWebRequest)WebRequest.Create(Url);request.CookieContainer this.Cookies;request.Method POST;request.ContentType application/x-www-form-urlencoded;request.ContentLength postDataStr.Length;request.UserAgent this.UserAgent;Stream myRequestStream request.GetRequestStream();StreamWriter myStreamWriter new StreamWriter(myRequestStream, Encoding.GetEncoding(gb2312));myStreamWriter.Write(postDataStr);myStreamWriter.Close();myRequestStream.Close();request.BeginGetResponse(async, request);}/// summary/// 异步通过GET方式发送数据/// /summary/// param nameUrlurl/param/// param namepostDataStrGET数据/param/// param nameasync/param/// returns/returnspublic void SendDataByGETAsyn(string Url, string postDataStr, AsyncCallback async){HttpWebRequest request (HttpWebRequest)WebRequest.Create(Url (postDataStr ? : ?) postDataStr);request.CookieContainer this.Cookies;request.Method GET;request.ContentType text/html;charsetUTF-8;request.UserAgent this.UserAgent;request.BeginGetResponse(async, request);}/// summary/// 使用HttpWebRequest POST图片等文件带参数/// /summary/// param nameurl/param/// param namefile/param/// param nameparamName/param/// param namecontentType/param/// param namenvc/param/// returns/returnspublic string HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc){string result string.Empty;string boundary --------------------------- DateTime.Now.Ticks.ToString(x);byte[] boundarybytes System.Text.Encoding.ASCII.GetBytes(\r\n-- boundary \r\n);HttpWebRequest wr (HttpWebRequest)WebRequest.Create(url);wr.ContentType multipart/form-data; boundary boundary;wr.Method POST;wr.KeepAlive true;wr.Credentials System.Net.CredentialCache.DefaultCredentials;Stream rs wr.GetRequestStream();string formdataTemplate Content-Disposition: form-data; name\{0}\\r\n\r\n{1};foreach (string key in nvc.Keys){rs.Write(boundarybytes, 0, boundarybytes.Length);string formitem string.Format(formdataTemplate, key, nvc[key]);byte[] formitembytes System.Text.Encoding.UTF8.GetBytes(formitem);rs.Write(formitembytes, 0, formitembytes.Length);}rs.Write(boundarybytes, 0, boundarybytes.Length);string headerTemplate Content-Disposition: form-data; name\{0}\; filename\{1}\\r\nContent-Type: {2}\r\n\r\n;string header string.Format(headerTemplate, paramName, file, contentType);byte[] headerbytes System.Text.Encoding.UTF8.GetBytes(header);rs.Write(headerbytes, 0, headerbytes.Length);FileStream fileStream new FileStream(file, FileMode.Open, FileAccess.Read);byte[] buffer new byte[4096];int bytesRead 0;while ((bytesRead fileStream.Read(buffer, 0, buffer.Length)) ! 0){rs.Write(buffer, 0, bytesRead);}fileStream.Close();byte[] trailer System.Text.Encoding.ASCII.GetBytes(\r\n-- boundary --\r\n);rs.Write(trailer, 0, trailer.Length);rs.Close();WebResponse wresp null;try{wresp wr.GetResponse();Stream stream2 wresp.GetResponseStream();StreamReader reader2 new StreamReader(stream2);result reader2.ReadToEnd();}catch (Exception ex){if (wresp ! null){wresp.Close();wresp null;}}finally{wr null;}return result;}} } View Code 接口地址http://mobile.ximalaya.com/m/index_subjects接口数据如下 {ret:0,focusImages:{ret:0,list:[{id:1384,shortTitle:DJ张羊 谢谢你的美好感恩特辑,longTitle:DJ张羊 谢谢你的美好感恩特辑,pic:http://fdfs.xmcdn.com/group5/M06/A8/1D/wKgDtlR1oKHSsFngAAFlxraThWc933.jpg,type:3,trackId:4428642,uid:1315711},{id:1388,shortTitle:小清新女神11月榜,longTitle:小清新女神11月榜,pic:http://fdfs.xmcdn.com/group5/M04/A8/25/wKgDtlR1owzjFmE7AAF3pxnuNxg222.jpg,type:5},{id:1383,shortTitle:王朔《你也不会年轻很久》 静波播讲,longTitle:王朔《你也不会年轻很久》 静波播讲,pic:http://fdfs.xmcdn.com/group5/M03/A8/1C/wKgDtlR1oE-xEoq6AAEfe5PJmt4656.jpg,type:3,trackId:4417987,uid:12512006},{id:1382,shortTitle:楚老湿大课堂长效图-娱乐,longTitle:楚老湿大课堂长效图-娱乐,pic:http://fdfs.xmcdn.com/group5/M06/A8/19/wKgDtlR1n7ORluWTAAFuKujnTB0163.jpg,type:3,trackId:4422955,uid:8401915},{id:1365,shortTitle:唱响喜玛拉雅活动图,longTitle:唱响喜玛拉雅活动图,pic:http://fdfs.xmcdn.com/group5/M06/A5/6C/wKgDtVR0VFXA3LWXAAMruRW5vnI973.png,type:8,url:http://activity.ximalaya.com/activity-web/activity/57?appiting},{id:1363,shortTitle:欧莱雅广告图24、25、27、28,longTitle:欧莱雅广告图24、25、27、28,pic:http://fdfs.xmcdn.com/group5/M05/A0/32/wKgDtlRyla6AnGneAAF2kpKTc2I036.jpg,type:4,url:http://ma8.qq.com/wap/index.html?utm_sourcexmlyutm_medium113282464utm_termutm_contentxmly01utm_campaignCPD_LRL_MEN_MA8%20Campaign_20141118_MO_other}]},categories:{ret:0,data:[]},latest_special:{title:感恩的心 感谢有你,coverPathSmall:http://fdfs.xmcdn.com/group5/M04/AA/9B/wKgDtlR2q_jxMbU-AATUrGYasdg092_mobile_small.jpg,coverPathBig:http://fdfs.xmcdn.com/group5/M04/AA/9B/wKgDtlR2q_jxMbU-AATUrGYasdg092.jpg,coverPathBigPlus:null,isHot:false},latest_activity:{title:唱响喜马拉雅-每年四季打造你的音乐梦想,coverPathSmall:http://fdfs.xmcdn.com/group5/M06/A4/DA/wKgDtlR0UQLik8xMABBJsD5tCNU868_mobile_small.jpg,isHot:true},recommendAlbums:{ret:0,maxPageId:250,count:1000,list:[{id:232357,title:今晚80后脱口秀 2014,coverSmall:http://fdfs.xmcdn.com/group4/M01/19/5A/wKgDtFMsAq3COyRPAAUQ_GUt96k211_mobile_small.jpg,playsCounts:29318050},{id:287570,title:大漠谣风中奇缘,coverSmall:http://fdfs.xmcdn.com/group4/M07/7D/90/wKgDtFRGQFPzpmIsAAQ3HgQ6JRU598_mobile_small.jpg,playsCounts:669091},{id:214706,title:段子来了 采采,coverSmall:http://fdfs.xmcdn.com/group3/M04/64/9D/wKgDsVJ6DnSy_6Q7AAEXoFUKDKE679_mobile_small.jpg,playsCounts:29},{id:233577,title:财经郎眼 2014,coverSmall:http://fdfs.xmcdn.com/group2/M02/4E/2F/wKgDsFLTVG7RU3ZQAAPtxcqJYug831_mobile_small.jpg,playsCounts:8877870}]}}有了数据就需要解析数据。接口数据为JSON格式这里使用了FluentJson这个开源项目可以把类与JSON数据互相转换。官网上有相关的源码和实例可以下载看一下。下面介绍使用方法。 就针对上面的那个发现也接口我定义了一个类。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using XIMALAYA.PCDesktop.Core.Models.Album; using XIMALAYA.PCDesktop.Core.Models.Category; using XIMALAYA.PCDesktop.Core.Models.FocusImage; using XIMALAYA.PCDesktop.Core.Models.Subject; using XIMALAYA.PCDesktop.Core.Models.User;namespace XIMALAYA.PCDesktop.Core.Models.Discover {public class SuperExploreIndexResult : BaseResult{/// summary/// 焦点图/// /summarypublic FocusImageResult FocusImages { get; set; }/// summary/// 分类/// /summarypublic CategoryResult Categories { get; set; }/// summary/// 最后一个专题/// /summarypublic object LatestSpecial { get; set; }/// summary/// 最后一个活动/// /summarypublic object LatestActivity { get; set; }/// summary/// 推荐专辑/// /summarypublic AlbumInfoResult1 Albums { get; set; }public SuperExploreIndexResult(): base(){this.doAddMap(() this.FocusImages, focusImages);this.doAddMap(() this.Categories, categories);this.doAddMap(() this.LatestActivity, latest_activity);this.doAddMap(() this.LatestSpecial, latest_special);this.doAddMap(() this.Albums, recommendAlbums);}} } View Code  这个SuperExploreIndexResult类的构造函数对应了接口数据中的射影关系。 生成的映射类如下 // auto-generated // 此代码由工具生成。 // 对此文件的更改可能会导致不正确的行为并且如果 // 重新生成代码这些更改将会丢失。 // 如存在本生成代码外的新需求请在相同命名空间下创建同名分部类实现 SuperExploreIndexResultConfigurationAppend 分部方法。 // /auto-generated using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;using FluentJson.Configuration; using FluentJson; using XIMALAYA.PCDesktop.Core.Data.Decorator; using XIMALAYA.PCDesktop.Core.Models.Discover;namespace XIMALAYA.PCDesktop.Core.Data {/// summary/// SuperExploreIndexResult/// /summary/// typeparam nameT/typeparampublic partial class SuperExploreIndexResultDecoratorT : DecoratorT{partial void doAddOtherConfig();/// summary/// /// /summary/// typeparam nameresult/typeparampublic SuperExploreIndexResultDecorator(ResultT result): base(result){}/// summary/// /// /summary/// typeparam nameresult/typeparampublic override void doAddConfig(){base.doAddConfig();this.Config.MapTypeSuperExploreIndexResult(map map.FieldSystem.Int32(field field.Ret, type type.To(ret)).FieldSystem.String(field field.Message, type type.To(msg)).FieldXIMALAYA.PCDesktop.Core.Models.FocusImage.FocusImageResult(field field.FocusImages, type type.To(focusImages)).FieldXIMALAYA.PCDesktop.Core.Models.Category.CategoryResult(field field.Categories, type type.To(categories)).FieldSystem.Object(field field.LatestActivity, type type.To(latest_activity)).FieldSystem.Object(field field.LatestSpecial, type type.To(latest_special)).FieldXIMALAYA.PCDesktop.Core.Models.Album.AlbumInfoResult1(field field.Albums, type type.To(recommendAlbums)));this.doAddOtherConfig();}} } View Code 这里只列出了一个SuperExploreIndexResult类还有CategoryResultFocusImageResultAlbumInfoResult1这三个类也做了同样的映射。这样这个接口的数据最终就可以映射为SuperExploreIndexResult类了。总之把接口中JSON数据中的对象是全部需要隐射的。 下面演示了如何调用上面的映射类。代码中所有带Decorator后缀的类都是映射类。采用了下装饰模式。 using System; using System.ComponentModel.Composition; using FluentJson; using XIMALAYA.PCDesktop.Core.Data; using XIMALAYA.PCDesktop.Core.Data.Decorator; using XIMALAYA.PCDesktop.Core.Models.Discover; using XIMALAYA.PCDesktop.Core.Models.Tags; using XIMALAYA.PCDesktop.Untils;namespace XIMALAYA.PCDesktop.Core.Services {/// summary/// 发现页接口数据/// /summary[Export(typeof(IExploreService))]class ExploreService : ServiceBaseSuperExploreIndexResult, IExploreService{#region 属性private ServiceParamsSuperExploreIndexResult SuperExploreIndexResult { get; set; }#endregion#region IExploreService 成员/// summary/// 获取发现首页数据/// /summary/// typeparam nameT/typeparam/// param nameact/param/// param nameparam/parampublic void GetDataT(Actionobject act, T param){ResultSuperExploreIndexResult result new ResultSuperExploreIndexResult();new SuperExploreIndexResultDecoratorSuperExploreIndexResult(result);//分类new CategoryResultDecoratorSuperExploreIndexResult(result);new CategoryDataDecoratorSuperExploreIndexResult(result);//焦点图new FocusImageResultDecoratorSuperExploreIndexResult(result);new FocusImageDataDecoratorSuperExploreIndexResult(result);//推荐用户//new UserDataDecoratorSuperExploreIndexResult(result);//推荐专辑new AlbumInfoResult1DecoratorSuperExploreIndexResult(result);new AlbumData1DecoratorSuperExploreIndexResult(result);//专题列表//new SubjectListResultDecoratorSuperExploreIndexResult(result);//new SubjectDataDecoratorSuperExploreIndexResult(result);this.SuperExploreIndexResult new ServiceParamsSuperExploreIndexResult(Json.DecoderForSuperExploreIndexResult(config config.DeriveFrom(result.Config)), act);//this.Act act;//this.Decoder Json.DecoderForSuperExploreIndexResult(config config.DeriveFrom(result.Config));try{this.Responsitory.Fetch(WellKnownUrl.SuperExploreIndex, param.ToString(), asyncResult {this.GetDecodeDataSuperExploreIndexResult(this.GetDataCallBack(asyncResult), this.SuperExploreIndexResult);});}catch (Exception ex){this.SuperExploreIndexResult.Act.BeginInvoke(new SuperExploreIndexResult{Ret 500,Message ex.Message}, null, null);}}#endregion} } View Code 如上只要配置好映射关系通过T4模板我们可以生成对应的映射关系类。 附上源码 下篇客户端使用了prismmef这个框架单独开发模块最后组合的方式。未完待续。。。。  转载于:https://www.cnblogs.com/nicktyui/p/4126698.html
http://www.sadfv.cn/news/130175/

相关文章:

  • 湖南网站推广公司腾云网站建设
  • 美工常用找素材网站html5建设网站
  • 做网站搭建需要什么人西服定制一般多少钱
  • html5 网站布局应用教程公司做一个网站如何定位
  • 买网站源码的网站网站建设案例 算命网站
  • 上海网站设计团队怎么查网站icp备案
  • 美食网站建设页面要求做网站用的字体
  • 信息网站建设汇报wordpress提交360
  • 做企业英语网站要注意哪些百度的营销中心上班怎么样
  • 广州越秀区租房温岭新站seo
  • seo网站推广的主要目的是什么微信开发商
  • 上海各区的网站有哪些公司深圳网站建设软件开发公司
  • 网站制作设计专业公司广州网站建设公司网络安全优化
  • 网站开发语言 知乎温州市名城建设集团有限公司网站
  • 登封市城乡建设路网站社群网站建设
  • 企业网站建设的文章广州网站推广服务商
  • 管家婆免费资料网站电销精准客户数据资源
  • 绍兴网站制作软件网络技术挑战赛
  • 网站被百度收录很重要建站之星服务器
  • 自己做黑彩网站常州免费做网站
  • 江门网站平台建设一个网站怎么做软件
  • 做网站推广 seo的移动端英文简称
  • 前端静态网站模板下载伊春网络建站公司
  • 营口汽车网站建设wordpress 升级后 插件
  • 购物网站 服务器 带宽 多大电商平台业务流程图
  • 常州做网站哪家便宜大理网站建设网站建设
  • 临淄关键词网站优化哪家好网站建设更改
  • 制作网站 公司简介2345网址导航下载桌面
  • 国度网络网站建设做官网需要多少钱
  • 公司网站宣传设计方案福田庆三眼睛案例图片