Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1111013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:28:54+00:00 2026-05-17T02:28:54+00:00

I’m trying to make a simple class for my REST calls so I won’t

  • 0

I’m trying to make a simple class for my REST calls so I won’t have the same code in multiple places in my application.

The problem is, I don’t know how to notify an object that called the UploadStringAsync() method from inside the UploadStringCompleted() event handler.

Here is my class, I have added comments to mark the place where I want to notify the object from which the Post method has been called:

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MyProject.Utils
{
    public class Rest
    {
        private WebClient client = new WebClient();

        private void Init()
        {
            client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);
        }

        public void Post(string uri, string postRequest, object objectToNotify)
        {
            Init();
            client.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            client.UploadStringAsync(new Uri(uri,
                UriKind.RelativeOrAbsolute), "POST", postRequest, objectToNotify);
        }

        private void client_UploadStringCompleted(object sender,
           UploadStringCompletedEventArgs e)
        {
            // here I would like to notify an object from 
            // which the Post() method has been called
            // I don't know how to do this as this method has no idea
            // which object called the method
            // this doesn't work:
            // (e.UserState.GetType())e.UserState.RestCallback(e);
        }
    }
}

EDIT:

My modified code after Jacob’s suggestions:

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;

namespace MyProject.Utils
{
    public class Rest
    {
        private WebClient client = new WebClient();

        private void Init()
        {
            client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);
        }

        public void Post(
            string uri, string postRequest,
            Action<UploadStringCompletedEventArgs> callback)
        {
            HtmlPage.Window.Alert("bbb");
            client.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            client.UploadStringAsync(new Uri(uri,
                UriKind.RelativeOrAbsolute), "POST", postRequest, callback);
        }

        private void client_UploadStringCompleted(object sender,
           UploadStringCompletedEventArgs result)
        {
            HtmlPage.Window.Alert("aaa");
            var callback = (Action<UploadStringCompletedEventArgs>)result.UserState;
            callback(result);
        }
    }
}

This is how I’m calling the Post method:

    string postRequest = "id=5":
    Rest restClient = new Rest();
    restClient.Post("http://mywebsite.com/", postRequest, RestCallback);

    .......................

    private void RestCallback(UploadStringCompletedEventArgs result)
    {
        if (result.Error != null)
        {
            ContactFormSuccess.Text = "There was an error sending email message.";
        }
        else
        {
            ContactFormSuccess.Text = "Email message successfully sent.";
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T02:28:54+00:00Added an answer on May 17, 2026 at 2:28 am

    You could modify your Rest class so that it’s given a callback delegate rather than the object to notify. It could look something like this:

    public void Post(
        string uri, string postRequest, 
        Action<UploadStringCompletedEventArgs> callback)
    {
        Init();
        client.Headers["Content-Type"] = "application/x-www-form-urlencoded";
        client.UploadStringAsync(
            new Uri(uri, UriKind.RelativeOrAbsolute), 
            "POST", postRequest, callback);
    }
    
    private void client_UploadStringCompleted(object sender,
       UploadStringCompletedEventArgs e)
    {
        var callback = (Action<UploadStringCompletedEventArgs>)e.UserState;
        callback(e);
    }
    

    Then, when you make the request, the call would look something like this:

    var restClient = new Rest();
    restClient.Post("http://your/url", "stuff", handleResult);
    
    
    // ...
    
    private void handleResult(UploadStringCompletedEventArgs result)
    {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I am trying to loop through a bunch of documents I have to put
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I have a bunch of posts stored in text files formatted in yaml/textile (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.