Ejemplo n.º 1
0
            /// <summary>
            /// Starts a new GET request to given Uri
            /// </summary>
            private void StartRequest(RequestContext context, Uri uri, string method = "GET")
            {
                if (uri.Scheme != "https")
                {
                    throw new ArgumentException("https url is required");
                }

                FSLog.Info(context.Type, uri);

                Request        = (HttpWebRequest)FSRequest.Create(uri);
                Request.Method = method;

                if (Request.Headers == null)
                {
                    Request.Headers = new System.Net.WebHeaderCollection();
                }

                if (!string.IsNullOrWhiteSpace(Parent.AuthToken))
                {
                    Request.Headers["authorizationtoken"] = Parent.AuthToken;
                }

                Request.Headers["platform"] = "WP";
                Request.Headers["version"]  = "3.0";
                // No caching
                Request.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString();

                Request.BeginGetResponse(new AsyncCallback((IAsyncResult asynchronousResult) =>
                {
                    ReadCallBack(context, asynchronousResult);
                }), Request);
            }
Ejemplo n.º 2
0
            private void StartPostRequest(RequestContext context, Uri uri, string data, string method = "POST")
            {
                if (uri.Scheme != "https")
                {
                    throw new ArgumentException("https url is required");
                }

                FSLog.Info(context.Type, uri);
                //FSLog.Debug(data);

                var bytes = System.Text.Encoding.UTF8.GetBytes(data);

                Request        = (HttpWebRequest)FSRequest.Create(uri);
                Request.Method = method;

                Request.Headers            = new System.Net.WebHeaderCollection();;
                Request.Headers["charset"] = "UTF-8";
                // No caching
                Request.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString();

                Request.ContentType   = "application/json";
                Request.ContentLength = bytes.Length;
                Request.Accept        = "application/json";

                if (!string.IsNullOrWhiteSpace(Parent.AuthToken))
                {
                    Request.Headers["authorizationtoken"] = Parent.AuthToken;
                }
                Request.Headers["platform"] = "WP";
                Request.Headers["version"]  = "3.0";

                Request.BeginGetRequestStream(new AsyncCallback((IAsyncResult writeResult) =>
                {
                    try
                    {
                        using (Stream writeStream = Request.EndGetRequestStream(writeResult))
                        {
                            writeStream.Write(bytes, 0, bytes.Length);
                        }

                        Request.BeginGetResponse(new AsyncCallback((IAsyncResult asynchronousResult) =>
                        {
                            ReadCallBack(context, asynchronousResult);
                        }), Request);
                    }
                    catch (Exception e)
                    {
                        FSLog.Exception(e);

                        context.ResponseBody = null;
                        context.IsCompleted  = true;
                    }
                }), Request);
            }