Alan2
Alan2

Reputation: 24602

HttpWebRequest timing out

I have this code that times out as there's a lot of data coming from the server. Does anyone know if there are ways to stop the time outs?

    static List<PhraseSource> GetPhrases()
    {
        var request = HttpWebRequest.Create("http://aaa.cloudapp.net/api/Phrase/GetJDTO");
        request.ContentType = "application/json";
        request.Method = "POST";

        using (var streamWriter = new StreamWriter(request.GetRequestStream()))
        {
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(new
            {
                form = "2", // just get words
                type = "0"
            });
            streamWriter.Write(json);
        }

Here's my C# server code. Not sure but maybe there's also a problem there with that but for now it seems to be on the client:

    [HttpPost]
    [AllowAnonymous]
    [Route("GetJDTO")]
    public async Task<IHttpActionResult> GetJDTO([FromBody] GetJDTOOptions options)
    {
        IQueryable<Phrase> query = null;

        query = db.Phrases.AsQueryable();

Upvotes: 0

Views: 243

Answers (1)

Kirk Larkin
Kirk Larkin

Reputation: 93303

The WebRequest object returned from your call to HttpWebRequest.Create has a Timeout property. In the case of HttpWebRequest, this defaults to 100 seconds.

In your example, you can set this to something higher if you need to. e.g.:

...
request.Method = "POST";
request.Timeout = **numberOfMilliseconds**;

You might want to consider paging, etc, as suggested by @thisextendsthat in the question comments, but Timeout addresses your specific question.

As you specifically asked about stopping the timeouts altogether, you can use Timeout.Infinite (as mentioned in the docs) but that has the potential to destroy your client-side experience, etc.

Upvotes: 1

Related Questions