C# 判断网址是否连接成功(多种都不成功)

wang20726 2008-10-21 11:10:01
请问在C#中,输入一个网址,如何判断网址是否连接成功?

注:之前在网上用的方法我都试过了,不成功
尝试的有:
1、使用Ping方法
2、使用HttpWebRequest加HttpWebResponse方法获取状态值,也不成功
3、使用Socket
IPHostEntry IPHost = Dns.Resolve(url);
EndPoint ep = new IPEndPoint(IPHost.AddressList[0], 80);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(ep);

if (sock.Connected)
{
isConnect = true;
}

也不行
4、最后使用WebClient这个类也不行
郁闷。。。。。现在 我都不知道用什么方法好一点。。。因为测试都不成功,各位大虾有什么好的办法?

...全文
1623 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
wang20726 2008-10-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cpio 的回复:]
用Socket连80端口不通?那就是连接不成功啊

用浏览器可以打开吗?
[/Quote]

问题是全部都能,如果你输入的网址不存在,他的值也是True
cpio 2008-10-21
  • 打赏
  • 举报
回复
用Socket连80端口不通?那就是连接不成功啊

用浏览器可以打开吗?
wang20726 2008-10-21
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 stcsk1226 的回复:]
引用 7 楼 wang20726 的回复:
引用 4 楼 stcsk1226 的回复:
WebClient应该可以得到服务器端返回的状态码,你可以判断下,如果不存在,返回的状态码应该是不对的,或者干脆解析一下读取到的文本文件,应该可以实现。


返回在状态码是OK 你说还能怎么办?

获取文本文件,是它跳转之后,即解析失败的网址(就像我们上网输入的网址不正确跳转的网址)?又何用?


对的,就是跳转后我们浏览器应该是给出一个错误的页面,那…
[/Quote]


没错。。。最后用你这个办法解决了,
是跳转后我们浏览器应该是给出一个错误的页面,那你解析一下这个页面的某些元素,即href
代码如下:


/// <summary>
/// 判断当前网址是否连通
/// </summary>
/// <param name="url">网址</param>
/// <returns></returns>
public bool isPing(string url)
{
bool isConnect = true ;
try
{
WebClient client = new WebClient();
byte[] buffer = client.DownloadData(url);
string page = Encoding.Default.GetString(buffer);
string encode = commondText.getHeaderInfo(commondText.getHeader(page), "charset");
if (encode.Equals("utf-8"))
{
page = Encoding.UTF8.GetString(buffer);
}

//如果获取的内容文字很少,或者没有连接地址的话,说明是无效的
if (page.Length <= 120 || page.ToLower().IndexOf("<a href") == -1)
{
isConnect = false;
}
}
catch (Exception)
{
isConnect = false;
}
return isConnect;
}
wang20726 2008-10-21
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 zhangzhuang502 的回复:]
下面的代码示例连接到远程终结点,请检查 Connected 属性,然后检查连接的当前状态。

client.Connect(anEndPoint);

