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

北京市昌平建设工程招标网站wordpress+小说系统

北京市昌平建设工程招标网站,wordpress+小说系统,电子政务网站建设ppt,重庆面条制作目录 应用场景 实现原理 实现代码 PostAnyWhere类 ashx文件部署 小结 应用场景 不同的接口服务器处理不同的应用#xff0c;我们会在实际应用中将A服务器的数据提交给B服务器进行数据接收并处理业务。 比如我们想要处理一个OFFICE文件#xff0c;由用户上传到A服务器…目录 应用场景 实现原理 实现代码 PostAnyWhere类 ashx文件部署 小结  应用场景 不同的接口服务器处理不同的应用我们会在实际应用中将A服务器的数据提交给B服务器进行数据接收并处理业务。 比如我们想要处理一个OFFICE文件由用户上传到A服务器上传成功后由B服务器负责进行数据处理和下载工作这时我们就需要 POST A服务器的文件数据到B服务器进行处理。 实现原理 将用户上传的数据或A服务器已存在的数据通过form-data的形式POST到B服务器B服务由指定ashx文件进行数据接收并转由指定的业务逻辑程序进行处理。如下图 实现代码 PostAnyWhere类 创建一个 PostAnyWhere 类 该类具有如下属性 1public string PostUrl     要提交的服务器URL 2public ListPostFileItem PostData   要准备的数据(PostFileItem类可包括数据和文件类型) 该类包含的关键方法如下 1public void AddText(string key, string value) 该方法将指定的字典数据加入到PostData中 2public void AddFile(string name, string srcFileName, string desName, string contentType text/plain) 该方法将指定的文件添加到PostData中其中 srcFileName 表示要添加的文件名desName表示接收数据生成的文件名 3public string Send()  该方法将开始POST传送数据 代码如下 public class PostAnyWhere{public string PostUrl { get; set; }public ListPostFileItem PostData { get; set; }public PostAnyWhere(){this.PostData new ListPostFileItem();}public void AddText(string key, string value){this.PostData.Add(new PostFileItem { Name key, Value value });}public void AddFile(string name, string srcFileName, string desName,string at, string contentType text/plain){string[] srcName Path.GetFileName(srcFileName).Split(.);string exName ;if (srcName.Length 1){exName .srcName[srcName.Length-1];}this.PostUrl https://www.xxx.com/test.ashx?guid desName;ReadyFile(name, GetBinaryData(srcFileName), exName,contentType);}void ReadyFile(string name, byte[] fileBytes, string fileExName , string contentType text/plain){this.PostData.Add(new PostFileItem{Type PostFileItemType.File,Name name,FileBytes fileBytes,FileName fileExName,ContentType contentType});}public string Send(){var boundary ---------------------------- DateTime.Now.Ticks.ToString(x);var request (HttpWebRequest)WebRequest.Create(this.PostUrl);request.ContentType multipart/form-data; boundary boundary;request.Method POST;request.KeepAlive true;Stream memStream new System.IO.MemoryStream();var boundarybytes System.Text.Encoding.ASCII.GetBytes(\r\n-- boundary \r\n);var endBoundaryBytes System.Text.Encoding.ASCII.GetBytes(\r\n-- boundary --);var formdataTemplate \r\n-- boundary \r\nContent-Disposition: form-data; name\{0}\;\r\n\r\n{1};var formFields this.PostData.Where(m m.Type PostFileItemType.Text).ToList();foreach (var d in formFields){var textBytes System.Text.Encoding.UTF8.GetBytes(string.Format(formdataTemplate, d.Name, d.Value));memStream.Write(textBytes, 0, textBytes.Length);}const string headerTemplate Content-Disposition: form-data; name\{0}\; filename\{1}\\r\nContent-Type: {2}\r\n\r\n;var files this.PostData.Where(m m.Type PostFileItemType.File).ToList();foreach (var fe in files){memStream.Write(boundarybytes, 0, boundarybytes.Length);var header string.Format(headerTemplate, fe.Name, fe.FileName ?? System.Byte[], fe.ContentType ?? text/plain);var headerbytes System.Text.Encoding.UTF8.GetBytes(header);memStream.Write(headerbytes, 0, headerbytes.Length);memStream.Write(fe.FileBytes, 0, fe.FileBytes.Length);}memStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);request.ContentLength memStream.Length;HttpWebResponse response;try{using (var requestStream request.GetRequestStream()){memStream.Position 0;var tempBuffer new byte[memStream.Length];memStream.Read(tempBuffer, 0, tempBuffer.Length);memStream.Close();requestStream.Write(tempBuffer, 0, tempBuffer.Length);}response (HttpWebResponse)request.GetResponse();}catch (WebException webException){response (HttpWebResponse)webException.Response;}if (response null){throw new Exception(HttpWebResponse is null);}var responseStream response.GetResponseStream();if (responseStream null){throw new Exception(ResponseStream is null);}using (var streamReader new StreamReader(responseStream)){return streamReader.ReadToEnd();}}}public class PostFileItem{public PostFileItem(){this.Type PostFileItemType.Text;}public PostFileItemType Type { get; set; }public string Value { get; set; }public byte[] FileBytes { get; set; }public string Name { get; set; }public string FileName { get; set; }public string ContentType { get; set; }}public enum PostFileItemType{Text 0,File 1}public byte[] GetBinaryData(string filename){if(!File.Exists(filename)){return null;}try{FileStream fs new FileStream(filename, FileMode.Open, FileAccess.Read);byte[] imageData new Byte[fs.Length];fs.Read( imageData, 0,Convert.ToInt32(fs.Length));fs.Close();return imageData;}catch(Exception){return null;}finally{}} ashx文件部署 在B服务器上部署ashx文件接收数据ashx程序即一般处理程序HttpHandler一个httpHandler接受并处理一个http请求需要实现IHttpHandler接口这个接口有一个IsReusable成员一个待实现的方法ProcessRequest(HttpContextctx) 。.ashx程序适合产生供浏览器处理的、不需要回发处理的数据格式。 示例代码如下 % WebHandler LanguageC# ClassHandler %using System; using System.Web; using System.IO;public class Handler : IHttpHandler {public void ProcessRequest (HttpContext context) {if (context.Request.Files.Count 0){string strPath System.Web.HttpContext.Current.Server.MapPath(~/app_data/test/);string strName context.Request.Files[0].FileName;string extPath.GetExtension(strName);string filename HttpContext.Current.Request.QueryString[guid].ToString()Path.GetFileNameWithoutExtension(strName);if(ext!){filename filename ext;}context.Request.Files[0].SaveAs(System.IO.Path.Combine(strPath, filename));} }public bool IsReusable {get {return false;} }} 小结  ashx处理接收的数据后后续还需要配合实际的接口功能继续处理应用。另外对于ashx页面实际的应用则需要使用安全访问控制只有正常登录或提供合法访问令牌的用户才可以进行访问。 以上代码仅供参考欢迎大家指正再次感谢您的阅读
http://www.pierceye.com/news/73664/

