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

  • SEARCH
  • Home
  • 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 6769967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:15:23+00:00 2026-05-26T15:15:23+00:00

I am trying to use the following code, but it does not work when

  • 0

I am trying to use the following code, but it does not work when exception occurs. Can anyone help me out on this? i am trying to throw web exception in the fetchresponse().catch(). is it possible to return diff type of data in return(more than just a string out).

IObservable<string> tempReturnData = null;
        try
        {
            // create http web request
            HttpWebRequest WSrequest = (HttpWebRequest)WebRequest.Create(WebURL);
            // instantiate Request class object
            RequestState rs = new RequestState();
            rs.Request = WSrequest;
            // Lock current webrequest object.. incase of retry attempt 
            lock (WSrequest)
            {
                rs.Request.ContentType = "application/x-www-form-urlencoded";
                rs.Request.Method = "POST";
                rs.Request.Timeout = 100;
                // bug in .net that closes the connection prior to it being finished 
                rs.Request.KeepAlive = false;
                rs.Request.ProtocolVersion = HttpVersion.Version10;
                // async pattern get request
                var fetchRequestStream = Observable.FromAsyncPattern<Stream>(rs.Request.BeginGetRequestStream, rs.Request.EndGetRequestStream);
                // async pattern get response
                var fetchResponse = Observable.FromAsyncPattern<WebResponse>(rs.Request.BeginGetResponse, rs.Request.EndGetResponse);
                // 
                tempReturnData = (from tempResult in fetchRequestStream() select tempResult).SelectMany(stream =>
                    {
                        using (var writer = new StreamWriter(stream)) writer.Write(postData);
                        // here i wants to catch web exception in fetchResponse()   FYI : in my function i am returning IObservable<string>
                        return fetchResponse().Catch(Observable.Empty<WebResponse>()).Retry(5);
                    }).Select(result =>
                    {
                        lock (rs)
                        {
                            rs.Response = (HttpWebResponse)result;

                            string s = "";
                            // if response is ok then read response stream data
                            if (rs.Response.StatusCode == HttpStatusCode.OK)
                            {
                                using (StreamReader reader = new StreamReader(rs.Response.GetResponseStream())) s = reader.ReadToEnd();
                            }
                            // Error case if error occurs then try after random time period
                            else
                            {
                                if (Attempt < appConfig.PSPRequestAttempt)
                                {
                                    Attempt++;
                                    RandomisePost(WebURL, postData, Attempt);
                                }
                            }
                            return s;
                        }
                    }); // get response stream data
                return tempReturnData;
            }
        }
        catch (Exception ex)
        {
            // Debug.WriteLine("Exception Occurs   " + ex.Message);
            return null;
        }
  • 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-26T15:15:24+00:00Added an answer on May 26, 2026 at 3:15 pm

    I think you’re trying too hard to mix Rx and non-Rx code together. Try to make your Rx code work in terms of a simple Rx query like this:

        return
            from st in fetchRequestStream()
            from rp in postDataAndFetchResponse(st)
            from s in fetchResult(rp)
            select s;
    

    This query relies on three functions that look like this: Func<X, IObservable<Y>>.

    You can then handle all of the retrying and exception handling using standard RX operators. No need to do any funky “randomise” calls!

    You can call it like this:

    FetchStringFromPost("url", "postData")
    .Retry(3)
    .Subscribe(s => { }, ex =>
    {
        /* Exceptions here! */
    }, () => { });
    

    Here’s the full code:

    public IObservable<string> FetchStringFromPost(string WebURL, string postData)
    {
        var request = (HttpWebRequest)WebRequest.Create(WebURL);
        request.ContentType = "application/x-www-form-urlencoded";
        request.Method = "POST";
        request.Timeout = 100;
        request.KeepAlive = false;
        request.ProtocolVersion = HttpVersion.Version10;
    
        var fetchRequestStream = Observable
            .FromAsyncPattern<Stream>(
                request.BeginGetRequestStream,
                request.EndGetRequestStream);
    
        var fetchResponse = Observable
            .FromAsyncPattern<WebResponse>(
                request.BeginGetResponse,
                request.EndGetResponse);
    
        Func<Stream, IObservable<HttpWebResponse>> postDataAndFetchResponse = st =>
        {
            using (var writer = new StreamWriter(st))
            {
                writer.Write(postData);
            }
            return fetchResponse().Select(rp => (HttpWebResponse)rp);
        };
    
        Func<HttpWebResponse, IObservable<string>> fetchResult = rp =>
        {
            if (rp.StatusCode == HttpStatusCode.OK)
            {
                using (var reader = new StreamReader(rp.GetResponseStream()))
                {
                    return Observable.Return<string>(reader.ReadToEnd());
                }
            }
            else
            {
                var msg = "HttpStatusCode == " + rp.StatusCode.ToString();
                var ex = new System.Net.WebException(msg,
                    WebExceptionStatus.ReceiveFailure);
                return Observable.Throw<string>(ex);
            }
        };
    
        return
            from st in fetchRequestStream()
            from rp in postDataAndFetchResponse(st)
            from s in fetchResult(rp)
            select s;
    }
    

    When I tested the above code I tried to call FetchStringFromPost("http://www.microsoft.com", "foo").Materialize() and got back this:

    WebException

    Seems to work like a treat. Let me know how you go.

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

Sidebar

Related Questions

I'm trying to use the following code but it's returning the wrong day of
I'm trying to use the following code (poorly written, but it's just a proof
I am trying to use the following code, which I have not been able
I'm trying to use the following code and it still strips out all the
For some reason the following code does not work as expected when in IE
I am trying to use the following code to write data into an excel
I am trying to use the following code to export tables from access to
I am trying to use the following code, to display $formcode, which is the
I'm trying to use the following code # LOAD XML FILE $XML = new
I'm trying to use the following code to make an image fadeOut and, only

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.