Ejemplo n.º 1
0
        public AutoConectWebSocket(string url, bool autoConnect = true, bool useProxy = false, System.Net.CookieContainer cookies = null, Dictionary <string, string> requestHeader = null) : base(url)
        {
            if (autoConnect)
            {
                Thread thread = new Thread(() =>
                {
                    while (true)
                    {
                        try
                        {
                            if (ReadyState != WebSocketState.Open)
                            {
                                if (cookies != null)
                                {
                                    Socket.Options.Cookies = cookies;
                                }

                                if (ReadyState != WebSocketState.Open && ReadyState != WebSocketState.Connecting)
                                {
                                    if (useProxy)
                                    {
                                        string proxyIP = HttpProxy.GetRandomProxyIP();
                                        if (proxyIP != "")
                                        {
                                            IWebProxy webProxy   = new WebProxy(proxyIP, HttpProxy.port);
                                            Socket.Options.Proxy = webProxy;
                                        }
                                    }
                                }

                                if (requestHeader != null)
                                {
                                    foreach (KeyValuePair <string, string> item in requestHeader)
                                    {
                                        Socket.Options.SetRequestHeader(item.Key, item.Value);
                                    }
                                }
                                Connect();
                            }
                        }
                        catch (Exception ex)
                        {
                            LogRecord.Warn("AutoConectWebSocket", string.Format("Client connect {0} error {1}.", Url, ex.ToString()));
                        }
                        resetEvent.WaitOne(3000);
                    }
                });
                thread.Start();
            }

            ConnectionClosed += AutoConectWebSocket_OnClose;
            Error            += AutoConectWebSocket_OnError;
            ConnectionOpened += AutoConectWebSocket_OnOpen;
        }
Ejemplo n.º 2
0
        public static string Post(string url, string data, string contentType, bool useProxy = false,
                                  Encoding encoding = null, int timeout = Timeout, NameValueCollection header = null, string userAgent = null,
                                  string referer    = null, CookieContainer cookieContainer = null)
        {
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            var client = new RestClient(url);

            client.Timeout = timeout;
            if (!string.IsNullOrEmpty(userAgent))
            {
                client.UserAgent = userAgent;
            }

            if (useProxy)
            {
                string proxyIP = HttpProxy.GetRandomProxyIP();
                if (proxyIP != "")
                {
                    IWebProxy webProxy = new WebProxy(HttpProxy.GetRandomProxyIP(), 3128);
                    client.Proxy = webProxy;
                }
            }

            if (cookieContainer != null)
            {
                client.CookieContainer = cookieContainer;
            }

            var request = new RestRequest(Method.POST);

            if (header != null && header.Count > 0)
            {
                string[] keys = header.AllKeys;
                foreach (string key in keys)
                {
                    request.AddHeader(key, header[key]);
                }
            }

            request.AddParameter(contentType, data, ParameterType.RequestBody);

            IRestResponse response = client.Execute(request);

            return(response.Content);
        }
Ejemplo n.º 3
0
        public static string Request(string url, string method, byte[] data, string contentType, bool useProxy = false,
                                     Encoding encoding = null, int timeout = Timeout, NameValueCollection header = null, string userAgent = null,
                                     string referer    = null, CookieContainer cookieContainer = null)
        {
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            HttpWebRequest request = HttpWebRequest.CreateHttp(url);

            request.Method      = method;
            request.ContentType = contentType;
            request.Timeout     = timeout;
            if (!string.IsNullOrEmpty(userAgent))
            {
                request.UserAgent = userAgent;
            }

            if (!string.IsNullOrEmpty(referer))
            {
                request.Referer = referer;
            }

            if (header != null && header.Count > 0)
            {
                request.Headers.Add(header);
            }

            if (useProxy)
            {
                string proxyIP = HttpProxy.GetRandomProxyIP();
                if (proxyIP != "")
                {
                    IWebProxy webProxy = new WebProxy(HttpProxy.GetRandomProxyIP(), 3128);
                    request.Proxy = webProxy;
                }
            }

            if (cookieContainer != null)
            {
                request.CookieContainer = cookieContainer;
            }

            if (method.ToLower() != "get" && data != null)
            {
                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(data, 0, data.Length);
                }
            }

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(stream, encoding))
                    {
                        return(reader.ReadToEnd());
                    }
                }
            }
        }