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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:11:28+00:00 2026-06-16T03:11:28+00:00

Inside a Portable Class Library, I’ve the following method which post data to a

  • 0

Inside a Portable Class Library, I’ve the following method which post data to a specific Url. The method works great. However I’d like to specify a more aggressive timeout (the default is 100 seconds).

Considering that there’s no Timeout property on the HttpWebRequest class from the Portable Class Library, how can I make sure that the call is abandoned if it takes longer than a few seconds?

public async Task<HttpResponse> PostAsync(Uri uri, string data)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";

    using (Stream requestStream = await request.GetRequestStreamAsync())
    {
        byte[] postBytes = Encoding.UTF8.GetBytes(data);
        requestStream.Write(postBytes, 0, postBytes.Length);
    }

    HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
    return new HttpResponse(response.StatusCode, await new StreamReader(response.GetResponseStream()).ReadToEndAsync());
}
  • 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-06-16T03:11:30+00:00Added an answer on June 16, 2026 at 3:11 am

    Below code either will return a HttpWebResponse or null if timed out.

    HttpWebResponse response = await TaskWithTimeout(request.GetResponseAsync(), 100);
    if(response != null)
    {
      ....
    }
    

    Task<HttpWebResponse> TaskWithTimeout(Task<WebResponse> task, int duration)
    {
        return Task.Factory.StartNew(() =>
        {
            bool b = task.Wait(duration);
            if (b) return (HttpWebResponse)task.Result;
            return null;
        });
    }
    

    –EDIT–

    Creating an extension method would be even better

    public static class SOExtensions
    {
        public static Task<T> WithTimeout<T>(this Task<T> task, int duration)
        {
            return Task.Factory.StartNew(() =>
            {
                bool b = task.Wait(duration);
                if (b) return task.Result;
                return default(T);
            });
        }
    }
    

    Usage would be:

    var response = (HttpWebResponse)await request.GetResponseAsync().WithTimeout(1000);
    

    –EDIT 2–

    Another way of doing it

    public async static Task<T> WithTimeout<T>(this Task<T> task, int duration)
    {
        var retTask = await Task.WhenAny(task, Task.Delay(duration))
                                .ConfigureAwait(false);
    
        if (retTask is Task<T>) return task.Result;
        return default(T);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use the cryptography in the Portable Class Library Contrib project on
Inside my Application class, I have a long-running function (e.g. downloading new data). Once
I have a Library project which i use it as a portable area project
Following from How can I build a targetting pack for Portable Class Libraries? and
Inside my Controller i have function that runs after user clicks on item, which
Is there a portable C library to access .zip archives? gzip or zlib (the
Suppose you've written a portable C++ code which runs smoothly on different platforms. To
It would be nice to be able to format a url in a portable
Inside the CLLocation Class Reference under the properties section this what it says for
Inside a Java enumerated class, I'd like to create a final static array containing

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.