C# 将文件上载到webservice并下载它返回的文件

C# 将文件上载到webservice并下载它返回的文件,c#,webclient,C#,Webclient,您好,我正在使用GZipStream压缩一个xml文件,并将其上载到一个Web服务,该服务将返回一个我必须下载的GZipStream。我在C#中使用WebClient时使用了以下方法,但它引发了异常“WebClient不支持并发I/O操作” 请在这方面帮助我。在这种情况下不应使用异步: byte[] data = Encoding.ASCII.GetBytes(xml); MemoryStream input = new MemoryStream(data);

您好,我正在使用GZipStream压缩一个xml文件,并将其上载到一个Web服务,该服务将返回一个我必须下载的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.UploadData(uri, gzipStream);
                var resStream = new GZipStream(postClient.OpenRead(url),CompressionMode.Decompress);
            var reader = new StreamReader(resStream);
            var textResponse = reader.ReadToEnd();
            return textResponse;
或者您应该等待异步操作(如果您的方法是异步的):


UploadDataAsync()可能存在问题?try UploadData()执行POST后,您确定需要向同一URI发出GET请求,还是要读取POST响应?将gzip文件发布到Web服务后,该服务将处理该文件并返回我下载的另一个gzip文件作为响应。
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;
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;