Esempio n. 1
0
        public async Task<string> GetPageAsync(string url, HttpVerb verb, string referrer = null)
        {
            var responseText = "";

            using (var request = RequestBuilder.Build(url, CommonHeaders(referrer), CookieJar))
            {
                request.Method = verb.ToVerbString();

                using (var response = await request.GetResponseAsync().ConfigureAwait(false))
                {
                    using (var stream = await response.GetResponseStreamAsync())
                    {
                        using (var sr = new StreamReader(stream))
                        {
                            responseText = sr.ReadToEnd();
                        }
                    }

                    var doc = new HtmlDocument();
                    doc.LoadHtml(responseText);

                    var scripts = doc.DocumentNode.Descendants("script");
                    foreach (var s in scripts.Where(s => s.InnerText.Contains("top.location")))
                    {
                        var newUrl = s.InnerText.Split('=')[1].Trim().Trim(new[] {' ', ';', '"'});
                        return await GetPageAsync(newUrl, verb).ConfigureAwait(false);
                    }
                }
            }

            return responseText;
        }
Esempio n. 2
0
 public RestClient(string endpoint, HttpVerb method)
 {
     EndPoint = endpoint;
     Method = method;
     ContentType = "application/json";//"text/xml";
     PostData = "";
 }
Esempio n. 3
0
		/**
         * RestClient with an end point, request type, post-data if any and content type
         */
        public RestClient (string endpoint, HttpVerb method, string postData,string contentTypeIdentifier)
        {
            EndPoint = endpoint;
            Method = method;
            ContentType = contentTypeIdentifier;
            PostData = postData;
        }
Esempio n. 4
0
 public RestClient(string endpoint, HttpVerb method, string postData)
     : this()
 {
     EndPoint = endpoint;
     Method = method;
     PostData = postData;
 }
Esempio n. 5
0
 public SimpleRestClient(string endpoint, HttpVerb method)
 {
     EndPoint = endpoint;
     Method = method;
     ContentType = "text/json";
     PostData = string.Empty;
 }
 public RestHelper(string endpoint, HttpVerb method)
 {
     EndPoint = endpoint;
     Method = method;
     ContentType = "application/json";
     PostData = "";
 }
Esempio n. 7
0
 public RestClient(string endpoint, HttpVerb method, string postData)
 {
     EndPoint = endpoint;
     Method = method;
     ContentType = "text/xml";
     PostData = postData;
 }
Esempio n. 8
0
 public RestClient(string endpoint, HttpVerb method, Object postObj)
 {
     this.EndPoint = endpoint;
     this.Method = method;
     this.ContentType = "application/json;charset=UTF-8";
     this.PostData = Newtonsoft.Json.JsonConvert.SerializeObject(postObj);
 }
Esempio n. 9
0
 public RestClient(string endpoint, HttpVerb method, string postData)
 {
     EndPoint = endpoint;
     Method = method;
     ContentType = "application/json";
     PostData = postData;
 }
