上传文件到webservice并下载它返回的文件
本文关键字:文件 返回 下载 webservice | 更新日期: 2023-09-27 18:03:30
嗨,我正在使用GZipStream压缩xml文件并将其上传到一个webservice,该webservice将返回我必须下载的GZipStream。我在c#中使用WebClient使用以下方法,但它抛出异常"WebClient不支持并发I/O操作"。
byte[] data = Encoding.ASCII.GetBytes(xml);
MemoryStream input = new MemoryStream(data);
MemoryStream output = new MemoryStream();
GZipStream zip = new GZipStream(output, CompressionMode.Compress);
input.WriteTo(zip);
byte[] gzipStream = output.ToArray();
//Constructing Request
var postClient = new WebClient();
Uri uri = new Uri(url);
postClient.UploadDataAsync(uri, gzipStream);
var resStream = new GZipStream(postClient.OpenRead(url),CompressionMode.Decompress);
var reader = new StreamReader(resStream);
var textResponse = reader.ReadToEnd();
return textResponse;
请帮帮我。
在这种情况下不应该使用async
byte[] data = Encoding.ASCII.GetBytes(xml);
MemoryStream input = new MemoryStream(data);
MemoryStream output = new MemoryStream();
GZipStream zip = new GZipStream(output, CompressionMode.Compress);
input.WriteTo(zip);
byte[] gzipStream = output.ToArray();
//Constructing Request
var postClient = new WebClient();
Uri uri = new Uri(url);
postClient.UploadData(uri, gzipStream);
var resStream = new GZipStream(postClient.OpenRead(url),CompressionMode.Decompress);
var reader = new StreamReader(resStream);
var textResponse = reader.ReadToEnd();
return textResponse;
或者你应该等待async操作(如果你的方法是async):
byte[] data = Encoding.ASCII.GetBytes(xml);
MemoryStream input = new MemoryStream(data);
MemoryStream output = new MemoryStream();
GZipStream zip = new GZipStream(output, CompressionMode.Compress);
input.WriteTo(zip);
byte[] gzipStream = output.ToArray();
//Constructing Request
var postClient = new WebClient();
Uri uri = new Uri(url);
await postClient.UploadDataAsync(uri, gzipStream);
var resStream = new GZipStream(postClient.OpenRead(url),CompressionMode.Decompress);
var reader = new StreamReader(resStream);
var textResponse = reader.ReadToEnd();
return textResponse;