/// <summary>
        /// Make a call to the CloudScrape API
        /// </summary>
        /// <param name="url"></param>
        /// <param name="method"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public CloudResponse Request(string url, string method = "GET", string body = null)
        {
            var req = System.Net.HttpWebRequest.Create(EndPoint + url) as HttpWebRequest;

            req.Headers.Add("X-CloudScrape-Access", accessKey);
            req.Headers.Add("X-CloudScrape-Account", accountId);
            req.UserAgent   = _userAgent;
            req.Timeout     = _requestTimeout;
            req.Accept      = "application/json";
            req.ContentType = "application/json";
            req.Method      = method;

            if (body != null)
            {
                using (var streamWriter = new StreamWriter(req.GetRequestStream()))
                {
                    streamWriter.Write(body);
                    streamWriter.Flush();
                }
            }

            CloudResponse   objCloudResponse = null;
            HttpWebResponse response         = null;
            StreamReader    readStream       = null;

            try
            {
                response = (HttpWebResponse)req.GetResponse();

                objCloudResponse = new CloudResponse();

                objCloudResponse.statusCode        = response.StatusCode;
                objCloudResponse.Headers           = response.Headers;
                objCloudResponse.StatusDescription = response.StatusDescription;

                WebHeaderCollection obj = response.Headers;
                Stream receiveStream    = response.GetResponseStream();
                readStream = new StreamReader(receiveStream, Encoding.UTF8);
                objCloudResponse.Content = readStream.ReadToEnd();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }

                if (readStream != null)
                {
                    readStream.Close();
                }
            }

            return(objCloudResponse);
        }
Пример #2
0
        /// <summary>
        /// Execute request
        /// </summary>
        /// <param name="url"></param>
        /// <param name="method"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public string RequestJson(string url, string method = "GET", string body = null)
        {
            CloudResponse response = this.Request(url, method, body);

            return(response.Content);
        }
        /// <summary>
        /// Make a call to the CloudScrape API
        /// </summary>
        /// <param name="url"></param>
        /// <param name="method"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public CloudResponse Request(string url, string method = "GET", string body = null)
        {
            var req = System.Net.HttpWebRequest.Create(EndPoint + url) as HttpWebRequest;

            req.Headers.Add("X-CloudScrape-Access", accessKey);
            req.Headers.Add("X-CloudScrape-Account", accountId);
            req.UserAgent = _userAgent;
            req.Timeout = _requestTimeout;
            req.Accept = "application/json";
            req.ContentType = "application/json";
            req.Method = method;

            if (body != null)
            {
                using (var streamWriter = new StreamWriter(req.GetRequestStream()))
                {
                    streamWriter.Write(body);
                    streamWriter.Flush();
                }
            }

            CloudResponse objCloudResponse = null;
            HttpWebResponse response = null;
            StreamReader readStream = null;

            try
            {

                response = (HttpWebResponse)req.GetResponse();

                objCloudResponse = new CloudResponse();

                objCloudResponse.statusCode = response.StatusCode;
                objCloudResponse.Headers = response.Headers;
                objCloudResponse.StatusDescription = response.StatusDescription;

                WebHeaderCollection obj = response.Headers;
                Stream receiveStream = response.GetResponseStream();
                readStream = new StreamReader(receiveStream, Encoding.UTF8);
                objCloudResponse.Content = readStream.ReadToEnd();

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }

                if (readStream != null)
                {
                    readStream.Close();
                }
            }

            return objCloudResponse;
        }