Running two calls to WebClient.UploadStringAsync afterwards

When you call WebClient.UploadStringAsync twice, without waiting for the WebClient.UploadStringCompleted event, the following exception is thrown:

"WebClient does not support concurrent I / O"

This does not seem to be supported.

The reason for wanting to run multiple HTTP POST requests without having to wait for a previous response is due to performance; I want to avoid a round trip. Is there a way around this limitation?

+3
source share
1 answer

You need to use multiple instances WebClient.

 var wc1 = new WebClient();
 wc1.UploadStringCompleted += (s, args) => {
    // do stuff when first upload completes
 }
 wc1.UploadString(uri1,str1);

 var wc2 = new WebClient();
 wc2.UploadStringCompleted += (s, args) => {
    // do stuff when second upload completes
    // might happen before first has completed
 }
 wc2.UploadString(uri2,str2);
+8
source

Source: https://habr.com/ru/post/1728903/


All Articles