Webresponse does not work over GSM connection

I am trying to send an http request over a GSM VPN tunnel. Below is the part of the code responsible for submitting it. I tested it using "clumsy" and it works fine up to 400-500ms. But after the program has entered the target system, all attempts to send a request end in error (a "catch" occurs, its state does not change in the device). The GSM connection is in a bad place (80-400ms ping, random packet drops, etc.) but I expected at least one of many attempts to succeed. What's wrong with my code?

Web Authentication Status: Timeout

The full http link looks like this:

192.168.1.100/outs.cgi?out0=0

Answer (states of outputs in the device in plain text):

10000

        private int switch_kontroler(string adress, int outnr, int state)
    {
        int i = 0;
        if (this.checkBox1.Checked == true) //I have checbox "start GSM mode" in Form
            goto GSM; 
        else
            goto Test;
   GSM:  
        if (i<3)
        {
            goto Test;
        }
        else
            goto Koniec;
   Test:
        try
            {
                i++;
                label3.Text = "Próba przełączenia: "+i;
                this.Refresh();
                WebRequest request = WebRequest.Create("http://" + adress + "/outs.cgi?out" + outnr + "=" + state);
                request.Method = "GET";
                request.Timeout = 1000; //BTW. Why program works up to 500ms if timeout is set at 1000?
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
                string result = sr.ReadToEnd();
                label3.Text = result.ToString();
                if (result[outnr] - '0' == state)
                    if (result[outnr] == '1')
                    {
                        label3.Text = "Załączono kamerę";
                        return 1; //info to the rest of program about succes (or not) of this method
                    }
                    else
                    {
                        label3.Text = "Wyłączono kamerę";
                        return 0;
                    }
                this.Refresh();
                response.Close();
            }
            catch (WebException e)
            {
                label3.Text = "Przełączenie nieudane: " + e.Status;
                this.Refresh();
                if (this.checkBox1.Checked == true)
                    goto GSM;
                else
                    goto Koniec;
Koniec:
       return 2;
                ;
        }

      

Basically, I'm at the "script kid" level in C #, so if you can be that nice, please provide as complete a code as possible; -)

+3


source to share


1 answer


Ok, the problem is solved. Thanks to DavidG

It looks like the response takes much longer than ping (it's just a few digits in plain text, so I'm surprised). I have increased the timeout to 10 seconds and it works great.



Announcement: The end of the timeout increased to 20 seconds, even 15 was causing the problem for most of the time (oddly enough, if there is a connection, I get a response after 2-3 seconds, but the timeout should be set to 10+)

0


source







All Articles