Exemplo n.º 1
0
    public Worker CreateWorker(Browser browser, string url, int timeout = 30)
    {
      if (browser == null)
        throw new ArgumentNullException("browser");

      if (string.IsNullOrEmpty(url))
        throw new ArgumentNullException("url");

      using (var client = new WebClient())
      {
        client.Headers.Add("authorization", authToken);

        client.QueryString.Add("os", browser.OsName);
        client.QueryString.Add("os_version", browser.OsVersion);
        client.QueryString.Add("browser", browser.BrowserName);
        client.QueryString.Add("browser_version", browser.BrowserVersion);
        client.QueryString.Add("device", browser.Device);
        client.QueryString.Add("timeout", timeout.ToString());
        client.QueryString.Add("url", HttpUtility.UrlEncode(url));

        var data = client.UploadString(Url + "/worker", string.Empty);

        return new Worker(((JObject)JsonConvert.DeserializeObject(data))["id"].Value<string>(), authToken);
      }
    }
Exemplo n.º 2
0
        public Worker CreateWorker(Browser browser, string url, int timeout = 30)
        {
            var request = WebRequest.Create(Url + "/worker");
            request.Headers.Add("authorization", authToken);

            request.Method = "POST";

            var postString = "browser=" + browser.Name + "&version=" + browser.Version + "&url=" + url + "&timeout=" +
                           timeout;
            var postBytes = Encoding.ASCII.GetBytes(postString);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postBytes.Length;

            using (var postStream = request.GetRequestStream())
                postStream.Write(postBytes, 0, postBytes.Length);

            var response = request.GetResponse();

            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                var data = reader.ReadToEnd();

                var id = JsonConvert.DeserializeAnonymousType(data, new { id = "asd" }).id;

                return new Worker(id, authToken);
            }
        }