Manos.Http.HttpRequest.Execute C# (CSharp) Method

Execute() public method

public Execute ( ) : void
return void
        public void Execute()
        {
            Socket = AppHost.Context.CreateSocket ();
            Socket.Connect (RemoteAddress, RemotePort, delegate {
                Stream = new HttpStream (this, Socket.GetSocketStream ());
                Stream.Chunked = false;
                Stream.AddHeaders = false;

                byte [] body = GetBody ();

                if (body != null) {
                    Headers.ContentLength = body.Length;
                    Stream.Write (body, 0, body.Length);
                }

                Stream.End (() => {
                    HttpResponse response = new HttpResponse (Context, this, Socket);

            //					response.OnCompleted += () => {
            //						if (OnResponse != null)
            //							OnResponse (response);
            //					};

                    response.Read (() => {
                        if (OnResponse != null) OnResponse (response);
                    });
                });
            });
        }

Usage Example

Example #1
0
        public void Curl(IManosContext ctx, string url, string method, string auth, string [] header_keys = null)
        {
            // TODO: Should we set the Host header automagically?
            Uri u = null;
            if (!Uri.TryCreate (url, UriKind.Absolute, out u)) {
                CurlError (ctx, "Url is invalid: {0}", url);
                return;
            }

            if (u.Scheme == "https") {
                CurlError (ctx, "https connections are not supported yet.");
                return;
            }

            {
                // TODO: Manos will handle this DNS stuff internally soon.
                IPAddress [] addrs = Dns.GetHostAddresses (u.Host);
                if (addrs.Length == 0) {
                    CurlError (ctx, "Could not resolve host: {0}", u.Host);
                    return;
                }

                UriBuilder builder = new UriBuilder (u);
                builder.Host = addrs [0].ToString ();

                url = builder.ToString ();
            }

            HttpRequest r = new HttpRequest (url) {
                Method = GetHttpMethod (method),
            };

            r.Headers.SetNormalizedHeader ("Host", u.Host);

            if (header_keys != null) {
                var header_vals = ctx.Request.Data.GetList ("header_vals");
                for (int i = 0; i < header_keys.Length; i++) {
               		r.Headers.SetHeader (header_keys [i], header_vals [i].SafeValue);
                }
            }

            if (auth == "basic")
                HttpUtility.AddBasicAuthentication (r, ctx.Request.Data ["username"], ctx.Request.Data ["password"]);

            AddBody (ctx, r);

            r.OnResponse += (response) => {

                var res = new Dictionary<object,object> ();
                var response_md = new StringBuilder ();
                var request_md = new StringBuilder ();

                r.WriteMetadata (request_md);
                response.WriteMetadata (response_md);

                res ["header"] = PrettyPrintMetadata (response_md.ToString ());
                res ["request"]  = PrettyPrintMetadata (request_md.ToString ()) + PrintBody (r.GetBody ());
                res ["body"] = "<div><pre>" + HttpUtility.HtmlEncode (response.PostBody) + "</pre></div>";

                ctx.Response.End (JSON.JsonEncode (res));
            };

            try {
                r.Execute ();
            } catch (Exception e) {
                CurlError (ctx, e.Message);
            }
        }
All Usage Examples Of Manos.Http.HttpRequest::Execute