相关文章:

  • 汕头网站设计浩森宇特wordpress汉化教程视频
  • 网站规划与设计期末大作业怎么做wordpress 英文
  • 网站网格布局网页制作步骤流程
  • 开网站建设wordpress iis建站
  • 网站开发课程设计参考文献沈阳男科医院哪家好哪个医院正规
  • 上海网站建设网站游戏做网站至少要花多少钱
  • 如何做属于自己的领券网站WordPress多域名无法登录
  • asp网站开发培训北京住建网站
  • 建网站石家庄湖北做网站平台哪家好
  • 陇南建设网站建立网站的方法
  • 制作网站的最大公司wordpress登录网站
  • 网站建站服务公司做网站软件是什么行业
  • 营销型网站建设明细报价表视觉传达设计网站
  • 购物网站的详细设计wordpress解压后怎么安装
  • 海南 网站制作wordpress搬家简书
  • 学摄影的网站有哪些项目运营方案计划书
  • 在线购物网站模板在线建站平台免费建网站
  • 做高清图的网站求个网站你懂我的意思2021
  • 哪有免费做网站注册10万公司实缴多少钱
  • 河南建设网站官网农业科技公司网站案例
  • 顺德网站建设制作网站开发技术题目
  • 西安航空城建设发展集团网站自学网站建设最快要多久
  • 怎么做晒鱼的网站晋江企业网站开发
  • 晋江网友交流区网站软件开发包括什么内容
  • 顺德网站百度广告官网
  • 中国建设报官方网站宁波网站推广宣传公司排名
  • 公司网站怎么规范管理的在线个人网页生成
  • 广州网站建设信科便宜wordpress 短信登录密码错误
  • 济宁市做网站做的比较好的小众网站
  • 大丰网站建设找哪家好国内建网站流程