public static System.Threading.Tasks.Task <System.IO.Stream> GetRequestStreamAsync(this System.Net.HttpWebRequest wr)
    {
        if (wr.ContentLength < 0)
        {
            throw new InvalidOperationException("The ContentLength property of the HttpWebRequest must first be set to the length of the content to be written to the stream.");
        }
        var tcs = new System.Threading.Tasks.TaskCompletionSource <System.IO.Stream>();

        wr.BeginGetRequestStream((result) =>
        {
            var source = (System.Net.HttpWebRequest)result.AsyncState;
            tcs.TrySetResult(source.EndGetRequestStream(result));
        }, wr);
        return(tcs.Task);
    }
Ejemplo n.º 2
0
 private static void MakeRequestJson(string method, string body, Action <ResponseDataJson> callback)
 {
     System.Net.HttpWebRequest request = System.Net.HttpWebRequest.CreateHttp(API_URL + method);
     request.ContentType = "text/json";
     request.Accept      = "text/json";
     request.Method      = "POST";
     request.BeginGetRequestStream((iar) =>
     {
         try
         {
             var reqStream = ((System.Net.HttpWebRequest)iar.AsyncState).EndGetRequestStream(iar);
             System.IO.StreamWriter writer = new System.IO.StreamWriter(reqStream);
             writer.Write(body);
             writer.Flush();
             request.BeginGetResponse((iar2) =>
             {
                 var isOk = false;
                 try
                 {
                     var response       = ((System.Net.HttpWebRequest)iar.AsyncState).EndGetResponse(iar2);
                     var responseStream = response.GetResponseStream();
                     var jsonStr        = new System.IO.StreamReader(responseStream).ReadToEnd();
                     responseStream.Dispose();
                     isOk = true;
                     callback.Invoke(new ResponseDataJson(ResponseCode.OK, jsonStr));
                 }
                 catch (Exception e)
                 {
                     if (!isOk)
                     {
                         callback.Invoke(new ResponseDataJson(ResponseCode.Fail, null, e));
                     }
                 }
             }, request);
         }
         catch (Exception e)
         {
             callback.Invoke(new ResponseDataJson(ResponseCode.Fail, null, e));
         }
     }, request);
 }
Ejemplo n.º 3
0
        public void send(object o)
        {
            var args = o as Jint.Native.Object.ObjectInstance;

            options = extractOptions(args);
            url     = new Uri(origin + options["url"]);


            if (options["transport"] == "browser")
            {
                req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            }
            else
            {
                req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
                req.CookieContainer = cookieContainer;
                // disable buffering (this only works for ClientHttp version)
                //_req.AllowWriteStreamBuffering = false; // causes silent crash on Mac OS X 10.8.x
            }

            req.Method = options["method"];

            // add custom headers
            if (headers.Count != 0)
            {
                foreach (string key in headers.Keys)
                {
                    if (headers[key] == null)
                    {
                        continue;
                    }

                    switch (key.ToLower())
                    {
                    // in silverlight 3, these are set by the web browser that hosts the Silverlight application.
                    // http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest%28v=vs.95%29.aspx
                    case "connection":
                    case "content-length":
                    case "expect":
                    case "if-modified-since":
                    case "referer":
                    case "transfer-encoding":
                    case "user-agent":
                        break;

                    // in silverlight this isn't supported, can not find reference to why not
                    case "range":
                        break;

                    // in .NET Framework 3.5 and below, these are set by the system.
                    // http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest%28v=VS.90%29.aspx
                    case "date":
                    case "host":
                        break;

                    case "accept":
                        req.Accept = (string)headers[key];
                        break;

                    case "content-type":
                        req.ContentType = headers[key];
                        break;

                    default:
                        req.Headers[key] = (string)headers[key];
                        break;
                    }
                }
            }

            req.ContentLength = 0;


            {
                req.ContentType = options["mimeType"];
            }



            if (req.ContentLength == 0)
            {
                req.BeginGetResponse(new AsyncCallback(responseCallback), req);
            }
            else
            {
                req.BeginGetRequestStream(new AsyncCallback(responseCallback), req);
            }
        }