Esempio n. 1
0
        public NodeHttpRequest(object options, DynamicObject callback = null)
        {
            _options = options as NodeHttpRequestOptions ?? new NodeHttpRequestOptions((dynamic)options);
            if (!string.IsNullOrEmpty(_options.proxy))
            {
                try
                {
                    Proxy = new WebProxy(_options.proxy.ToLower().Contains("http://") ? _options.proxy : "http://" + _options.proxy, true);
                }
                catch (Exception)
                {
                }
                var handler = new HttpClientHandler();
                if (Proxy == null)
                {
                    handler.UseProxy = false;
                }
                else
                {
                    handler.Proxy = Proxy;
                }
            }


            if (callback != null)
            {
                @on("response", callback);
            }
        }
Esempio n. 2
0
        internal static void MakeRequest(DynamicObject config, DynamicObject callback, Microsoft.ClearScript.V8.V8ScriptEngine engine)
        {
            var options = new NodeHttpRequestOptions(config);
            var uriObj  = new Uri((config.GetMember <object>("uri") ?? config.GetMember <object>("url")).ToString());

            options.url      = (config.GetMember <string>("uri") ?? config.GetMember <string>("url"));
            options.host     = uriObj.Host;
            options.hostname = uriObj.Host;
            options.scheme   = uriObj.Scheme;
            options.path     = uriObj.PathAndQuery;
            options.port     = uriObj.Port;
            options.method   = config.GetMember("method", "GET");
            options.headers  = config.GetMember <DynamicObject>("headers");
            bool isJson = config.GetMember("json", false);

            var req = new NodeHttpRequest(options);
            Action <NodeHttpResponse> wrapperCallback = resp =>
            {
                if (callback == null)
                {
                    return;
                }
                //    string body = null;
                object body    = null;
                var    apiResp = resp.body as string;
                if (isJson)
                {
                    var parser = (dynamic)engine.Evaluate("JSON.parse");
                    body = parser(apiResp);
                }
                else
                {
                    body = apiResp;
                }

                callback.AsDynamic().call(null, null, resp, body);
            };

            req.@on("response", wrapperCallback);

            req.end();
        }