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


    代码可能有误

  • 相关阅读:
    UVA 1557
    基于角色的权限设计(二)
    用DirectShow实现视频採集-流程构建
    oracle中schema指的是什么?
    GCC 命令行具体解释
    希尔排序
    单点登录SSO的实现原理
    济南最新公交线路一览(BRT)
    编写你自己的单点登录(SSO)服务
    C和指针 (pointers on C)——第五章:操作符和表达式
  • 原文地址:https://www.cnblogs.com/soundcode/p/2966011.html
Copyright © 2011-2022 走看看