【发布时间】:2015-04-17 08:58:07
【问题描述】:
我正在尝试通过 GSM VPN 隧道发送 http 请求。下面是负责发送它的部分代码。我已经使用“笨拙”对其进行了测试,它可以在 400-500 毫秒内正常工作。但是在程序进入目标系统后,所有发送请求的尝试都以错误结束(“catch”发生,out in device 不会改变其状态)。 GSM 链路位置不佳(ping 80-400 毫秒,偶然丢包等),但我期待多次尝试中至少有一次会以成功告终。我的代码有什么问题?
Webexception 状态为:“超时”
完整的http链接如下:
192.168.1.100/outs.cgi?out0=0
答案是(设备中的输出状态为纯文本):
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;
;
}
基本上我在 c# 中处于“脚本小子”级别,所以如果你能做到这一点,请提供尽可能完整的代码;-)
【问题讨论】:
-
发生
catch时,抛出的异常是什么? -
它以“超时”状态结束。这有点奇怪,我已经开始尝试 Windows ping,最长响应时间是 350 毫秒(并且没有丢包)
-
350ms ping 以及通过 GSM 下载目标网页需要多长时间?
标签: c# gsm httpwebresponse