Set Timeout for HttpWebRequest in Xamarin using C# .Net

Last Reply 3 months ago By dharmendr

Posted 3 months ago

How to set timeout here?

I can see only continueTimeout property for the httpwebrequest in my project

            var request = (HttpWebRequest)WebRequest.Create(url);
            try
            {
                request.Headers[System.Net.HttpRequestHeader.Authorization] = "Basic " + DefaultCredentialsCrypto.Tokenvalue(Logincredentials.Userid, Logincredentials.Userpwd);
                request.Method = "GET";
                request.Accept = "text/xml";

                request.Headers["MobileDevice"] = Device.RuntimePlatform;
                //request.Headers["AppVersion"] = Translate.GetTranslatedText("tbVersion");
                request.ContinueTimeout = 5;
            }

 
           var taskComplete = new TaskCompletionSource<HttpWebResponse>();
            try
            {
                
                request.BeginGetResponse(asyncResponse =>
                 {
                     HttpWebResponse response = null;
                     try
                     {
                        response = (HttpWebResponse)request.EndGetResponse(asyncResponse);
                         if (response != null)
                         {
                             if (response.StatusCode == System.Net.HttpStatusCode.OK)
                                 taskComplete.SetResult(response);
                         }
                     }
                     catch (WebException exc)
                     {
                         if (exc.Response != null && (exc.Message.Equals("The remote server returned an error: 488.") || exc.Message.Contains("488")))
                             taskComplete.SetResult(exc.Response as HttpWebResponse);
                         else if (exc != null && exc.Response != null && (exc.Message.Equals("The remote server returned an error: Unauthorized.") || exc.Message.Contains("Unauthorized")))
                             taskComplete.SetResult(exc.Response as HttpWebResponse);
                         else if (exc != null && exc.Response != null && (exc.Message.Contains("403") || exc.Message.Contains("Forbidden")))
                             taskComplete.SetResult(exc.Response as HttpWebResponse);
                         else
                         {
                             exc.GetBaseException();
                             taskComplete.SetResult(null);
                         }
                         if (request != null)
                         {
                             // request.Abort();
                             request = null;
                         }
                         return;
                     }
                 }, request);
            }
          
            catch(Exception ex)
            {
                taskComplete.SetResult(null);
                if (request != null)
                {
                   // request.Abort();
                    request = null;
                }
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }   
            return taskComplete.Task;        

 

You are viewing reply posted by: dharmendr 3 months ago.
Posted 3 months ago

I think it is not available in Xamarin. Please create another project and check is TimeOut available or not.