[英]response using httpwebrequest and uri problems
I'm trying to test if a website is up or down and I ran into some problems. 我正在尝试测试网站是否正常运行,但遇到了一些问题。
First I know if I'm to test the status of the server it's possible to do it with a simple piece of code like: 首先,我知道如果我要测试服务器的状态,可以使用以下简单的代码来完成:
If My.Computer.Network.Ping(TextBox1.Text) Then
TextBox2.Text = "Online"
Else
TextBox2.Text = "Offline"
End If
But i refuse to do so since Pings are not always a reliable method of determining if an external web server is up. 但是我拒绝这样做,因为Ping并非始终是确定外部Web服务器是否启动的可靠方法。
So i try to work this out using this code: 所以我尝试使用以下代码解决这个问题:
Try
Dim fr As System.Net.HttpWebRequest
Dim targetURI As New Uri(TextBox1.Text)
fr = DirectCast(System.Net.HttpWebRequest.Create(targetURI), System.Net.HttpWebRequest)
If (fr.GetResponse().ContentLength > 0) Then
TextBox2.Text = "Website appears to exist!"
Else
TextBox2.Text = "Website appears to have no content at all!"
End If
Catch ex As System.Net.WebException
TextBox2.Text = "Cannot connect - website not responding!"
End Try
That code works. 该代码有效。 But the problem is if I type http://google.com
in the TextBox1
and try to get server status then sometimes it shows up as "Website appears to have no content at all!" 但是问题是,如果我在TextBox1
键入http://google.com
并尝试获取服务器状态,则有时会显示为“网站似乎根本没有内容!” even when http://google.com
is online and have contents. 即使http://google.com
处于在线状态并包含内容。 Not just for google but for other sites as well. 不仅适用于Google,而且适用于其他网站。
I have two question regarding this code. 关于此代码,我有两个问题。
Why does it tells me that "Website appears to have no content at all!" 为什么它告诉我“网站似乎根本没有内容!” even when http://google.com
or any other site is online and have contents? 即使http://google.com
或任何其他网站都在线并且有内容?
Suppose i type in just google.com
or any site without http://
or https://
in TextBox1 it gives me an error saying invalid URI format
. 假设我在TextBox1中只输入google.com
或没有http://
或https://
任何网站,这会给我一个错误,指出invalid URI format
。 Is there anyway I could automatically add http://
or https://
to the beginning of the url entered in TextBox1? 无论如何,我可以自动将http://
或https://
到TextBox1中输入的URL的开头吗?
I know I can add this to the above code to do that: 我知道我可以将其添加到上面的代码中来做到这一点:
Dim baseAddressx As String = "http://"
Dim targetURI As New Uri(baseAddressx & TextBox1.Text)
but what if I type http://google.com
when the code it self adds http://
that will make a problem. 但是如果我在自己的代码中添加http://
时输入了http://google.com
怎么办呢? Any better way to achieve that? 还有更好的方法吗?
What I think you should do is that you should read the Status Code of the response, not the content length. 我认为您应该做的是阅读响应的状态码,而不是内容长度。 It can be read as follows: 内容如下:
HttpWebResponse res = (HttpWebResponse)fr.GetResponse();
int statusCode = (int)response.StatusCode
if(statusCode == 200)
//Website is up else.
int statusCode = (int)response.StatusCode
if(statusCode == 200)
//Website is up else.
You may create enums to have a good understanding of it. 您可以创建枚举以对其有很好的理解。 And respond in various ways to the user based on which value you get. 并根据获得的价值以各种方式对用户做出响应。 Hope that helps. 希望能有所帮助。
Sorry I am no expert in VB. 抱歉,我不是VB专家。 I know C#, I hope you can convert this to VB easily. 我知道C#,希望您可以轻松将其转换为VB。 See here . 看这里 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.