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. }  


代码可能有误

转载于:https://www.cnblogs.com/soundcode/archive/2013/03/18/2966011.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值