Esempio n. 1
0
        public static QuickWebRequest Create(Uri uri, Uri refuri, QuickWebRequest AliveRequest, bool bKeepAlive)
        {
            if (bKeepAlive &&
                AliveRequest != null &&
                AliveRequest.response != null &&
                AliveRequest.response.IsKeepLive &&
                AliveRequest.response.SocketConnected &&
                AliveRequest.RequestUri.Host == uri.Host)
            {
                AliveRequest.RequestUri = uri;
                AliveRequest.RefererUri = refuri;

                return(AliveRequest);
            }

            //如果原socket还在连接状态,但是主机名发生了变化,导致连接不能复用,需要关闭原socket
            if (AliveRequest != null &&
                AliveRequest.response != null &&
                AliveRequest.response.IsKeepLive &&
                AliveRequest.response.SocketConnected &&
                AliveRequest.RequestUri != null &&
                AliveRequest.RequestUri.Host != uri.Host)
            {
                AliveRequest.response.Close();
            }

            return(new QuickWebRequest(uri, refuri, bKeepAlive));
        }
Esempio n. 2
0
        /// <summary>
        /// 发送连接,并且接收包头分析,只能在连接正常并且keepLive,并获取了包头的情况下调用。
        /// 并且不能调用两次
        /// </summary>
        /// <param name="request"></param>
        public void ConnectAndGetHeader(QuickWebRequest request)
        {
            isRequestSend = false;
            RequestUri    = request.RequestUri;
            RedirectUri   = null;
            socket        = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress[] addrlist = null;
            if (IsIPAddress(RequestUri.Host))
            {
                addrlist    = new IPAddress[1];
                addrlist[0] = IPAddress.Parse(RequestUri.Host);
            }
            else
            {
                addrlist = Dns.GetHostEntry(RequestUri.Host).AddressList;
            }
            IPEndPoint remoteEP = null;

            foreach (IPAddress ipaddr in addrlist)
            {
                try
                {
                    remoteEP = new IPEndPoint(ipaddr, RequestUri.Port);
                    if (this.Timeout > 0)
                    {
                        socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, Timeout * 1000);
                        socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, Timeout * 1000);
                    }
                    socket.Connect(remoteEP);
                    break;
                }
                catch
                {
                    remoteEP = null;
                }
            }

            //发送请求

            request.Header = request.Method + " " + RequestUri.PathAndQuery + " HTTP/1.1\r\n" + request.Headers;
            socket.Send(Encoding.ASCII.GetBytes(request.Header));
            isRequestSend = true;

            //接收包头
            ReceiveHeader();
        }
Esempio n. 3
0
        public static QuickWebRequest Create(Uri uri,Uri refuri, QuickWebRequest AliveRequest, bool bKeepAlive)
        {
            if( bKeepAlive &&
                AliveRequest != null &&
                AliveRequest.response != null &&
                AliveRequest.response.IsKeepLive &&
                AliveRequest.response.SocketConnected &&
                AliveRequest.RequestUri.Host == uri.Host)
            {
                AliveRequest.RequestUri = uri;
                AliveRequest.RefererUri = refuri;

                return AliveRequest;
            }

            //如果原socket还在连接状态,但是主机名发生了变化,导致连接不能复用,需要关闭原socket
            if (AliveRequest != null &&
                AliveRequest.response != null &&
                AliveRequest.response.IsKeepLive &&
                AliveRequest.response.SocketConnected &&
                AliveRequest.RequestUri != null &&
                AliveRequest.RequestUri.Host != uri.Host) {
                    AliveRequest.response.Close();
            }

            return new QuickWebRequest(uri, refuri, bKeepAlive);
        }
