• asp.net post任何数据类型流到网站当当网API传XML文件


    下面的代码可以post任何类型文件,主要是把数据流统一转换为base64格式.
    
    //------------------------------发送部分--------------------------------------------------
    
    
    
            string url = "http://localhost/im/upfile?aa=5";//发送到的页面的地址
    
             HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    
    
    
             //读取一个图片
    
             FileStream fs = new FileStream(Server.MapPath("~/NET基础知识.doc"), 
                  System.IO.FileMode.Open,     
    
                  System.IO.FileAccess.Read);
    
             byte[] filecontent = new byte[fs.Length];
    
             fs.Read(filecontent, 0, filecontent.Length);
    
             fs.Close();
    
             fs.Dispose();
    
    
    
             //将图片转换成base64编码的流
    
             string a = Convert.ToBase64String(filecontent);       
    
       
    
             //读取base64编码流,发送
    
             byte[] requestBytes = System.Text.Encoding.Default.GetBytes(a);
    
    
    
             req.Method = "POST";
    
             req.ContentType = "application/x-www-form-urlencoded";
    
             req.ContentLength = requestBytes.Length;
    
             Stream requestStream = req.GetRequestStream();
    
             requestStream.Write(requestBytes, 0, requestBytes.Length);
    
             requestStream.Close();
    
    
    
             //接收返回参数,到string backstr
    
             HttpWebResponse res = (HttpWebResponse)req.GetResponse();
    
             StreamReader sr = new StreamReader(res.GetResponseStream(), 
                System.Text.Encoding.Default);
    
             string backstr = sr.ReadToEnd();
    
             sr.Close();
    
             res.Close();
    
             //输出参数
    
             Response.Write(backstr);
    
    //-----------------------------------------------接收部分-----------------------------------
    
    
             //接收到的参数
    
             string bb= Request.QueryString["aa"];
    
             Encoding myEncoding = Encoding.GetEncoding("utf-8");
    
    
    
             //接收传递过来的数据流
    
             Stream resStream = Request.InputStream;
    
    
    
             byte[] filecontent = new byte[resStream.Length];
    
             //将数据流读入byte数组
    
             resStream.Read(filecontent, 0, filecontent.Length);
    
             //数组转换为string以便转换base64使用
    
             string a = myEncoding.GetString(filecontent);
    
             //将string读取base64解密到byte数组
    
             byte[] filecontent2 = Convert.FromBase64String(a);
    
             //写入目录
    
             File.WriteAllBytes(Server.MapPath("~/"+bb+".doc"), filecontent2);
    
             //返回值
    
             Response.Write("ok");
    
             Response.End();
    

      

  • 相关阅读:
    windows 远程连接报错
    这30类CSS选择器 你必须记在脑袋里!
    Print to the console in django without UnicodeEncodeErrors
    基于md5的解决js,css缓存问题for django
    Django开发经验
    vscode常用的插件
    点击时添加不同的类
    jQuery 点击空白,隐藏元素
    多功能日历插件:Kalendae
    jQuery中的data()方法
  • 原文地址:https://www.cnblogs.com/xiaofengfeng/p/3016764.html
Copyright © 2020-2023  润新知