zoukankan      html  css  js  c++  java
  • REST接口POST方法发送文件到服务器(C#)

    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    
    namespace xxxx
    {
        public class WebRequestPostExample
        {
            public static void Main()
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http###files/Cygwin.pdf");
                FileStream fs = new FileStream(@"D:\Cygwin.ISO", FileMode.Open, FileAccess.Read);
                Byte[] bytes = new Byte[10240];
                request.Method = "POST";
                request.Proxy = null;
                request.Headers.Add("XXX", "XXX");
                request.ContentType = "application/octet-stream";
                Stream dataStream = request.GetRequestStream();
                int count = fs.Read(bytes, 0, 10240);
                while (count != 0)
                {
                    dataStream.Write(bytes, 0, count);
                    count = fs.Read(bytes, 0, 10240);
                }
                fs.Close();
                dataStream.Close();
                try
                {
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    response.Close();
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine("!!!!!!ERROR!!!!!!!!" + ex.ToString() + "!!!!!!!!ERROR!!!!!!!!");
                }
    
    
    
    
            }
        }
    }


    代码可能有误

  • 相关阅读:
    程序11
    程序9
    程序10
    程序8
    提示框
    程序6
    莫名其妙的“网线未连接”
    UI自动化测试笔记(2)
    UI自动化测试笔记(1)
    三年开发项目经验总结
  • 原文地址:https://www.cnblogs.com/gitran/p/3644151.html
Copyright © 2011-2022 走看看