webclient上传数据到ashx服务

时间:2022-05-15 15:50:36

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\r\n" +
"Content-Disposition: form-data; name=\"FileData\"; FileName=\"" + ZipName + "\"\r\n" +
"Content-Type: application/octet-stream\r\n\r\n";//; LayerName=\"GDGA_ZD\"
string formEnd = "\r\n--margin--\r\n";
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);