UploadStringAsync()的返回值。?

时间:2013-12-04 11:49:05

标签: c# .net windows-phone return webclient

我的问题是正确的还是不正确我不知道,但我想知道是否可以使用WebClient返回post方法的UploadStringAsync()的值?

    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, result);的值,请告诉我?

3 个答案:

答案 0 :(得分:4)

您可以使用UploadStringCompleted事件并在事件处理程序中获取结果。一旦上传完成(失败或成功),就会引发事件。

附加

client.UploadStringCompleted 
               += new UploadStringCompletedEventHandler (UploadStringCallback2);

使用:

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

如果您想要返回上传结果,可以等待事件像here一样被提升  使用AutoResetEvent

答案 1 :(得分:4)

在.NET 4.5中,我们可以使用UploadStringTaskAsync直接检索返回值。

string rspBody = await client.UploadStringTaskAsync(uri, "POST");

ref:MSDN

答案 2 :(得分:1)

该功能无法返回任何内容。您已经为UploadStringCompleted设置了事件处理程序,您可以在处理程序中获取操作的结果。

这是签名:

public delegate void UploadStringCompletedEventHandler(
    Object sender,
    UploadStringCompletedEventArgs e
)

第二个参数包含您需要的信息:UploadStringCompletedEventArgsResult属性包含服务器的响应。