Return value of UploadStringAsync().?

后端 未结 3 795
臣服心动
臣服心动 2021-01-21 21:01

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?

<
相关标签:
3条回答
  • 2021-01-21 21:34

    From .NET 4.5 we can use UploadStringTaskAsync to retrieve return value directly.

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

    ref: MSDN

    0 讨论(0)
  • 2021-01-21 21:36

    There is nothing to return from that function. You've already set up an event handler for UploadStringCompleted, you can get the result of the action in the handler.

    This is the signature:

    public delegate void UploadStringCompletedEventHandler(
        Object sender,
        UploadStringCompletedEventArgs e
    )
    

    The second parameter has the information you need: UploadStringCompletedEventArgs, the Result property contains the server's response.

    0 讨论(0)
  • 2021-01-21 21:38

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

    Attach

    client.UploadStringCompleted 
                   += new UploadStringCompletedEventHandler (UploadStringCallback2);
    

    Use:

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

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

    0 讨论(0)
提交回复
热议问题