#StackBounty: #c# #.net #http #rest In-consistence behavior of C# code

Bounty: 50

I am creating Windows service. That is working sometimes when I start it first time. After few executions my code throwing exceptions like

Server unavailable 503

So many requests 429

Looks like my code is correct but few cases it is not working. Could you please check it once.

 private void timer1_Tick(object sender, ElapsedEventArgs e)
 {
    string jsonString = "";
    string jsonstring2 = "";
    string prodfetchurl = HOST;
    string prodfetchurl1 = "testURL";

    var req =
        WebRequest.Create(prodfetchurl) as HttpWebRequest;

    req.Method = "GET";
    req.KeepAlive = true;
    InitializeRequest(req);
    //req.Proxy = null;
    req.Accept = MIME_TYPE;
    System.Threading.Thread.Sleep(200000);
    var response = (HttpWebResponse)req.GetResponse();
    WriteToFile("First service called...");
    if (response.StatusCode == HttpStatusCode.OK)
    {
        Stream responseStream = response.GetResponseStream();
        StreamReader responseReader = new StreamReader(responseStream);
        jsonString = responseReader.ReadToEnd();
    }

    var deserialsseobj = JsonConvert.DeserializeObject<ProductList>(jsonString).Products.Where(i => i.Failed > 0).ToList();
    foreach (var a in deserialsseobj)
    {
        var pid = a.ID;
        string url = FailedDevicesUrl + pid.Value + "/failed";
        var req2 = WebRequest.Create(url) as HttpWebRequest;
        req2.Method = "GET";
        req2.KeepAlive = true;
        InitializeRequest(req2);
        //req2.Proxy = null;
        req2.Timeout = 300000;
        req2.Accept = MIME_TYPE;     
        System.Threading.Thread.Sleep(200000);

        var response1 = (HttpWebResponse)req2.GetResponse();
        Stream responsestream2 = response1.GetResponseStream();
        WriteToFile("Second service called...");
        if (response1.StatusCode == HttpStatusCode.OK)
        {
            StreamReader responsereader1 = new StreamReader(responsestream2);
            jsonstring2 = responsereader1.ReadToEnd();
        }
        var output = JsonConvert.DeserializeObject<List<FailedDeviceList>>(jsonstring2);  // Will get List of the Failed devices
        AutoReprocess(pid.Value, output);
        List<int> deviceids = new List<int>();
        Reprocessdata reproc = new Reprocessdata();
        Reprocessdata.DeviceId rprod = new Reprocessdata.DeviceId();

        reproc.ForceFlag = true;
        reproc.ProductID = pid.Value;
        foreach (var dd in output)
        {
            rprod.ID = dd.DeviceId;
            reproc.DeviceIds.Add(rprod);
        }
        // Reprocess the Product in Devices
        var req3 = WebRequest.Create(ReprocessUrl) as HttpWebRequest;
        req3.Method = "POST";
        InitializeRequest(req3);
        req3.Accept = MIME_TYPE;
        req3.ContentType = "application/json";
        using (StreamWriter writer = new StreamWriter(req3.GetRequestStream()))
        {
            string json = new JavaScriptSerializer().Serialize(reproc);

            writer.Write(json);
            writer.Close();
        }
        var response5 = (HttpWebResponse)req3.GetResponse();
        WriteToFile("Third service called...");
        if (response5.StatusCode == HttpStatusCode.OK)
        {
            string result;
            using (StreamReader rdr = new StreamReader(response5.GetResponseStream()))
            {
                result = rdr.ReadToEnd();
            }
        }
    }
    response.Close();
 }

Methods used in above code

public void AutoReprocess(int pid, List<FailedDeviceList> output)
{
    List<int> deviceids = new List<int>();
    Reprocessdata reproc = new Reprocessdata();
    Reprocessdata.DeviceId rprod = new Reprocessdata.DeviceId();
    reproc.ForceFlag = true;
    reproc.ProductID = pid;
    foreach (var dd in output)
    {
        rprod.ID = dd.DeviceId;
        reproc.DeviceIds.Add(rprod);
    }
    var req3 = WebRequest.Create(ReprocessUrl) as HttpWebRequest;
    req3.Method = "POST";
    req3.KeepAlive = true;
    InitializeRequest(req3);
    req3.Accept = MIME_TYPE;
    req3.Timeout = 300000;
    req3.ContentType = "application/json";
    using (StreamWriter writer = new StreamWriter(req3.GetRequestStream()))
    {
        string json = new JavaScriptSerializer().Serialize(reproc);

        writer.Write(json);
        writer.Close();
    }
    System.Threading.Thread.Sleep(100000);
    var response5 = (HttpWebResponse)req3.GetResponse();
    WriteToFile("Third service called...");
    if (response5.StatusCode == HttpStatusCode.OK)
    {
        string result;
        using (StreamReader rdr = new StreamReader(response5.GetResponseStream()))
        {
            result = rdr.ReadToEnd();
        }
    }
}

public void InitializeRequest(HttpWebRequest request)
{
    request.Headers.Add("aw-tenant-code", API_TENANT_CODE);
    request.Credentials = new NetworkCredential(USER_NAME, PASSWORD);
    request.KeepAlive = true;
    //request.AddRange(1024);
    //request.Proxy = null; 
}


Get this bounty!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.