Skip to content

How I can set timeout to httpstream which I'm reading? #42733

@shahinjabbar

Description

@shahinjabbar

Hi there. I am getting an HTTP stream and reading it until it sends some data but I need to figure out how to set a timeout in case of not receiving data from the stream which I'm reading using StreamReader class. This stream sends me data every 50-80 seconds and I wanna handle it when it does not send any data in this timestamp.The sample code is below:

            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(_configuration.GetSection("COnfig").GetValue<string>("Url"));
                var streamresponse = await client.GetStreamAsync("stream");
                using (StreamReader reader = new StreamReader(streamresponse))
                {
                    //reader.BaseStream.ReadTimeout = 2000;
                    while (true)
                    {
                        string line = await reader.ReadLineAsync();
                        if (!string.IsNullOrWhiteSpace(line))
                        {
                            string decoded = HttpUtility.UrlDecode(line, Encoding.GetEncoding("UTF-8")); ;
                            //some logic
                        }
                    }
                }

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Net.HttpquestionAnswer questions and provide assistance, not an issue with source code or documentation.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions