コード例 #1
0
        public async Task <HTTPResult> PostAsync(string url, string data)
        {
            var result = new HTTPResult();

            try
            {
                using (var response = await _httpClient.PostAsync(new Uri(url), new StringContent(data, Encoding.UTF8, "application/json")))
                {
                    result.statusCode = response.StatusCode;
                    result.content    = await response.Content.ReadAsStringAsync();
                }
            }
            catch (Exception e)
            {
                _log.Error(string.Format("Exception caught executing POST {0}", url), e);
            }

            return(result);
        }
コード例 #2
0
        public async Task <HTTPResult> GetAsync(string url)
        {
            var result = new HTTPResult();

            try
            {
                using (var response = await _httpClient.GetAsync(new Uri(url)))
                {
                    result.statusCode = response.StatusCode;
                    result.content    = response.Content.ReadAsStringAsync().Result;
                }
            }
            catch (Exception e)
            {
                _log.Error($"Exception caught executing GET {url}", e);
            }

            return(result);
        }
コード例 #3
0
                public UniRx.IObservable <HTTPResult> Invoke(JToken payload = null)
                {
                    return(UniRx.Observable.Range(0, 1)
                           .Select(_ => {
                        if (is_connecting)
                        {
                            throw new Sas.Exception(Sas.ERRNO.REQUET_ALREADY_CONNECTING.ToErrCode());
                        }
                        return _;
                    })
                           .SelectMany(_ => {
                        mShared.Add(mReq);
                        if (payload == null)
                        {
                            return UniRx.Observable.Range(0, 1);
                        }
                        return mReq.GetRequestStreamAsObservable()
                        .Select(stream => {
                            mReq.ContentType = "application/json";
                            var packet = new PROTOCOL();
                            packet.ver = PROTOCOL.VERSION;
                            packet.serial = mShared.send_cnt + 1;
                            packet.utc = UnixDateTime.ToUnixTimeMilliseconds(System.DateTime.UtcNow);
                            packet.command = "post";
                            packet.payload = payload;
                            // new JArray(payload.ToList());
                            var pretty_json = Newtonsoft.Json.JsonConvert.SerializeObject(packet);

                            var bytes = System.Text.Encoding.UTF8.GetBytes(pretty_json);
                            stream.BeginWrite(bytes, 0, bytes.Length, (handle) => {
                                stream.EndWrite(handle);
                                stream.Dispose();
                            }, null);
                            return _;
                        });
                    })
                           .SelectMany(_ => {
                        return UniRx.Observable.Create <HTTPResult> (observer => {
                            var handle = mReq.GetResponseAsObservable()
                                         .Select(response => {
                                using (var ostream = response.GetResponseStream()) {
                                    using (System.IO.StreamReader reader = new System.IO.StreamReader(ostream)) {
                                        ostream.Flush();
                                        var data = reader.ReadToEnd();

                                        var ret = new HTTPResult();
                                        ret.req = mReq;
                                        ret.res = response;
                                        ret.param = payload;
                                        var root = JObject.Parse(data);
                                        ret.protocol = new PROTOCOL();
                                        ret.protocol.ver = (long)root["ver"];
                                        ret.protocol.utc = (long)root["utc"];
                                        ret.protocol.serial = (long)root["serial"];
                                        ret.protocol.command = (string)root["command"];
                                        ret.protocol.payload = root["payload"];

                                        if (ret.protocol.command == "error")
                                        {
                                            var jError = ret.protocol.GetPayload(token => {
                                                var obj = (JObject)token;
                                                var err = new Error {
                                                    code = (int)obj["errno"],
                                                    what = (string)obj["what"],
                                                };

                                                return err;
                                            });
                                            throw new Sas.Net.ExceptionRes(response, jError);
                                        }

                                        return ret;
                                    }
                                }
                            })
                                         .Delay(TimeSpan.FromMilliseconds(1), UniRx.Scheduler.MainThread)
                                         .Do(protocol => mShared.Del(protocol.req, protocol.res))
                                         .Subscribe(data => observer.OnNext(data),
                                                    err => {
                                var exception = new Net.ExceptionReq(mReq, payload, err);
                                UniRx.Observable.Range(0, 1)
                                .SubscribeOnMainThread()
                                .Do(__ => {
                                    mShared.Del(mReq, null, exception);
                                    throw exception;
                                })
                                .SubscribeOnMainThread()
                                .Subscribe(__ => { },
                                           inner => observer.OnError(inner));
                            },
                                                    () => observer.OnCompleted());

                            return UniRx.Disposable.Create(() => {
                                handle.Dispose();
                            });
                        });
                    }));
                }
コード例 #4
0
ファイル: AHTTPService.cs プロジェクト: subbuballa/Hermod
        /// <summary>
        /// Try to return a single optional HTTP parameter as UInt64.
        /// </summary>
        /// <param name="Name">The name of the parameter.</param>
        /// <param name="HTTPResult">The HTTPResult.</param>
        /// <returns>True if the parameter exist; False otherwise.</returns>
        protected Boolean TryGetParameter_UInt64_(String Name, out HTTPResult<UInt64> HTTPResult)
        {
            List<String> _StringValues = null;

            if (IHTTPConnection.RequestHeader.QueryString != null)
                if (IHTTPConnection.RequestHeader.QueryString.TryGetValue(Name, out _StringValues))
                {

                    UInt64 _Value;

                    if (UInt64.TryParse(_StringValues[0], out _Value))
                    {
                        HTTPResult = new HTTPResult<UInt64>(_Value);
                        return true;
                    }

                    else
                    {
                        HTTPResult = new HTTPResult<UInt64>(IHTTPConnection.RequestHeader, HTTPStatusCode.BadRequest, "The given optional parameter '" + Name + "' is invalid!");
                        return true;
                    }

                }

            HTTPResult = new HTTPResult<UInt64>(default(UInt64));
            return false;
        }