Esempio n. 10
0
        /// <summary>
        /// Make an HTTP request, with the given query args
        /// </summary>
        /// <param name="url">The URL of the request</param>
        /// <param name="httpVerb">The HTTP verb to use</param>
        /// <param name="argument">String thar represents the request data, must be in JSON format</param>
        private static string MakeRequest(Uri url, HttpVerb httpVerb, string argument = null)
        {
            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = httpVerb.ToString();
            request.ContentType = "application/json";

            if (httpVerb == HttpVerb.POST)
            {
                var encoding = new ASCIIEncoding();
                var postDataBytes = new byte[0];
                if (argument != null)
                {
                    postDataBytes = encoding.GetBytes(argument);
                }

                request.ContentLength = postDataBytes.Length;

                var requestStream = request.GetRequestStream();
                requestStream.Write(postDataBytes, 0, postDataBytes.Length);
                requestStream.Close();
            }

            try
            {
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    var reader = new StreamReader(response.GetResponseStream());
                    return reader.ReadToEnd();
                }
            }
            catch (WebException e)
            {
                throw new CoticulaApiException("Server Error", e.Message);
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Builds a HTTP request from the given arguments.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="verb">The verb.</param>
        /// <param name="agent">The agent.</param>
        /// <param name="cookies">The cookies.</param>
        /// <param name="referer">The referer.</param>
        /// <param name="retries">The retries.</param>
        /// <param name="timeout">The timeout.</param>
        /// <param name="accept">The accept.</param>
        /// <param name="encoding">The encoding.</param>
        /// <returns></returns>
        public HttpRequest Build(string url, HttpVerb verb , string agent, CookieContainer cookies = null, string referer = "", int retries = 0, int timeout = 10000, BaseMime accept = null, Encoding encoding = null)
        {
            var request = new HttpRequest
                          {
                              Url = url,
                              UserAgent = agent,
                              Verb = verb,
                              Referer = referer,
                              Retries = retries,
                              Timeout = timeout,
                              Encoding = encoding ?? Encoding.UTF8
                          };

            if (accept != null)
            {
                request.Accept = accept.ToString();
            }

            if (cookies != null)
            {
                request.Cookies = cookies;
            }

            return request;
        }
Esempio n. 12
0
 public RestClient(string endpoint, HttpVerb method, string postData)
 {
     this.EndPoint = endpoint;
     this.Method = method;
     this.ContentType = "text/xml";
     this.PostData = postData;
 }
Esempio n. 13
0
        public HttpClient(string endpoint, HttpVerb httpVerb, NameValueCollection headers, string contentType, string parameters)
        {
            try
            {
                this.Request = (HttpWebRequest)WebRequest.Create(endpoint);
                this.Request.ContentLength = 0;
                this.Request.ContentType = contentType.ToString();
                this.Request.Method = httpVerb.ToString();

                if (headers != null)
                {
                    foreach (var key in headers.AllKeys)
                    {
                        this.Request.Headers.Add(key, headers[key]);
                    }
                }

                if (this.Request.Method == HttpVerb.POST.ToString() && !String.IsNullOrEmpty(parameters))
                {
                    var encoding = new UTF8Encoding();
                    var bytes = Encoding.GetEncoding("utf-8").GetBytes(parameters);

                    this.Request.ContentLength = bytes.Length;

                    using (var writeStream = Request.GetRequestStream())
                    {
                        writeStream.Write(bytes, 0, bytes.Length);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ArgumentException(String.Format("Create WebRequest Error: {0}", ex.Message));
            }
        }
Esempio n. 14
0
        public WebRequestUtility(string realm, Uri requestUri, HttpVerb verb, Boolean keepAlive, String userAgent, NetworkCredential networkCredentials)
        {
            if (requestUri == null)
                throw new ArgumentNullException("requestUri");

            this.Realm = realm;
            this.RequestUri = requestUri;
            this.Verb = verb;
            this.KeepAlive = keepAlive;
            this.UserAgent = userAgent;
            this.UseOAuth = false;
            if (networkCredentials != null)
                this.NetworkCredentials = networkCredentials;

            this.Parameters = new Dictionary<string, object>();

            if (string.IsNullOrEmpty(this.RequestUri.Query)) return;

            foreach (Match item in Regex.Matches(this.RequestUri.Query, @"(?<key>[^&?=]+)=(?<value>[^&?=]+)"))
            {
                this.Parameters.Add(item.Groups["key"].Value, item.Groups["value"].Value);
            }

            this.RequestUri = new Uri(this.RequestUri.AbsoluteUri.Replace(this.RequestUri.Query, ""));
        }
Esempio n. 15
0
 public RestClient(string endpoint, HttpVerb method, string postData, string callType, string token)
 {
     EndPoint = endpoint;
     Method = method;
     ContentType = "application/json";
     CallType = callType;
     PostData = postData;
     Token = token;
 }
Esempio n. 16
0
 public RestClient(string endpoint, string command, HttpVerb method)
 {
     _params = new Dictionary<string, string>();
     EndPoint = endpoint;
     Command = command;
     Method = method;
     ContentType = "text/xml";
     PostData = string.Empty;
 }
Esempio n. 17
0
            public void MakeRequest(HttpVerb method, string url, HeaderProvider header, AuthenticationProvider auth, BodyProvider body)
            {
                this.method = method;
                this.url = url;
                this.headers = header;
                this.auth = auth;
                this.body = body;

                this.MakeRequest();
            }
Esempio n. 18
0
		public XmlElement GetRequestResponseElement(string requestPath, string data, HttpVerb verb)
		{
			IWebRequest request = _webRequestFactory.CreateWebRequest(string.Format("{0}/{1}", Url, requestPath));
			request.Method = verb;
			request.BasicAuthorization = _authorization;
			if (verb != HttpVerb.Get)
			{
				request.RequestText = data;
			}
			return request.Response.DocumentElement;
		}
Esempio n. 19
0
 public static IMockNetworkWithExpectation Expecting(
     HttpVerb verb,
     string path,
     IDictionary<string, string> queryParams,
     string requestContent)
 {
     var mock = new MockNetwork();
     mock._verb = verb;
     mock._path = path;
     mock._queryParams = queryParams;
     mock._requestContent = requestContent;
     return mock;
 }
Esempio n. 20
0
        public WebRequestUtility(string realm, Uri requestUri, HttpVerb verb, AccessTokens tokens, Boolean keepAlive = false, String userAgent = "")
            : this(realm, requestUri, verb, keepAlive, userAgent, null)
        {
            this.Tokens = tokens;

            if (tokens != null)
            {
                if (string.IsNullOrEmpty(this.Tokens.ConsumerKey) || string.IsNullOrEmpty(this.Tokens.ConsumerSecret))
                {
                    throw new ArgumentException("Consumer key and secret are required for OAuth requests.");
                }

                if (string.IsNullOrEmpty(this.Tokens.AccessToken) ^ string.IsNullOrEmpty(this.Tokens.AccessTokenSecret))
                {
                    throw new ArgumentException("The access token is invalid. You must specify the key AND secret values.");
                }

                this.UseOAuth = true;
            }
        }
Esempio n. 21
0
 /// <summary>
         /// 发出一次新的Head请求,获取资源的长度
         /// 此请求会忽略PostingData, Files, StartPoint, EndPoint, Verb
         /// </summary>
         /// <returns>返回的资源长度</returns>
 public async Task<int> HeadContentLength()
 {
     Reset();
     HttpVerb lastVerb = verb;
     verb = HttpVerb.HEAD;
     using (HttpWebResponse res = await GetResponse())
     {
         verb = lastVerb;
         return (int)res.ContentLength;
     }
 }
Esempio n. 22
0
        private async Task<HttpWebRequest> CreateRequest()
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

            req.CookieContainer = new CookieContainer();
            //req.Headers.Add("Accept-Language", defaultLanguage);
            req.Accept = accept;
            // req.UserAgent = userAgent;
            // req.KeepAlive = false;

            //if (context.Cookies != null)
            //    req.CookieContainer.Add(context.Cookies);
            //if (!string.IsNullOrEmpty(context.Referer))
            //    req.Referer = context.Referer;

            if (verb == HttpVerb.HEAD)
            {
                req.Method = "HEAD";
                return req;
            }

            if (postingData.Count > 0 || files.Count > 0)
                verb = HttpVerb.POST;

            if (verb == HttpVerb.POST)
            {
                req.Method = "POST";

                MemoryStream memoryStream = new MemoryStream();
                StreamWriter writer = new StreamWriter(memoryStream);

                if (files.Count > 0)
                {
                    string newLine = "\r\n";
                    string boundary = Guid.NewGuid().ToString().Replace("-", "");
                    req.ContentType = "multipart/form-data; boundary=" + boundary;

                    foreach (string key in postingData.Keys)
                    {
                        writer.Write("--" + boundary + newLine);
                        writer.Write("Content-Disposition: form-data; name=\"{0}\"{1}{1}", key, newLine);
                        writer.Write(postingData[key] + newLine);
                    }

                    foreach (HttpUploadingFile file in files)
                    {
                        writer.Write("--" + boundary + newLine);
                        writer.Write(
                        "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}",
                        file.FieldName,
                        file.FileName,
                        newLine
                        );
                        writer.Write("Content-Type: application/octet-stream" + newLine + newLine);
                        writer.Flush();
                        memoryStream.Write(file.Data, 0, file.Data.Length);
                        writer.Write(newLine);
                        writer.Write("--" + boundary + newLine);
                    }
                }
                else
                {
                    req.ContentType = "application/x-www-form-urlencoded";
                    StringBuilder sb = new StringBuilder();
                    foreach (string key in postingData.Keys)
                    {
                        sb.AppendFormat("{0}={1}&", key, postingData[key]);
                    }
                    if (sb.Length > 0)
                        sb.Length--;
                    writer.Write(sb.ToString());
                }

                writer.Flush();

                using (Stream stream = await req.GetRequestStreamAsync())
                {
                    memoryStream.WriteTo(stream);
                }
            }

            //if (startPoint != 0 && endPoint != 0)
            //    req.AddRange(startPoint, endPoint);
            //else if (startPoint != 0 && endPoint == 0)
            //    req.AddRange(startPoint);

            return req;
        }
Esempio n. 23
0
 /// <summary>
  /// 清空PostingData, Files, StartPoint, EndPoint, ResponseHeaders, 并把Verb设置为Get.
  /// 在发出一个包含上述信息的请求后,必须调用此方法或手工设置相应属性以使下一次请求不会受到影响.
  /// </summary>
 public void Reset()
 {
     verb = HttpVerb.GET;
     files.Clear();
     postingData.Clear();
     responseHeaders = null;
     startPoint = 0;
     endPoint = 0;
 }
Esempio n. 24
0
 public RequestBuilder(string url, HttpVerb method)
 {
     this.url = url;
     this.method = method;
 }
Esempio n. 25
0
        //
        // InitializeKnownVerbs - Does basic init for this object,
        //  such as creating defaultings and filling them
        //
        private static ListDictionary InitializeKnownVerbs() {

            namedHeaders = new ListDictionary(CaseInsensitiveString.StaticInstance);

            namedHeaders["GET"] = new HttpVerb(false, true, false, false);
            namedHeaders["CONNECT"] = new HttpVerb(false, true, true, false);
            namedHeaders["HEAD"] = new HttpVerb(false, true, false, true);
            namedHeaders["POST"] = new HttpVerb(true, false, false, false);
            namedHeaders["PUT"] = new HttpVerb(true, false, false, false);

            // default Verb
            KnownVerbs.DefaultVerb = new HttpVerb(false, false, false, false);

            return namedHeaders;
        }
Esempio n. 26
0
        internal static HttpVerb GetHttpVerbType(String name)
        {
            HttpVerb obj = (HttpVerb)KnownVerbs.namedHeaders[name];

            return(obj != null ? obj : KnownVerbs.DefaultVerb);
        }
Esempio n. 27
0
 public void Open(HttpVerb verb, string url, bool async)
 {
 }
Esempio n. 28
0
 public RestWebRequest(Uri uri, HttpVerb verb)
 {
     _baseRequest = new RestRequest(HttpWebRequest.Create(uri));
     _baseRequest.Method = verb.ToString();
     InitRequest();
 }
 public void Open(HttpVerb verb, string url, bool @async)
 {
 }
Esempio n. 30
0
        /// <summary>
        /// Make an HTTP request, with the given query args
        /// </summary>
        /// <param name="url">The URL of the request</param>
        /// <param name="httpVerb">The HTTP verb to use</param>
        /// <param name="args">Dictionary of key/value pairs that represents
        /// the key/value pairs for the request</param>
        /// <param name="contentType"></param>
        /// <exception cref="FacebookApiException"></exception>
        /// <exception cref="SecurityException"></exception>
        internal string Request(string url, HttpVerb httpVerb, Dictionary<string, string> args, out string contentType)
        { 
            if (args != null && args.Keys.Count > 0 && httpVerb == HttpVerb.Get)
            {
                url = url+ (url.Contains("?") ? "&" : "?") + EncodeDictionary(args);
            }

            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Proxy = Proxy;
            request.Timeout = (int)Timeout.TotalMilliseconds;
            request.Headers.Add(HttpRequestHeader.AcceptLanguage, Culture.IetfLanguageTag.ToLowerInvariant());
            request.Accept = "text/javascript";
            request.Method = httpVerb.ToString();

            if (httpVerb == HttpVerb.Post)
            {
                string postData = EncodeDictionary(args);
                byte[] postDataBytes = Encoding.UTF8.GetBytes(postData);

                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = postDataBytes.Length;

                try
                {
                    using (Stream requestStream = request.GetRequestStream())
                    {
                        requestStream.Write(postDataBytes, 0, postDataBytes.Length);
                    }
                }
                catch(WebException ex)
                {
                    if (ex.Status == WebExceptionStatus.Timeout)
                        throw OperationTimeout(ex);

                    throw;
                }
                catch (Exception ex)
                {
                    throw TransportError(ex);
                }
            }

            HttpWebResponse response;
            try
            {
                using (response = (HttpWebResponse)request.GetResponse())
                {
                    contentType = ExtractContentType(response);
                    return new StreamReader(response.GetResponseStream()).ReadToEnd();
                }
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ProtocolError)
                {
                    response = (HttpWebResponse)ex.Response;
                    contentType = ExtractContentType(response);

                    if (contentType == "text/javascript")
                    {
                        using (response)
                        {
                            return new StreamReader(response.GetResponseStream()).ReadToEnd();
                        }
                    }
                }

                throw TransportError(ex);
            }
            catch (Exception ex)
            {
                throw TransportError(ex);
            }
        }
 public void Open(HttpVerb verb, string url, bool @async, string userName, string password)
 {
 }
 public void Open(HttpVerb verb, string url)
 {
 }
Esempio n. 33
0
 public void Open(HttpVerb verb, string url, bool async, string userName, string password)
 {
 }
Esempio n. 34
0
 public void Open(HttpVerb verb, string url)
 {
 }