UploadStringAsync的返回值()。? [英] Return value of UploadStringAsync().?

查看:681
本文介绍了UploadStringAsync的返回值()。?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是正确或不正确的,我不知道,但我想知道是否有可能重返岗位方法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?

推荐答案

您可以使用<一个href="http://msdn.microsoft.com/en-us/library/system.net.webclient.uploadstringcompleted%28v=vs.110%29.aspx"相对=nofollow> 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

这篇关于UploadStringAsync的返回值()。?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