none
WebRequest with timeout RRS feed

  • Question

  • Hello,

    public MyObject RequestTest()
    {
    	MyObject ret = null;
    	string json = "";
    	using (var client = new WebClient())
    	{
    		try
    		{
    			json = client.DownloadString(WebRequest);
    			ret = JsonConvert.DeserializeObject<MyObject>(json);
    			Json = json.ToString();
    		}
    Works well. Problem is no setting for timeout.
    How I can do it?
    I found this.
    But is not work. I use the using and add the reference.
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Net;

    test

    Picture - Create is not exist, why?

    Any idea, solution?

      catch (WebException wex)
                    {
        
    What is the right way to catch the error 404?
    What should I do if the server sends an error object?
    With best regards Markus

    Monday, October 22, 2018 4:04 PM

Answers

  • Evaluate the next approach:

     

    string json = null;

    TimeSpan timeout = TimeSpan.FromSeconds( 10 );

     

    var t = new Thread( () =>

    {

        try

        {

            using( var client = new WebClient() )

            {

                json = client.DownloadString( WebRequest );

            }

        }

        catch( Exception exc )

        {

            // TODO: process the errors

        }

    } );

    t.IsBackground = true;

    t.Start();

     

    if( !t.Join( timeout ) )

    {

        // Timeout error

        t.Interrupt();

    }

    else

    {

        if( json == null )

        {

            // Failed to download the string

        }

        else

        {

            // OK, convert the string.

            // . . .

        }

    }

     

    The timeout was set to 10 seconds.



    • Edited by Viorel_MVP Monday, October 22, 2018 5:54 PM
    • Marked as answer by Markus Freitag Tuesday, October 23, 2018 3:52 PM
    Monday, October 22, 2018 5:53 PM
  • Hi Markus Freitag,

    Thank you for posting here.

    >>Picture - Create is not exist, why?

    According to your description, I test the Create method. Nothing wrong in my application.

    Please check again. If it still exists, please provide more details with error message.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Markus Freitag Tuesday, October 23, 2018 3:52 PM
    Tuesday, October 23, 2018 3:11 AM
    Moderator

All replies

  • Evaluate the next approach:

     

    string json = null;

    TimeSpan timeout = TimeSpan.FromSeconds( 10 );

     

    var t = new Thread( () =>

    {

        try

        {

            using( var client = new WebClient() )

            {

                json = client.DownloadString( WebRequest );

            }

        }

        catch( Exception exc )

        {

            // TODO: process the errors

        }

    } );

    t.IsBackground = true;

    t.Start();

     

    if( !t.Join( timeout ) )

    {

        // Timeout error

        t.Interrupt();

    }

    else

    {

        if( json == null )

        {

            // Failed to download the string

        }

        else

        {

            // OK, convert the string.

            // . . .

        }

    }

     

    The timeout was set to 10 seconds.



    • Edited by Viorel_MVP Monday, October 22, 2018 5:54 PM
    • Marked as answer by Markus Freitag Tuesday, October 23, 2018 3:52 PM
    Monday, October 22, 2018 5:53 PM
  • Hi Markus Freitag,

    Thank you for posting here.

    >>Picture - Create is not exist, why?

    According to your description, I test the Create method. Nothing wrong in my application.

    Please check again. If it still exists, please provide more details with error message.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Markus Freitag Tuesday, October 23, 2018 3:52 PM
    Tuesday, October 23, 2018 3:11 AM
    Moderator

  • Please check again. If it still exists, please provide more details with error message.




    Hello Wendy,

    now works, I restart all.

    Thanks.

    Greeting Markus

    Tuesday, October 23, 2018 3:53 PM