Esempio n. 1
0
        private void requestThread()
        {
            HttpWebResponse httpWebRespones = null;

            try
            {
                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                HttpWebRequest httpWebRequest = WebRequest.Create(githubUrl) as HttpWebRequest;
                httpWebRequest.Timeout           = 60 * 1000;
                httpWebRequest.ReadWriteTimeout  = 60000;
                httpWebRequest.AllowAutoRedirect = true;
                httpWebRequest.UserAgent         = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36";
                httpWebRespones = (HttpWebResponse)httpWebRequest.GetResponse();


                using (Stream stream = httpWebRespones.GetResponseStream())
                {
                    List <byte> lst   = new List <byte>();
                    int         nRead = 0;
                    while ((nRead = stream.ReadByte()) != -1)
                    {
                        lst.Add((byte)nRead);
                    }
                    byte[] bodyBytes = lst.ToArray();

                    string body = Encoding.UTF8.GetString(bodyBytes, 0, bodyBytes.Length);

                    var data = JsonConvert.DeserializeObject <GithubModel>(body);
                    Info.IsPre       = data.prerelease;
                    Info.Title       = data.name;
                    Info.Version     = data.tag_name;
                    Info.DownloadUrl = data.assets[0].browser_download_url;
                    Info.HtmlUrl     = data.html_url;
                    RequestCompleteEvent?.Invoke(this, Info);
                }
            }
            catch (Exception ec)
            {
                LogHelper.Warning(ec.ToString());
                RequestErrorEvent?.Invoke(this, ec.Message);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Sends the specified request to the server
        /// (Proxy not supported)
        /// </summary>
        /// <param name="parsedRequest"></param>
        /// <param name="host"></param>
        /// <param name="port"></param>
        /// <param name="https"></param>
        public void SendRequest(HttpRequestInfo parsedRequest, string host, int port, bool https)
        {
            _requestCompleteEvent.Reset();

            _dataBuilder = new ByteArrayBuilder();

            if (_connection == null)
            {
                _connection = new HttpClientConnection(host, port, https);
            }

            try
            {
                //connect
                if (_connection.Connect())
                {
                    bool isProxy = _connection.Host != host;

                    //add connection closed header only this is supported at the moment
                    parsedRequest.Headers["Connection"] = "close";

                    if (isProxy)
                    {
                        parsedRequest.Headers["Proxy-Connection"] = "close";
                    }

                    // Turn off accepting of gzip/deflate
                    //parsedRequest.Headers.Remove("Accept-Encoding");

                    //calculate the content length
                    if (parsedRequest.ContentData != null)
                    {
                        parsedRequest.Headers["Content-Length"] = parsedRequest.ContentData.Length.ToString();
                    }

                    parsedRequest.Host     = host;
                    parsedRequest.Port     = port;
                    parsedRequest.IsSecure = https;

                    if (isProxy && https)
                    {
                        //send a connect message to the proxy
                        SendConnect(host, port);
                    }

                    byte[] reqBytes = Constants.DefaultEncoding.GetBytes(parsedRequest.ToString(isProxy && !https));

                    //write to the stream
                    _connection.Stream.Write(reqBytes, 0, reqBytes.Length);

                    //start reading
                    _buffer = new byte[MAX_BUFFER_SIZE];
                    _connection.Stream.BeginRead(_buffer, 0, MAX_BUFFER_SIZE, new AsyncCallback(ReadResponse), _connection.Stream);
                }
                else
                {
                    throw new Exception("Cannot connect to server");
                }
            }
            catch (Exception ex)
            {
                SdkSettings.Instance.Logger.Log(TraceLevel.Error, "HttpClient error sending request {0}", ex.Message);
                //notify the caller that the request was completed with an error
                if (RequestComplete != null)
                {
                    RequestComplete.Invoke(
                        new HttpClientRequestCompleteEventArgs());
                    RequestCompleteEvent.Set();
                }

                _connection.Close();
            }
        }