internal override void DeseralizeFromStream() { DiagnoseHelper.CheckReference(ResponseStream, "Can not get response stream"); ResponseStream.Seek(0, SeekOrigin.Begin); ContentTypeEntity ct = new ContentTypeEntity(OriginalResponse.ContentType); int contentLen = (int)OriginalResponse.ContentLength; //DiagnoseHelper.CheckStringIgnoreCase(ct.ContentType, HttpLayer.JSonContentType, "Login response content type is not correct"); byte[] buffer = null; if (contentLen != -1) { buffer = new byte[contentLen]; int leftLen = contentLen; while (leftLen != 0) { int readLen = ResponseStream.Read(buffer, 0, leftLen); leftLen -= readLen; if (leftLen != 0) { LogHelper.OnlineLogger.Debug("ToDo:: can not read all data in once, need to sleep!" + HttpLayer.LogRequetId(ResponseId)); } } } else { List <byte> buffList = new List <byte>(); int tempBufLen = 10 * 1024; byte[] bufTemp = new byte[tempBufLen]; int readLen = 0; while ((readLen = ResponseStream.Read(bufTemp, 0, tempBufLen)) != 0) { if (readLen == tempBufLen) { buffList.AddRange(bufTemp); } else { buffList.AddRange(bufTemp.Take(readLen)); } } buffer = buffList.ToArray(); contentLen = buffList.Count; } string sResult = ct.Encoding.GetString(buffer, 0, contentLen); DiagnoseHelper.CheckString(sResult, "Login response content is empty"); this.Object = JsonConvert.DeserializeObject <JObject>(sResult); }
public JSonRequest(string uri, JObject jObject, string method = HttpConst.HttpMethod_Post) : base(uri, method) { #if DEBUG Object = jObject; #endif DiagnoseHelper.CheckArgument(jObject, "jObject can not be null"); Serialize2Stream(jObject); }
public void CancelRequest(Request request) { DiagnoseHelper.CheckArgument(request, "request"); lock (cacheLocker) { cancelledCache.Add(request); } cacheChangedNotifier.Set(); }
public StreamBufferEnumerator(int id, Stream stream, long streamLen, int chunkSize = BlockSize) { this.logId = id; DiagnoseHelper.CheckArgument(stream, "target stream can not null"); LogHelper.OnlineLogger.Debug(string.Format("Create stream enumerator, length={0}, chunksize={1}, {2}", streamLen, chunkSize, HttpLayer.LogRequetId(logId))); ChunkSize = chunkSize; targetStreams = new StreamCollection(stream); streamLength = streamLen; Reset(); }
public void AddRequest(Request request) { DiagnoseHelper.CheckArgument(request, "request"); request.IsSending = false; request.PrepareLowLevelRequest(); DiagnoseHelper.CheckReference(request.HttpRequest, "Can not convert http request from specific request"); DiagnoseHelper.CheckReference(request.Response, "Can not create response from specific request"); lock (cacheLocker) { addedCache.Add(request); } cacheChangedNotifier.Set(); }
public void CancelRequest(HttpResponse response) { DiagnoseHelper.CheckArgument(response, "Can not cancel empty response"); response.IsStopped = true; }
public void CancelRequest(HttpRequest request) { DiagnoseHelper.CheckArgument(request, "Can not cancel empty request"); request.Stop(); }
public void SendRequest(HttpRequest request) { DiagnoseHelper.CheckArgument(request, "Can not cancel empty request"); DiagnoseHelper.CheckReference(request.Context, "You should set context"); HttpWebRequest originalReq = request.OriginalRequest; originalReq.UseDefaultCredentials = true; //originalReq.ReadWriteTimeout = streamAccessTimeout; string additinalCookie = null; if (request.Headers != null) { if (originalReq.Headers == null) { originalReq.Headers = new WebHeaderCollection(); } foreach (var pairHeader in request.Headers) { if (pairHeader.Key == HttpHeaderHelper.cookieKey) { additinalCookie = pairHeader.Value; continue; } originalReq.Headers[pairHeader.Key] = pairHeader.Value; } } SetAuthentication(originalReq); SaveCookies(originalReq, additinalCookie); //SetUserAgent(originalReq); if (request.RequestStreams != null) { try { LogHelper.OnlineLogger.Info(string.Format("Send Http Request Stream to {0}. {1}", originalReq.RequestUri.ToString(), LogRequetId(request.RequestId))); var result = originalReq.BeginGetRequestStream(new AsyncCallback(OnRequestStreamReady), request); LogHelper.OnlineLogger.Info(string.Format("Sent {0}. {1}", originalReq.RequestUri.ToString(), LogRequetId(request.RequestId))); AddRequestTimer(request); } catch (Exception ex) { LogHelper.OnlineLogger.Error(string.Format("Can not send http request \n{0}" + LogRequetId(request.RequestId), ex.ToString())); request.Exception = ex; ReportFailed(request); } } else { try { LogHelper.OnlineLogger.Info(string.Format("Send Http Request to {0}. {1}", originalReq.RequestUri.ToString(), LogRequetId(request.RequestId))); var result = originalReq.BeginGetResponse(new AsyncCallback(OnResponseReady), request); LogHelper.OnlineLogger.Info(string.Format("Sent {0}. {1}", originalReq.RequestUri.ToString(), LogRequetId(request.RequestId))); AddRequestTimer(request); } catch (Exception ex) { LogHelper.OnlineLogger.Error(string.Format("Can not begin get http response. {0}" + LogRequetId(request.RequestId), ex.ToString())); request.Exception = ex; ReportFailed(request); } } }