zoukankan      html  css  js  c++  java
  • Asp.Net获取远程数据并保存为文件的简单代码

    <%@Page Language="C#" Debug="true"%>
    <%@Import NameSpace="System.Net"%>
    <%@Import NameSpace="System.IO"%>
    <script language="C#" runat="Server">
    string sException=null;
    public bool GetHttpFile(string sUrl,string sSavePath){
            bool bRslt=false;
            WebResponse oWebRps=null;
            WebRequest oWebRqst=WebRequest.Create(sUrl);
            oWebRqst.Timeout=50000;
            try{
                    oWebRps=oWebRqst.GetResponse();
            }
            catch(WebException e){
                    sException=e.Message.ToString();
            }
            catch(Exception e){
                    sException=e.ToString();
            }
            finally{
                    if(oWebRps!=null){
                            BinaryReader oBnyRd=new BinaryReader(oWebRps.GetResponseStream(),System.Text.Encoding.GetEncoding("GB2312"));
                            int iLen=Convert.ToInt32(oWebRps.ContentLength);
                            FileStream oFileStream;
                            try{
                                    if(File.Exists(Request.MapPath("RecievedData.tmp"))){
                                            oFileStream=File.OpenWrite(sSavePath);
                                    }
                                    else{
                                            oFileStream=File.Create(sSavePath);
                                    }
                                    oFileStream.SetLength((Int64)iLen);
                                    oFileStream.Write(oBnyRd.ReadBytes(iLen),0,iLen);
                                    oFileStream.Close();
                            }
                            finally{
                                    oBnyRd.Close();
                                    oWebRps.Close();
                            }
                            bRslt=true;
                    }
            }
            return bRslt;
    }
    </script>
    <%
    TimeSpan oStartTime=DateTime.Now.TimeOfDay;
    Response.Write(GetHttpFile("http://www.spbdev.com/download/DotNetInfo1.0.rar",Request.MapPath("RecievedFile.rar")));
    Response.Write("<br><br>\r\n执行时间:" + DateTime.Now.TimeOfDay.Subtract(oStartTime).TotalMilliseconds.ToString() + " 毫秒");
    %>
  • 相关阅读:
    使用idea进行远程调试
    map根据属性排序、取出map前n个
    编写shell脚本一键启动 重启 停止springboot jar包
    IDEA给类和方法配置注释模板(参数换行显示)
    springboot文件上传报错
    WebMvcConfigurer 与 WebMvcConfigurationSupport避坑指南
    WebMvcConfigurerAdapter详解和过时后的替代方案
    springboot上传文件过大,全局异常捕获,客户端没有返回值
    javascript中Math.random()产生随机数总结
    关于微信中的localStorage及使用cookie的解决方案
  • 原文地址:https://www.cnblogs.com/JensonBin/p/1988199.html
Copyright © 2011-2022 走看看