// 确定SOCKET是否一直处于连接状态
bool blockingState = client.Blocking;
try
{
byte [] tmp = new byte[1];

client.Blocking = false;
client.Send(tmp, 0, 0);
Console.WriteLine("Connected!");
}
catch (SocketException e)
{
// 10035 == WSAEWOULD…
[/Quote]

这种方法也不行。。。。有时有效,有时无效。。。
wang20726 2008-10-21
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 gujianxin 的回复:]
WebClient client = new WebClient();
try
{
Stream str = client.OpenRead(this.textBox1.Text);
using (System.IO.TextReader read = new StreamReader(str, Encoding.UTF8))
{
while (true)
{
string tmp = read.ReadLine();
if (tmp == null)
break;
}
client.Dispose();
} …
[/Quote]

其实这种方法就我说的第四种方法,如果有些网址无法解析还可以判断,但当网址可以解析的时候,即可以解析但却跳转到错误页里去了
就好像我:www.qsdfkjalsdfalsdf.com
他却跳转到http://autosearch.gd.vnet.cn/ 这样判断不也成功?
zhangzhuang502 2008-10-21
  • 打赏
  • 举报
回复
下面的代码示例连接到远程终结点,请检查 Connected 属性,然后检查连接的当前状态。

client.Connect(anEndPoint);

// 确定SOCKET是否一直处于连接状态
bool blockingState = client.Blocking;
try
{
byte [] tmp = new byte[1];

client.Blocking = false;
client.Send(tmp, 0, 0);
Console.WriteLine("Connected!");
}
catch (SocketException e)
{
// 10035 == WSAEWOULDBLOCK
if (e.NativeErrorCode.Equals(10035))
Console.WriteLine("Still Connected, but the Send would block");
else
{
Console.WriteLine("Disconnected: error code {0}!", e.NativeErrorCode);
}
}
finally
{
client.Blocking = blockingState;
}

Console.WriteLine("Connected: {0}", client.Connected);
zhangzhuang502 2008-10-21
  • 打赏
  • 举报
回复
Connected 属性的值反映最近操作时的连接状态。如果您需要确定连接的当前状态,请进行非阻止、零字节的 Send 调用。如果该调用成功返回或引发 WAEWOULDBLOCK 错误代码 (10035),则该套接字仍然处于连接状态;否则,该套接字不再处于连接状态。

如果调用用户数据报协议 (UDP) 套接字上的 Connect,则 Connected 属性始终返回 true;不过,此操作不更改 UDP 的内在无连接特性。
// .Connect throws an exception if unsuccessful
client.Connect(anEndPoint);

// This is how you can determine whether a socket is still connected.
bool blockingState = client.Blocking;
try
{
byte [] tmp = new byte[1];

client.Blocking = false;
client.Send(tmp, 0, 0);
Console.WriteLine("Connected!");
}
catch (SocketException e)
{
// 10035 == WSAEWOULDBLOCK
if (e.NativeErrorCode.Equals(10035))
Console.WriteLine("Still Connected, but the Send would block");
else
{
Console.WriteLine("Disconnected: error code {0}!", e.NativeErrorCode);
}
}
finally
{
client.Blocking = blockingState;
}

Console.WriteLine("Connected: {0}", client.Connected);
gujianxin 2008-10-21
  • 打赏
  • 举报
回复
This property is read-only.
When this property returns true , the current instance was connected at the time of the last I/O operation; it might not still be connected. When this property returns false , the current instance was never connected or is not currently connected.

The current instance is considered connected when the System.Net.Sockets.Socket.RemoteEndPoint property contains a valid endpoint.


[Note: The System.Net.Sockets.Socket.Accept and System.Net.Sockets.Socket.Connect(System.Net.EndPoint) methods, and their asynchronous counterparts set this property.]

也就是说sock.Connected 只和RemoteEndPoint 是否有效有关,只要执行Accept 或Connect ,并且RemoteEndPoint 有效,那就是true
gujianxin 2008-10-21
  • 打赏
  • 举报
回复
WebClient client = new WebClient();
try
{
Stream str = client.OpenRead(this.textBox1.Text);
using (System.IO.TextReader read = new StreamReader(str, Encoding.UTF8))
{
while (true)
{
string tmp = read.ReadLine();
if (tmp == null)
break;
}
client.Dispose();
}
MessageBox.Show(" connect");
}
catch (Exception ex)
{
MessageBox.Show("can't connect");
}
这样是可以的
stcsk1226 2008-10-21
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wang20726 的回复:]
引用 4 楼 stcsk1226 的回复:
WebClient应该可以得到服务器端返回的状态码,你可以判断下,如果不存在,返回的状态码应该是不对的,或者干脆解析一下读取到的文本文件,应该可以实现。


返回在状态码是OK 你说还能怎么办?

获取文本文件,是它跳转之后,即解析失败的网址(就像我们上网输入的网址不正确跳转的网址)?又何用?
[/Quote]

对的,就是跳转后我们浏览器应该是给出一个错误的页面,那你解析一下这个页面的某些元素,如果存在这些内容就判定为不存在此网址,这样应该可以,只是想法,没有实践过。
wang20726 2008-10-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 stcsk1226 的回复:]
WebClient应该可以得到服务器端返回的状态码,你可以判断下,如果不存在,返回的状态码应该是不对的,或者干脆解析一下读取到的文本文件,应该可以实现。
[/Quote]

返回在状态码是OK 你说还能怎么办?

获取文本文件,是它跳转之后,即解析失败的网址(就像我们上网输入的网址不正确跳转的网址)?又何用?
zcl26 2008-10-21
  • 打赏
  • 举报
回复
我也想知道解决方法,楼主有办法以后分享一下啊
killer_liqiao 2008-10-21
  • 打赏
  • 举报
回复
关注...
stcsk1226 2008-10-21
  • 打赏
  • 举报
回复
WebClient应该可以得到服务器端返回的状态码,你可以判断下,如果不存在,返回的状态码应该是不对的,或者干脆解析一下读取到的文本文件,应该可以实现。
guest78978 2008-10-21
  • 打赏
  • 举报
回复
UP

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