Monday, November 21, 2016

webclient timeout exception c#

HI All,

Please find the below code to Overcome Timeout Issue from WebClient

         /// <summary>
        /// MyWebClient to increase Timeout
        /// </summary>
        public class MyWebClient : WebClient
        {
            //time in milliseconds
            private int timeout;
            public int Timeout
            {
                get
                {
                    return timeout;
                }
                set
                {
                    timeout = value;
                }
            }

            public MyWebClient()
            {
                this.timeout = 300000;
            }

            public MyWebClient(int timeout)
            {
                this.timeout = timeout;
            }

            protected override WebRequest GetWebRequest(Uri address)
            {
                var result = base.GetWebRequest(address);
                result.Timeout = this.timeout;
                return result;
            }
        }

Call the Above Code using below

using (var client = new MyWebClient())
 {
client.Headers.Add(username and Password); // Replace "username and Password" with your username and Password
client.Headers[HttpRequestHeader.ContentType] = "application/json";
var result = client.UploadString(URL, "POST", JSONData); // Replace "URL" with your Service URL and "JSONData" with your JSON Output Data
}


No comments:

Post a Comment

How to Run Microsoft Flow for Every one Month

Introduction: In this Blog we will see how to Run Microsoft Flow for Every one Month. Implementation Steps:   1. Navigate to  https://make.p...