Exemplo n.º 1
0
        private static string GetInternal(
            string url,
            Dictionary <string, string> headers,
            bool gzip,
            int retries,
            int sleepTimeMSec,
            int timeout,
            string vmName,
            bool isOnUIThreadOnPurpose,
            string oem = "bgp")
        {
            if (Thread.CurrentThread.ManagedThreadId == 1 && !isOnUIThreadOnPurpose)
            {
                Logger.Warning("WARNING: This network call is from the UI thread. StackTrace: {0}", (object)new StackTrace());
            }
            NameValueCollection headerCollection = HTTPUtils.GetRequestHeaderCollection(vmName);
            Uri uri = new Uri(url);

            if (uri.Host.Contains("localhost") || uri.Host.Contains("127.0.0.1"))
            {
                RegistryKey registryKey = RegistryUtils.InitKeyWithSecurityCheck("Software\\BlueStacks" + (oem.Equals("bgp", StringComparison.InvariantCultureIgnoreCase) ? "" : "_" + oem));
                headerCollection.Add("x_api_token", (string)registryKey.GetValue("ApiToken", (object)""));
            }
            else
            {
                headerCollection.Remove("x_api_token");
            }
            return(HTTP.Get(url, headers, gzip, retries, sleepTimeMSec, timeout, headerCollection, Utils.GetUserAgent(oem)));
        }
Exemplo n.º 2
0
        private static string HttpUploadFileInternal(
            string url,
            string file,
            string paramName,
            string contentType,
            Dictionary <string, string> headers,
            Dictionary <string, string> data)
        {
            Logger.Info("Uploading {0} to {1}", (object)file, (object)url);
            string str1 = "---------------------------" + DateTime.Now.Ticks.ToString("x", (IFormatProvider)CultureInfo.InvariantCulture);

            byte[]         bytes1         = Encoding.ASCII.GetBytes("\r\n--" + str1 + "\r\n");
            Uri            destination    = new Uri(url);
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

            httpWebRequest.ContentType = "multipart/form-data; boundary=" + str1;
            httpWebRequest.Method      = "POST";
            httpWebRequest.KeepAlive   = true;
            httpWebRequest.Timeout     = 300000;
            httpWebRequest.UserAgent   = Utils.GetUserAgent("bgp");
            if (!destination.Host.Contains("localhost") && !destination.Host.Contains("127.0.0.1"))
            {
                Uri proxy = httpWebRequest.Proxy.GetProxy(destination);
                Logger.Debug("URI of proxy = " + ((object)proxy != null ? proxy.ToString() : (string)null));
            }
            if (headers != null)
            {
                foreach (KeyValuePair <string, string> header in headers)
                {
                    httpWebRequest.Headers.Set(StringUtils.GetControlCharFreeString(header.Key), StringUtils.GetControlCharFreeString(header.Value));
                }
            }
            httpWebRequest.Headers.Add(HTTPUtils.GetRequestHeaderCollection(""));
            if (data == null)
            {
                data = new Dictionary <string, string>();
            }
            Stream requestStream = httpWebRequest.GetRequestStream();
            string format        = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";

            foreach (KeyValuePair <string, string> keyValuePair in data)
            {
                requestStream.Write(bytes1, 0, bytes1.Length);
                byte[] bytes2 = Encoding.UTF8.GetBytes(string.Format((IFormatProvider)CultureInfo.InvariantCulture, format, (object)keyValuePair.Key, (object)keyValuePair.Value));
                requestStream.Write(bytes2, 0, bytes2.Length);
            }
            requestStream.Write(bytes1, 0, bytes1.Length);
            byte[] bytes3 = Encoding.UTF8.GetBytes(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n", (object)paramName, (object)file, (object)contentType));
            requestStream.Write(bytes3, 0, bytes3.Length);
            string str2 = Path.Combine(Environment.ExpandEnvironmentVariables("%TEMP%"), Path.GetFileName(file)) + "_bst";

            System.IO.File.Copy(file, str2);
            if (contentType.Equals("text/plain", StringComparison.InvariantCultureIgnoreCase))
            {
                string s        = System.IO.File.ReadAllText(str2);
                byte[] numArray = new byte[1048576];
                byte[] bytes2   = Encoding.UTF8.GetBytes(s);
                requestStream.Write(bytes2, 0, bytes2.Length);
            }
            else
            {
                FileStream fileStream = new FileStream(str2, FileMode.Open, FileAccess.Read);
                byte[]     buffer     = new byte[4096];
                int        count;
                while ((count = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    requestStream.Write(buffer, 0, count);
                }
                fileStream.Close();
            }
            System.IO.File.Delete(str2);
            byte[] bytes4 = Encoding.ASCII.GetBytes("\r\n--" + str1 + "--\r\n");
            requestStream.Write(bytes4, 0, bytes4.Length);
            requestStream.Close();
            string      str3        = (string)null;
            WebResponse webResponse = (WebResponse)null;

            try
            {
                webResponse = httpWebRequest.GetResponse();
                using (Stream responseStream = webResponse.GetResponseStream())
                {
                    using (StreamReader streamReader = new StreamReader(responseStream))
                    {
                        str3 = streamReader.ReadToEnd();
                        Logger.Info("File uploaded, server response is: {0}", (object)str3);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Error uploading file", (object)ex);
                webResponse?.Close();
                throw;
            }
            finally
            {
            }
            return(str3);
        }