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 3429212
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:03:14+00:00 2026-05-18T07:03:14+00:00

I have a class that proccesses n number of http requests / repsonses asynchronously

  • 0

I have a class that proccesses n number of http requests / repsonses asynchronously and recursively. I was pretty happy with my class until I realised that I may have coded myself into a bit of a hole. Essentially I would like to raise an event after each response is read to comfirm if the application has proceeded to the next or finished early for whatever reason. I will admit I am really new to asynchronous programming so I am using the Async Enumerator class to help me out. I don’t mind having to rewrite this method or indeed the entire class as long as the base functionality remains the same, that is it takes n number of requests and reads through them sequencially.

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 Webtext.Implementations;
using Webtext.Interfaces;
using Wintellect.Threading.AsyncProgModel;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Webtext.Implementations
{
public class SMSSender : Interfaces.IOperator
{
    private HttpWebRequest httpRequester;
    private HttpWebResponse httpResponse;
    private IOperatorRequestCollection requestCollection;
    private IUrlBuilder urlBuilder;
    private AsyncEnumerator asyncEnum;
    private CookieContainer cookieContainer;
    CookieCollection cookieCollection;

    public SMSSender()
    {
        cookieContainer = new CookieContainer();
        cookieCollection = new CookieCollection();
    }

    public void Send(string UserName, string Password, string MessageRecipient, string Message, OperatorList Operator)
    {
        urlBuilder = new UrlBuilder();

        requestCollection = new OperatorRequestCollection { OperatorRequestList = urlBuilder.GetUrlRequests(UserName, Password, MessageRecipient, Message, Operator) };

        ProcessRequests();
    }

    private void ProcessRequests()
    {
        asyncEnum = new AsyncEnumerator();
        asyncEnum.BeginExecute(GetData(asyncEnum, requestCollection.GetCurrentRequest()), asyncEnum.EndExecute);
    }

    private IEnumerator<Int32> GetData(AsyncEnumerator asyncEnum, IOperatorRequest request)
    {
        httpRequester = (HttpWebRequest)WebRequest.Create(request.RequestUrl);
        httpRequester.AllowAutoRedirect = true;
        httpRequester.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6";

        if (httpRequester.CookieContainer == null)
            httpRequester.CookieContainer = cookieContainer;

        httpRequester.BeginGetResponse(asyncEnum.End(), null);
        yield return 1;

        httpResponse = (HttpWebResponse)httpRequester.EndGetResponse(asyncEnum.DequeueAsyncResult());

        if (httpResponse.StatusCode == HttpStatusCode.OK)
        {
            // Create the stream, encoder and reader.
            Stream responseStream = httpResponse.GetResponseStream();
            Encoding streamEncoder = Encoding.UTF8;
            StreamReader responseReader = new StreamReader(responseStream, streamEncoder);
            CheckRequest(request, responseReader.ReadToEnd());
        }
    }

    private void CheckRequest(IOperatorRequest Request, string Response)
    {
        if (Response.Contains(Request.SuccessMessage))
        {
            AddCookiesToRequest();

            bool lastCallFinished = false;
            if (requestCollection.GetCurrentRequest() != requestCollection.GetLastRequest())
            {
                requestCollection.MoveNextToCurrent();
                ProcessRequests();
            }
            else
            {
                if (lastCallFinished == false)
                {
                    ProcessRequests();
                    lastCallFinished = true;
                }
            }
        }
    }

    private void AddCookiesToRequest()
    {
        // This method is O2 specific at the moment. I do not know if this sort of methods
        // will be needed for the others so for the moment it stays as is.
        cookieCollection.Add(httpResponse.Cookies);
        foreach (Cookie cookie in cookieCollection)
        {
            cookie.Path = "/";
            cookie.Domain = "o2online.ie";
        }
        cookieContainer.Add(httpResponse.ResponseUri, cookieCollection);
    }
}

}

EDIT: This edit is to explain what I am looking to do:

I am essentially making 5 (This could be n number) requests using HttpWebRequest. I am doing this recursively as each new request depends on the response gotten from the previous request. This functionality works currently.

The issue I am having is, as the responses are coming back asynchronously I have no way of getting back to the UI thread to let the UI know how far along I am. If any of the response fails the collection of requests fails as a whole so it would be nice to know which request failed as well as why.

I would also like to have another async operation working that can take in this data and display it with an indeterminate progress bar. That way the user is getting real time data from the UI as to how far along the operation is and if it failed why. I tried a backround worker here but because the first request is asyncrhonous and DO_WORK is a synchronous method the WORK_Completed event of a backround worker is called before the work is actually finished.

As for the AsyncEnumerator. It essentially uses an enumerator to specify what to do after yield (It calls move next when HttpEndGetResponse is ready). This eleviates some of the issues in that I can marshal an event back to the UI thread to talk to the progress worker but it is still messy so I have not yet implemented it.

What I would like to do is fire off a SEND method on a SENDER object, then fire off a SHOW_PROGRESS method on a different async call using a PROGRESSDISPLAY object. As each iteration of the Send method yields send data back to the UI which sends it off the PROGRESSDISPLAY object and allow it to update until the final call which would tell PROGRESSDISPLAY to close.

  • 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-18T07:03:15+00:00Added an answer on May 18, 2026 at 7:03 am

    In the end it seemed that a rewrite is the best option. The new class has methods that will be fired back to the UI thread. It wasn’t so much that this class was wrong. In fact it works fine. The issue lay with the fact that I never allowed for a response mechinism at all. I will update this post with the new class once it is finished.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a backend server (Jetty) that processes HTTP requests and interacts with a
Suppose I have a class that processes some data: class SomeClass { public: void
I have class that extend FragmentActivity in it I add fragment to layout as
I have class that looks like this: class A { public: class variables_map vm
I have class that represents users. Users are divided into two groups with different
I have class grades that I need to check if a specific grade is
I have class A such that: class A { static int i; A(); f1();
I have a class that I instantiate to save or load xml data. For
I have a Class that has a private variable with a public setter/getter function:
I have a class that has several member classes as attributes. The constructor of

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.