コード例 #1
0
ファイル: RestClient.cs プロジェクト: StolenRuby/consortium
        // just sync post data, ignoring result
        public void POSTRequest(byte[] src, IServiceAuth auth)
        {
            try
            {
                _request               = (HttpWebRequest)WebRequest.Create(buildUri());
                _request.ContentType   = "application/xml";
                _request.Timeout       = 90000;
                _request.Method        = "POST";
                _asyncException        = null;
                _request.ContentLength = src.Length;
                if (auth != null)
                {
                    auth.AddAuthorization(_request.Headers);
                }
                else
                {
                    _request.AllowWriteStreamBuffering = false;
                }
            }
            catch (Exception e)
            {
                m_log.WarnFormat("[REST]: POST {0} failed with exception {1} {2}",
                                 _request.RequestUri, e.Message, e.StackTrace);
                return;
            }

            try
            {
                using (Stream dst = _request.GetRequestStream())
                {
                    dst.Write(src, 0, src.Length);
                }

                using (HttpWebResponse response = (HttpWebResponse)_request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(responseStream))
                        {
                            string responseStr = reader.ReadToEnd();
                            if (WebUtil.DebugLevel >= 5)
                            {
                                int reqnum = WebUtil.RequestNumber++;
                                WebUtil.LogOutgoingDetail("REST POST", responseStr);
                            }
                        }
                    }
                }
            }
            catch (WebException e)
            {
                m_log.WarnFormat("[REST]: POST {0} failed with status {1} and message {2}",
                                 _request.RequestUri, e.Status, e.Message);
                return;
            }
            catch (Exception e)
            {
                m_log.WarnFormat("[REST]: AsyncPOST {0} failed with exception {1} {2}",
                                 _request.RequestUri, e.Message, e.StackTrace);
                return;
            }
        }
コード例 #2
0
ファイル: RestClient.cs プロジェクト: osCore2/osCore2-1
        public Stream Request(Stream src, IServiceAuth auth)
        {
            _request               = (HttpWebRequest)WebRequest.Create(buildUri());
            _request.KeepAlive     = false;
            _request.ContentType   = "application/xml";
            _request.Timeout       = 90000;
            _request.Method        = RequestMethod;
            _asyncException        = null;
            _request.ContentLength = src.Length;
            if (auth != null)
            {
                auth.AddAuthorization(_request.Headers);
            }

            src.Seek(0, SeekOrigin.Begin);

            int reqnum = WebUtil.RequestNumber++;

            if (WebUtil.DebugLevel >= 3)
            {
                m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} REST {1} to {2}", reqnum, _request.Method, _request.RequestUri);
            }
            if (WebUtil.DebugLevel >= 5)
            {
                WebUtil.LogOutgoingDetail(string.Format("SEND {0}: ", reqnum), src);
            }


            try
            {
                using (Stream dst = _request.GetRequestStream())
                {
//                    m_log.Debug("[REST]: GetRequestStream is ok");

                    byte[] buf    = new byte[1024];
                    int    length = src.Read(buf, 0, 1024);
//                    m_log.Debug("[REST]: First Read is ok");
                    while (length > 0)
                    {
                        dst.Write(buf, 0, length);
                        length = src.Read(buf, 0, 1024);
                    }
                }

                _response = (HttpWebResponse)_request.GetResponse();
            }
            catch (WebException e)
            {
                m_log.WarnFormat("[REST]: Request {0} {1} failed with status {2} and message {3}",
                                 RequestMethod, _request.RequestUri, e.Status, e.Message);
                return(null);
            }
            catch (Exception e)
            {
                m_log.WarnFormat(
                    "[REST]: Request {0} {1} failed with exception {2} {3}",
                    RequestMethod, _request.RequestUri, e.Message, e.StackTrace);
                return(null);
            }

            if (WebUtil.DebugLevel >= 5)
            {
                using (Stream responseStream = _response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        string responseStr = reader.ReadToEnd();
                        WebUtil.LogResponseDetail(reqnum, responseStr);
                    }
                }
            }

            if (_response != null)
            {
                _response.Close();
            }

//            IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request);

            // TODO! Implement timeout, without killing the server
            // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
            //ThreadPool.RegisterWaitForSingleObject(responseAsyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true);

            return(null);
        }
コード例 #3
0
ファイル: RestClient.cs プロジェクト: StolenRuby/consortium
        /// <summary>
        /// Perform a synchronous request
        /// </summary>
        public MemoryStream Request(IServiceAuth auth)
        {
            lock (_lock)
            {
                try
                {
                    _request             = (HttpWebRequest)WebRequest.Create(buildUri());
                    _request.ContentType = "application/xml";
                    _request.Timeout     = 90000;
                    _request.Method      = RequestMethod;
                    _asyncException      = null;
                    if (auth != null)
                    {
                        auth.AddAuthorization(_request.Headers);
                    }
                    else
                    {
                        _request.AllowWriteStreamBuffering = false;
                    }

                    if (WebUtil.DebugLevel >= 3)
                    {
                        m_log.DebugFormat("[REST CLIENT] {0} to {1}", _request.Method, _request.RequestUri);
                    }

                    using (_response = (HttpWebResponse)_request.GetResponse())
                    {
                        using (Stream src = _response.GetResponseStream())
                        {
                            int length = src.Read(_readbuf, 0, BufferSize);
                            while (length > 0)
                            {
                                _resource.Write(_readbuf, 0, length);
                                length = src.Read(_readbuf, 0, BufferSize);
                            }
                        }
                    }
                }
                catch (WebException e)
                {
                    using (HttpWebResponse errorResponse = e.Response as HttpWebResponse)
                    {
                        if (null != errorResponse && HttpStatusCode.NotFound == errorResponse.StatusCode)
                        {
                            // This is often benign. E.g., requesting a missing asset will return 404.
                            m_log.DebugFormat("[REST CLIENT] Resource not found (404): {0}", _request.Address.ToString());
                        }
                        else
                        {
                            m_log.Error(string.Format("[REST CLIENT] Error fetching resource from server: {0} ", _request.Address.ToString()), e);
                        }
                    }
                    return(null);
                }

                if (_asyncException != null)
                {
                    throw _asyncException;
                }

                if (_resource != null)
                {
                    _resource.Flush();
                    _resource.Seek(0, SeekOrigin.Begin);
                }

                if (WebUtil.DebugLevel >= 5)
                {
                    WebUtil.LogOutgoingDetail("[REST CLIENT]", _resource);
                }

                return(_resource);
            }
        }