• REST接口POST方法发送文件到服务器(C#)


    1. using System;  
    2. using System.IO;  
    3. using System.Net;  
    4. using System.Text;  
    5.   
    6. namespace xxxx  
    7. {  
    8.     public class WebRequestPostExample  
    9.     {  
    10.         public static void Main()  
    11.         {  
    12.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http###files/Cygwin.pdf");  
    13.             FileStream fs = new FileStream(@"D:\Cygwin.ISO", FileMode.Open, FileAccess.Read);  
    14.             Byte[] bytes = new Byte[10240];  
    15.             request.Method = "POST";  
    16.             request.Proxy = null;  
    17.             request.Headers.Add("XXX""XXX");  
    18.             request.ContentType = "application/octet-stream";  
    19.             Stream dataStream = request.GetRequestStream();  
    20.             int count = fs.Read(bytes, 0, 10240);  
    21.             while (count != 0)  
    22.             {  
    23.                 dataStream.Write(bytes, 0, count);  
    24.                 count = fs.Read(bytes, 0, 10240);  
    25.             }  
    26.             fs.Close();  
    27.             dataStream.Close();  
    28.             try  
    29.             {  
    30.                 HttpWebResponse response = (HttpWebResponse)request.GetResponse();  
    31.                 response.Close();  
    32.             }  
    33.             catch (System.Exception ex)  
    34.             {  
    35.                 Console.WriteLine("!!!!!!ERROR!!!!!!!!" + ex.ToString() + "!!!!!!!!ERROR!!!!!!!!");  
    36.             }  
    37.   
    38.   
    39.   
    40.   
    41.         }  
    42.     }  
    43. }  


    代码可能有误

  • 相关阅读:
    ES中对应的SQL的count(distinct 列名) java实现
    maven使用
    自旋锁
    Java手写死锁并用jps和jstack查看
    已知二叉树前序和中序,算法写出后续遍历的结果
    Idea里搭建SpringMVC项目,部署的包下没有lib
    Spring配置文件中关于bean标签的id属性和name属性的说明
    ORA-12519: TNS:no appropriate service handler found 解决
    springmvc常用注解标签详解
    Spring/SpringMvc 配置文件常用标签解释
  • 原文地址:https://www.cnblogs.com/soundcode/p/2966011.html
Copyright © 2020-2023  润新知