/// <summary> /// Perform a synchronous request /// </summary> public Stream Request(IServiceAuth auth) { lock (_lock) { _request = (HttpWebRequest)WebRequest.Create(buildUri()); _request.KeepAlive = false; _request.ContentType = "application/xml"; _request.Timeout = 200000; _request.Method = RequestMethod; _asyncException = null; if (auth != null) { auth.AddAuthorization(_request.Headers); } int reqnum = WebUtil.RequestNumber++; if (WebUtil.DebugLevel >= 3) { m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} REST {1} to {2}", reqnum, _request.Method, _request.RequestUri); } // IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request); try { 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); } // 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); // _allDone.WaitOne(); } } } 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.LogResponseDetail(reqnum, _resource); } return(_resource); } }
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); }
/// <summary> /// Perform a synchronous request /// </summary> public Stream Request(IServiceAuth auth) { lock (_lock) { _request = (HttpWebRequest)WebRequest.Create(buildUri()); _request.ContentType = "application/xml"; _request.Timeout = 90000; _request.Method = RequestMethod; _asyncException = null; if (auth != null) { auth.AddAuthorization(_request.Headers); } int reqnum = WebUtil.RequestNumber++; if (WebUtil.DebugLevel >= 3) { m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} REST {1} to {2}", reqnum, _request.Method, _request.RequestUri); } try { 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.LogResponseDetail(reqnum, _resource); } return(_resource); } }