[英]Displaying a picturebox? I don't see what I am doing wrong here
I am checking to see if a server was reachable, then setting a picturebox's "Visible" boolean to true. 我正在检查服务器是否可以访问,然后将图片框的“可见”布尔值设置为true。 However, when I first ran the code, no errors appeared, but no picturebox appeared either.
但是,当我第一次运行代码时,没有出现错误,但是也没有出现图片框。
There is a offlinePic (Sets Visible boolean to true, and brings to front when the test fails) There is a onlinePic (Sets Visible boolean to true, and brings to front when the test passes) 有一个offlinePic(将Visible布尔值设置为true,并在测试失败时显示在最前面)有一个onlinePic(将Visible布尔值设置为true,并在测试通过时显示在最前面)
I thought it would be possible that it was the server screwing things up, so I changed it to try with Google.com, and there is no difference. 我认为可能是服务器搞砸了,所以我将其更改为可以在Google.com上尝试,并且没有区别。
private void Launcher_Load(object sender, EventArgs e)
{
TestServerConnection();
}
public void TestServerConnection()
{
string url = "www.google.com";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 15000;
request.Method = "HEAD";
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (onlinePic.Visible == false) onlinePic.Visible = true;
onlinePic.BringToFront();
}
}
catch (WebException)
{
if (offlinePic.Visible == false) offlinePic.Visible = true;
offlinePic.BringToFront();
}
}
The format for your url is wrong. 您的网址格式错误。
www.google.com should be http://www.google.com www.google.com应该是http://www.google.com
Everything else works fine for me. 其他一切对我来说都很好。
Not all servers can respond on HEAD
try to use GET
instead in request.Method
. 并非所有服务器都可以在
HEAD
响应,而是尝试在request.Method
使用GET
。 And specify protocol http
in the URL. 并在URL中指定协议
http
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.