日期:2014-05-18  浏览次数:20911 次

C# 关于代理检测的问题(难,真的难)?
现在一个程序
通过我自已写的方法来判断是代理是否正常工作,核心代码如下:
C# code
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可以参考

------解决方案--------------------
你先别用qq代理测试 你直接ping一下试试
------解决方案--------------------
网络编程的主要工作量大多在异常处理了。测试,异常会花很多时间。常有很古怪的问题。楼主仔细找找吧。
------解决方案--------------------
帮你顶一下吧!!
------解决方案--------------------
利用telnet [ip] [port]测试

程序代码就不帮你写啦