zoukankan      html  css  js  c++  java
  • 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. }  


    代码可能有误

  • 相关阅读:
    Vs 开发时无法断点问题
    VS启动调试速度异常的缓慢问题
    vs2017 调试时 浏览器关闭不想中断调试
    聚簇索引和非聚簇索引
    java实现阿里云短信服务发送验证码
    mysql定时器
    token,加密,签名
    Redis更新缓存同步数据库的理解
    Token
    解决哈希冲突的方法
  • 原文地址:https://www.cnblogs.com/soundcode/p/2966011.html
Copyright © 2011-2022 走看看