C# 关于代理检测的问题(难,真的难)?

hwj383 2008-07-17 06:41:42
现在一个程序
通过我自已写的方法来判断是代理是否正常工作,核心代码如下:
public void Connect(string server, int port, Proxy proxy, ConnectCallBack callback)
{
this._client = new TcpClient();
try
{
this._server = server;
this._port = port;
this._callback = callback;
this._proxy = proxy;
this._client.BeginConnect(proxy.Server, proxy.Port, new AsyncCallback(this.Connected), null);
}
catch
{
}
}

private void Connected(IAsyncResult ar)
{
try
{
this._client.EndConnect(ar);
NetworkStream stream = this._client.GetStream();
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine(string.Format("CONNECT {0}:{1} HTTP/1.1", this._proxy.Server, this._proxy.Port));
writer.WriteLine("Accept: */*");
writer.WriteLine("Content-Type: text/html");
writer.WriteLine("Proxy-Connection: Keep-Alive");
if (!string.IsNullOrEmpty(this._proxy.User))
{
string s = this._proxy.User + ":" + this._proxy.Password;
writer.WriteLine("Proxy-Authorization: Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(s)));
}
writer.WriteLine("Content-length: 0");
writer.WriteLine();
writer.Flush();
bool isSuccess = false;
string msg = "代理服务器错误";
StreamReader reader = new StreamReader(stream);
for (string str4 = reader.ReadLine(); (str4 != null) && (str4.Length > 0); str4 = reader.ReadLine())
{
if (str4.ToUpper().IndexOf("HTTP/1.0") > -1)
{
string[] strArray = str4.Split(new char[] { ' ' });
if (strArray.Length > 1)
{
if (strArray[1] == "200")
{
isSuccess = true;
msg = "代理服务器工作正常";
}
else
{
msg = "代理服务器错误 错误码" + strArray[1];
}
}
}
}
this._callback(isSuccess, msg);
}
catch (Exception)
{
this._callback(false, "连接代理服务器失败");
}
}


服务器返回状态码403(隐藏),程序认为代理不可用~
但是QQ代理测试是正常的,也是可用的,请问上述方法检测代理是否有问题,最好有demo可以参考
...全文
414 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hznetnewpower 2009-12-14
  • 打赏
  • 举报
回复
看一下回复
Santos 2008-07-18
  • 打赏
  • 举报
回复
利用telnet [ip] [port]测试

程序代码就不帮你写啦
manbufenglin 2008-07-17
  • 打赏
  • 举报
回复
帮你顶一下吧!!
江城老温 2008-07-17
  • 打赏
  • 举报
回复
网络编程的主要工作量大多在异常处理了。测试,异常会花很多时间。常有很古怪的问题。楼主仔细找找吧。
whq1982 2008-07-17
  • 打赏
  • 举报
回复
你先别用qq代理测试 你直接ping一下试试

111,041

社区成员

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

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

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