UploadStringAsync的返回值()。?返回值、UploadStringAsync

2023-09-03 07:46:47 作者:星辰.

我的问题是正确或不正确的,我不知道,但我想知道是否有可能重返岗位方法UploadStringAsync()使用Web客户端的价值?

My question is correct or incorrect I don't know, but I would like to know if is it possible to return the value of UploadStringAsync() of post methods using WebClient?

    string serviceURL = REST_URI + servicePath;
    Uri URI = new Uri(serviceURL);
    System.Net.WebClient webClient = new WebClient();
    webClient.Headers["ContentType"] = "application/json";
    webClient.Headers["Accept"] = "application/json";
    webClient.UploadStringCompleted += this.sendPostCompleted;
    webClient.UploadStringAsync(URI, HTTP_POST, result);
    return ??;

如果我们能回到的值UploadStringAsync(URI,HTTP_POST,结果); 请让我知道

If we can return the value of UploadStringAsync(URI, HTTP_POST, result); please let me know?

推荐答案

您可以使用 UploadStringCompleted 事件,并获得导致事件处理程序。一旦上传完成(失败或成功)事件出现。

You can use UploadStringCompleted event and get result in event handler. Once upload completes (failed or succeed) event is raised.

连接

client.UploadStringCompleted 
               += new UploadStringCompletedEventHandler (UploadStringCallback2);

使用:

void UploadStringCallback2(object sender, UploadStringCompletedEventArgs e)
{
//e.Result use it
}

如果你想返回上传您可以等待事件的结果,在这里提出类似  使用的AutoResetEvent

If you want to return result of the upload you can wait for event to be raised like here using AutoResetEvent