Esempio n. 4
0
        private string ReadUrlContent(ref string url, bool checkredirect, bool checkHtmlRedurect,int timeoutsecond)
        {
            string content = String.Empty;

            try
            {
                Uri uri = new Uri(url);
                gRequest = QuickWebRequest.Create(uri, null, gRequest, false);
                gResponse = gRequest.GetResponse(timeoutsecond);

                if (checkredirect)
                {
                    if (gResponse.RedirectUri != null && gResponse.RedirectUri.AbsoluteUri != url)
                    {
                        return ReadUrlContent(gResponse.RedirectUri.AbsoluteUri, false, checkHtmlRedurect);
                    }
                }

                byte[] bytes = gResponse.ReadResponse();
                if (bytes == null)
                {
                    return null;
                }

                System.Text.Encoding charset = HttpUtils.DetectCharset(gResponse, bytes);
                if (charset == null)
                { //默认使用GB2312
                    charset = System.Text.Encoding.GetEncoding("GB2312");
                }

                // 使用指定的编码
                if (!string.IsNullOrEmpty(Encoding))
                {
                    charset = System.Text.Encoding.GetEncoding(Encoding);
                }

                //完成到UTF8的编码转换
                if (charset != System.Text.Encoding.UTF8)
                {
                    bytes = System.Text.Encoding.Convert(charset, System.Text.Encoding.UTF8, bytes);
                    charset = System.Text.Encoding.UTF8;
                }

                content = charset.GetString(bytes);
                //检查一次Html重定向
                if (checkHtmlRedurect)
                {
                    System.Text.RegularExpressions.Regex redirectRegex = new System.Text.RegularExpressions.Regex("<META\\s+HTTP-EQUIV\\s*=\\s*[\"]*Refresh[\"]*\\s+CONTENT=[\"\\s]*\\d+\\s*[;]\\s*URL=(?<match>.*?)[\\s\"]*>", System.Text.RegularExpressions.RegexOptions.Singleline | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    System.Text.RegularExpressions.Match match = redirectRegex.Match(content);
                    if (match.Success)
                    {
                        if (!string.IsNullOrEmpty(match.Groups["match"].Value) && match.Groups["match"].Value != url)
                        {
                            url = match.Groups["match"].Value;
                            return ReadUrlContent(url, checkredirect, false);
                        }
                    }
                }
            }
            catch
            {
                //暂时不做处理。
            }
            finally
            {
                gResponse.Close();
            }

            return content;
        }
Esempio n. 5
0
        /// <summary>
        /// 发送连接,并且接收包头分析,只能在连接正常并且keepLive,并获取了包头的情况下调用。
        /// 并且不能调用两次
        /// </summary>
        /// <param name="request"></param>
        public void ConnectAndGetHeader(QuickWebRequest request)
        {
            isRequestSend = false;
            RequestUri = request.RequestUri;
            RedirectUri = null;
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress[] addrlist = null;
            if (IsIPAddress(RequestUri.Host))
            {
                addrlist = new IPAddress[1];
                addrlist[0] = IPAddress.Parse(RequestUri.Host);
            }
            else
            {
                addrlist = Dns.GetHostEntry(RequestUri.Host).AddressList;
            }
            IPEndPoint remoteEP = null;
            foreach (IPAddress ipaddr in addrlist)
            {
                try
                {
                    remoteEP = new IPEndPoint( ipaddr, RequestUri.Port );
                    if (this.Timeout > 0)
                    {
                        socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, Timeout * 1000);
                        socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, Timeout * 1000);
                    }
                    socket.Connect(remoteEP);
                    break;
                }
                catch
                {
                    remoteEP = null;
                }
            }

            //发送请求

            request.Header = request.Method + " " + RequestUri.PathAndQuery + " HTTP/1.1\r\n" + request.Headers;
            socket.Send(Encoding.ASCII.GetBytes(request.Header));
            isRequestSend = true;

            //接收包头
            ReceiveHeader();
        }
Esempio n. 6
0
        private string ReadUrlContent(ref string url, bool checkredirect, bool checkHtmlRedurect, int timeoutsecond)
        {
            string content = String.Empty;

            try
            {
                Uri uri = new Uri(url);
                gRequest  = QuickWebRequest.Create(uri, null, gRequest, false);
                gResponse = gRequest.GetResponse(timeoutsecond);

                if (checkredirect)
                {
                    if (gResponse.RedirectUri != null && gResponse.RedirectUri.AbsoluteUri != url)
                    {
                        return(ReadUrlContent(gResponse.RedirectUri.AbsoluteUri, false, checkHtmlRedurect));
                    }
                }

                byte[] bytes = gResponse.ReadResponse();
                if (bytes == null)
                {
                    return(null);
                }

                System.Text.Encoding charset = HttpUtils.DetectCharset(gResponse, bytes);
                if (charset == null)
                { //默认使用GB2312
                    charset = System.Text.Encoding.GetEncoding("GB2312");
                }

                // 使用指定的编码
                if (!string.IsNullOrEmpty(Encoding))
                {
                    charset = System.Text.Encoding.GetEncoding(Encoding);
                }

                //完成到UTF8的编码转换
                if (charset != System.Text.Encoding.UTF8)
                {
                    bytes   = System.Text.Encoding.Convert(charset, System.Text.Encoding.UTF8, bytes);
                    charset = System.Text.Encoding.UTF8;
                }

                content = charset.GetString(bytes);
                //检查一次Html重定向
                if (checkHtmlRedurect)
                {
                    System.Text.RegularExpressions.Regex redirectRegex = new System.Text.RegularExpressions.Regex("<META\\s+HTTP-EQUIV\\s*=\\s*[\"]*Refresh[\"]*\\s+CONTENT=[\"\\s]*\\d+\\s*[;]\\s*URL=(?<match>.*?)[\\s\"]*>", System.Text.RegularExpressions.RegexOptions.Singleline | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    System.Text.RegularExpressions.Match match         = redirectRegex.Match(content);
                    if (match.Success)
                    {
                        if (!string.IsNullOrEmpty(match.Groups["match"].Value) && match.Groups["match"].Value != url)
                        {
                            url = match.Groups["match"].Value;
                            return(ReadUrlContent(url, checkredirect, false));
                        }
                    }
                }
            }
            catch
            {
                //暂时不做处理。
            }
            finally
            {
                gResponse.Close();
            }

            return(content);
        }