zoukankan      html  css  js  c++  java
  • webclient上传数据到ashx服务

    1.上传参数

    UploadData()方法可以上传数据参数,需要将所要上传的数据拼成字符。

     // 创建一个新的 WebClient 实例.  
      WebClient myWebClient = new WebClient();  
      string postData = "Username=admin&Password=admin";  
      // 注意这种拼字符串的ContentType  
      myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");  
      // 转化成二进制数组  
      byte[] byteArray = Encoding.ASCII.GetBytes(postData);  
      // 上传数据,并获取返回的二进制数据.  
      byte[] responseArray = myWebClient.UploadData(远程服务URI,"POST",byteArray);  

    2.上传文件

      UploadFile()方法可以上传文件,只要获取文件上传文件的地址即可。
      String uriString = "远程服务URI";  
        
      // 创建一个新的 WebClient 实例.  
      WebClient myWebClient = new WebClient();  
        
      string fileName = @"文件地址";  
        
      // 直接上传,并获取返回的二进制数据.  
      byte[] responseArray = myWebClient.UploadFile(uriString,"POST",fileName);  

    3.上传参数和文件

    使用UploadData()方法,把文件拼接成byte[] data数据Post过去,就可以达到同样的效果。

    System.IO.FileStream zipfs = new System.IO.FileStream(ZipPath, FileMode.Open, FileAccess.Read);//testdata.zip
    byte[] byteZipFile = new byte[zipfs.Length];
    zipfs.Read(byteZipFile, 0, Convert.ToInt32(zipfs.Length));
    zipfs.Close();
    string formStart = "--margin " +
    "Content-Disposition: form-data; name="FileData"; FileName="" + ZipName + "" " +
    "Content-Type: application/octet-stream ";//; LayerName="GDGA_ZD"
    string formEnd = " --margin-- ";
    byte[] btStart = Encoding.UTF8.GetBytes(formStart);
    byte[] btEnd = Encoding.UTF8.GetBytes(formEnd);
    byte[] bytes = new byte[btStart.Length + byteZipFile.Length+ btEnd.Length];
    btStart.CopyTo(bytes, 0);
    byteZipFile.CopyTo(bytes, btStart.Length);
    btEnd.CopyTo(bytes, btStart.Length + byteZipFile.Length);
    WebClient client = new WebClient();
    client.Encoding = System.Text.Encoding.UTF8;
    client.Headers.Add("Content-Type", "multipart/form-data; boundary=margin");
    client.Headers.Add("ContentLength", bytes.Length.ToString());
    client.UploadData(new Uri("你的服务地址"), "POST", bytes);
    4.上传多文件(不同类型文件)
    同理,使用UploadData()方法,把不同的文件拼接成byte[],再合并 data数据Post过去

    例如:


    // 生成需要上传的二进制数组
    CreateBytes cb = new CreateBytes();
    // 所有表单数据
    ArrayList bytesArray = new ArrayList();
    //zip文件流
    FileStream zipfs = new FileStream(ZipPath, FileMode.Open,FileAccess.Read, FileShare.Read);
    string ContentType = "application/octet-stream";
    byte[] zipfileBytes = new byte[zipfs.Length];
    zipfs.Read(zipfileBytes, 0, Convert.ToInt32(zipfs.Length));
    bytesArray.Add(cb.CreateFieldData("FileData", ZipPath, ContentType, zipfileBytes));
    //xml文件流
    FileStream xmlfs = new FileStream(Checkxml, FileMode.Open, FileAccess.Read, FileShare.Read);
    string contentType = "text/xml";
    byte[] xmlfileBytes = new byte[xmlfs.Length];
    xmlfs.Read(xmlfileBytes, 0, Convert.ToInt32(xmlfs.Length));
    bytesArray.Add(cb.CreateFieldData("xmlData", Checkxml, contentType, xmlfileBytes));
    // 合成所有文件并生成二进制数组
    byte[] bytes = cb.JoinBytes(bytesArray);

    WebClient client = new WebClient();
    client.Encoding = System.Text.Encoding.UTF8;//定义对象语言
    client.Headers.Add("Content-Type", cb.ContentType);
    client.Headers.Add("ContentLength", bytes.Length.ToString());
    client.UploadData(new Uri("上传服务地址"), "POST", bytes);

  • 相关阅读:
    批处理 bat 查询局域网内在线电脑IP
    svn忽略不需要同步的文件夹或文件
    脚本设置IP bat 命令行设置自动获取IP和固定IP
    Lua中使用状态机FSM简单例子
    Lua 数组排序 table.sort的注意事项
    lua中怎么替换掉字符串中的$^特殊字符?
    C#应用程序隐藏调用bat脚本
    Oracle 基础教程
    Python编程
    利用Python进行数据分析(九)NumPy高级应用
  • 原文地址:https://www.cnblogs.com/zxtceq/p/10715796.html
Copyright © 2011-2022 走看看