public override void StartHandleRequestBody(Stream outputStream, EndRequestBody endRequestBody)
        {
            RequestBodyState requestBodyState = new RequestBodyState();

            requestBodyState.endRequestBody = endRequestBody;
            requestBodyState.outputStream   = outputStream;
            requestBodyState.buffer         = data;
            int count = (int)(contentLength > SEGMENT_SIZE ? SEGMENT_SIZE : contentLength);

            try
            {
                requestBodyState.complete = count;
                outputStream.BeginWrite(requestBodyState.buffer, 0, count, AsyncStreamCallback, requestBodyState);
            }
            catch (Exception ex)
            {
                if (outputStream != null)
                {
                    outputStream.Close();
                }
                QLog.E(TAG, ex.Message, ex);
                requestBodyState.endRequestBody(ex);
                requestBodyState = null;
            }
        }
Beispiel #2
0
        private void AsyncStreamCallback(IAsyncResult ar)
        {
            RequestBodyState requestBodyState = ar.AsyncState as RequestBodyState;
            Stream           outputStream;

            try
            {
                outputStream = requestBodyState.outputStream;
                outputStream.EndWrite(ar);
                if (progressCallback != null)
                {
                    UpdateProgress(requestBodyState.complete, contentLength);
                }
                if (requestBodyState.complete < contentLength)
                {
                    long remain = contentLength - requestBodyState.complete;
                    int  count  = (int)(remain > SEGMENT_SIZE ? SEGMENT_SIZE : remain);
                    int  offset = (int)requestBodyState.complete;
                    requestBodyState.complete += count;
                    outputStream.BeginWrite(requestBodyState.buffer, offset, count, AsyncStreamCallback, requestBodyState);
                }
                else
                {
                    //write over
                    requestBodyState.endRequestBody(null);
                }
            }
            catch (Exception ex)
            {
                QLog.E(TAG, ex.Message, ex);
                requestBodyState.endRequestBody(ex);;
            }
        }
Beispiel #3
0
        private void AsyncStreamCallback(IAsyncResult ar)
        {
            RequestBodyState requestBodyState = ar.AsyncState as RequestBodyState;
            Stream           outputStream;

            try
            {
                outputStream = requestBodyState.outputStream;
                outputStream.EndWrite(ar);
                if (progressCallback != null)
                {
                    UpdateProgress(requestBodyState.complete, realContentLength);
                }
                if (requestBodyState.complete < realContentLength)
                {
                    if (srcPath != null)
                    {
                        long remain = realContentLength - requestBodyState.complete;
                        int  count  = fileStream.Read(requestBodyState.buffer, 0, (int)(requestBodyState.buffer.Length > remain ? remain : requestBodyState.buffer.Length));
                        requestBodyState.complete += count;
                        outputStream.BeginWrite(requestBodyState.buffer, 0, count, AsyncStreamCallback, requestBodyState);
                    }
                    else if (data != null)
                    {
                        long remain = realContentLength - requestBodyState.complete;
                        int  count  = (int)(remain > SEGMENT_SIZE ? SEGMENT_SIZE : remain);
                        int  offset = (int)requestBodyState.complete;
                        requestBodyState.complete += count;
                        outputStream.BeginWrite(requestBodyState.buffer, offset, count, AsyncStreamCallback, requestBodyState);
                    }

                    outputStream.Flush();
                }
                else
                {
                    if (fileStream != null)
                    {
                        fileStream.Close();
                    }
                    WriteEndLine(outputStream);
                    outputStream.Flush();
                    //write over
                    requestBodyState.endRequestBody(null);
                }
            }
            catch (Exception ex)
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                }
                QLog.E(TAG, ex.Message, ex);
                requestBodyState.endRequestBody(ex);
            }
        }
        /// <summary>
        /// async to excute
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response"></param>
        /// <param name="config"></param>
        public static void Schedue(Request request, Response response, HttpClientConfig config)
        {
            HttpWebRequest httpWebRequest = null;
            RequestState   requestState   = new RequestState();

            try
            {
                requestState.request = request;

                requestState.response = response;

                httpWebRequest = WebRequest.Create(request.RequestUrlString) as HttpWebRequest;

                httpWebRequest.AllowWriteStreamBuffering = false;

                //bind webRequest
                request.BindHttpWebRequest(httpWebRequest);

                //handle request header
                HandleHttpWebRequestHeaders(request, httpWebRequest, config);

                requestState.httpWebRequest = httpWebRequest;

                //handle request body
                if (request.Body != null)
                {
                    httpWebRequest.ContentLength = request.Body.ContentLength;
                    httpWebRequest.BeginGetRequestStream(new AsyncCallback(AsyncRequestCallback), requestState);
                }
                else
                {
                    //wait for response
                    httpWebRequest.BeginGetResponse(new AsyncCallback(AsyncResponseCallback), requestState);
                }

                //print log
                PrintReqeustInfo(httpWebRequest);
            }
            catch (WebException webEx)
            {
                response.OnFinish(false, webEx);
                //abort
                requestState.Clear();
                QLog.D(TAG, webEx.Message, webEx);
            }
            catch (Exception ex)
            {
                response.OnFinish(false, ex);
                //abort
                requestState.Clear();
                QLog.E(TAG, ex.Message, ex);
            }
        }
Beispiel #5
0
        public override void OnWrite(Stream outputStream)
        {
            FileStream fileStream = null;

            try
            {
                byte[] buffer    = new byte[SEGMENT_SIZE];
                int    bytesRead = 0;
                long   completed = bytesRead;
                fileStream = new FileStream(srcPath, FileMode.Open, FileAccess.Read);
                fileStream.Seek(fileOffset, SeekOrigin.Begin);//seek to designated position
                long remain = contentLength - completed;
                if (remain > 0)
                {
                    while ((bytesRead = fileStream.Read(buffer, 0, (int)(buffer.Length > remain ? remain : buffer.Length))) != 0)
                    {
                        outputStream.Write(buffer, 0, bytesRead);
                        outputStream.Flush();
                        completed += bytesRead;
                        if (progressCallback != null)
                        {
                            UpdateProgress(completed, contentLength);
                        }
                        remain = contentLength - completed;
                        if (remain == 0)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    if (progressCallback != null)
                    {
                        UpdateProgress(completed, contentLength);
                    }
                }
            }
            catch (Exception ex)
            {
                QLog.E(TAG, ex.Message, ex);
                throw;
            }
            finally
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                }
            }
        }
Beispiel #6
0
        private void AsyncStreamCallback(IAsyncResult ar)
        {
            RequestBodyState requestBodyState = ar.AsyncState as RequestBodyState;
            Stream           outputStream     = null;

            try
            {
                outputStream = requestBodyState.outputStream;
                outputStream.EndWrite(ar);
                outputStream.Flush();
                if (progressCallback != null)
                {
                    UpdateProgress(requestBodyState.complete, contentLength);
                }
                if (requestBodyState.complete < contentLength)
                {
                    long remain = contentLength - requestBodyState.complete;
                    int  count  = fileStream.Read(requestBodyState.buffer, 0, (int)(requestBodyState.buffer.Length > remain ? remain : requestBodyState.buffer.Length));
                    requestBodyState.complete += count;
                    outputStream.BeginWrite(requestBodyState.buffer, 0, count, AsyncStreamCallback, requestBodyState);
                }
                else
                {
                    if (fileStream != null)
                    {
                        fileStream.Close();
                    }
                    if (outputStream != null)
                    {
                        outputStream.Close();
                    }
                    //write over
                    requestBodyState.endRequestBody(null);
                    requestBodyState = null;
                }
            }
            catch (Exception ex)
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                }
                if (outputStream != null)
                {
                    outputStream.Close();
                }
                QLog.E(TAG, ex.Message, ex);
                requestBodyState.endRequestBody(ex);
                requestBodyState = null;
            }
        }
        public override void StartHandleRequestBody(Stream outputStream, EndRequestBody endRequestBody)
        {
            RequestBodyState requestBodyState = new RequestBodyState();

            requestBodyState.endRequestBody = endRequestBody;
            requestBodyState.outputStream   = outputStream;
            requestBodyState.complete       = 0;
            int size = (int)(contentLength > SEGMENT_SIZE ? SEGMENT_SIZE : contentLength);

            fileStream = new FileStream(srcPath, FileMode.Open, FileAccess.Read);
            fileStream.Seek(fileOffset, SeekOrigin.Begin);
            try
            {
                requestBodyState.buffer = new byte[size];// 4kb
                long remain = contentLength - requestBodyState.complete;
                if (remain > 0)
                {
                    int count = fileStream.Read(requestBodyState.buffer, 0, (int)(requestBodyState.buffer.Length > remain ? remain : requestBodyState.buffer.Length));
                    requestBodyState.complete = count;
                    outputStream.BeginWrite(requestBodyState.buffer, 0, count, AsyncStreamCallback, requestBodyState);
                }
                else
                {
                    if (fileStream != null)
                    {
                        fileStream.Close();
                    }
                    if (outputStream != null)
                    {
                        outputStream.Close();
                    }
                    //write over
                    requestBodyState.endRequestBody(null);
                    requestBodyState = null;
                }
            }
            catch (Exception ex)
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                }
                if (outputStream != null)
                {
                    outputStream.Close();
                }
                QLog.E(TAG, ex.Message, ex);
                requestBodyState.endRequestBody(ex);
                requestBodyState = null;
            }
        }
 /// <summary>
 /// handle cos response
 /// </summary>
 /// <param name="inputStream"></param>
 /// <exception cref="CosServerException"> throw CosServerException </exception>
 /// <exception cref="Exception">throw Exception</exception>
 public void HandleResponseBody(Stream inputStream)
 {
     try
     {
         if (isDownload)
         {
             fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
             fileStream.Seek(fileOffset, SeekOrigin.Begin);
             byte[] buffer    = new byte[SEGMENT_SIZE];
             int    recvLen   = 0;
             long   completed = recvLen;
             while ((recvLen = inputStream.Read(buffer, 0, buffer.Length)) != 0)
             {
                 fileStream.Write(buffer, 0, recvLen);
                 if (progressCallback != null)
                 {
                     completed += recvLen;
                     progressCallback(completed, contentLength);
                 }
             }
             fileStream.Flush();
         }
         else
         {
             if (parseStream != null)
             {
                 parseStream(inputStream, contentType, contentLength);
             }
         }
     }
     catch (Exception ex)
     {
         QLog.E("ResponseBody", ex.Message, ex);
         throw;
     }
     finally
     {
         if (inputStream != null)
         {
             inputStream.Close();
             inputStream.Dispose();
         }
         if (fileStream != null)
         {
             fileStream.Close();
             fileStream.Dispose();
         }
         //if (memoryStream != null) memoryStream.Close();
     }
 }
        public void StartHandleResponseBody(Stream inputStream, EndResponseBody endResponseBody)
        {
            ResponseBodyState responseBodyState = new ResponseBodyState();

            responseBodyState.inputStream     = inputStream;
            responseBodyState.endResponseBody = endResponseBody;
            responseBodyState.completed       = 0L;
            try
            {
                int    count  = (int)((contentLength > SEGMENT_SIZE || contentLength <= 0) ? SEGMENT_SIZE : contentLength);
                byte[] buffer = new byte[count];
                responseBodyState.buffer = buffer;
                if (isDownload)
                {
                    fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
                    fileStream.Seek(fileOffset, SeekOrigin.Begin);
                    responseBodyState.inputStream.BeginRead(responseBodyState.buffer, 0, responseBodyState.buffer.Length, AsyncStreamCallback, responseBodyState);
                }
                else
                {
                    memoryStream             = new MemoryStream(count);
                    responseBodyState.buffer = buffer;
                    responseBodyState.inputStream.BeginRead(responseBodyState.buffer, 0, responseBodyState.buffer.Length, AsyncStreamCallback, responseBodyState);
                }
            }
            catch (Exception ex)
            {
                responseBodyState.endResponseBody(false, ex);
                responseBodyState.Clear();
                if (fileStream != null)
                {
                    fileStream.Close();
                    fileStream.Dispose();
                }
                if (memoryStream != null)
                {
                    memoryStream.Close();
                    memoryStream.Dispose();
                }
                QLog.E("ResponseBody", ex.Message, ex);
            }
        }
Beispiel #10
0
        public static void AsyncRequestCallback(IAsyncResult ar)
        {
            RequestState requestState  = ar.AsyncState as RequestState;
            Stream       requestStream = null;

            try
            {
                HttpWebRequest httpWebRequest = requestState.httpWebRequest;
                requestStream = httpWebRequest.EndGetRequestStream(ar);

                ////开始写入数据
                //requestState.request.Body.OnWrite(requestStream);

                ////wait for response
                //httpWebRequest.BeginGetResponse(AsyncResponseCallback, requestState);

                requestState.request.Body.StartHandleRequestBody(requestStream, delegate(Exception exception)
                {
                    if (exception != null)
                    {
                        // handle request body throw exception
                        requestState.response.OnFinish(false, exception);
                        //abort
                        requestState.Clear();
                        QLog.E(TAG, exception.Message, exception);
                    }
                    else
                    {
                        //wait for response
                        httpWebRequest.BeginGetResponse(new AsyncCallback(AsyncResponseCallback), requestState);
                    }
                });
            }
            catch (Exception ex)
            {
                requestState.response.OnFinish(false, ex);
                //abort
                requestState.Clear();
                QLog.E(TAG, ex.Message, ex);
            }
        }
Beispiel #11
0
 public override string GetMD5()
 {
     try
     {
         fileStream = new FileStream(srcPath, FileMode.Open, FileAccess.Read);
         fileStream.Seek(fileOffset, SeekOrigin.Begin);
         return(DigestUtils.GetMd5ToBase64(fileStream, contentLength));
     }
     catch (Exception ex)
     {
         QLog.E(TAG, ex.Message, ex);
         throw;
     }
     finally
     {
         if (fileStream != null)
         {
             fileStream.Close();
         }
     }
 }
Beispiel #12
0
        public override void StartHandleRequestBody(Stream outputStream, EndRequestBody endRequestBody)
        {
            requestBodyState = new RequestBodyState();
            requestBodyState.endRequestBody = endRequestBody;
            requestBodyState.outputStream   = outputStream;
            requestBodyState.complete       = 0;

            //write paramters
            WriteParameters(outputStream);

            //写入content-disposition: form-data; name = "file"; filename = "xxx"\r\n
            WriteFileParameters(outputStream);

            outputStream.Flush();


            int size = (int)(realContentLength > SEGMENT_SIZE ? SEGMENT_SIZE : realContentLength);

            if (srcPath != null)
            {
                try
                {
                    fileStream = new FileStream(srcPath, FileMode.Open, FileAccess.Read);
                    fileStream.Seek(fileOffset, SeekOrigin.Begin);
                    requestBodyState.buffer = new byte[size];// 4kb
                    long remain = realContentLength - requestBodyState.complete;
                    if (remain > 0)
                    {
                        int count = fileStream.Read(requestBodyState.buffer, 0, (int)(requestBodyState.buffer.Length > remain ? remain : requestBodyState.buffer.Length));
                        requestBodyState.complete = count;
                        outputStream.BeginWrite(requestBodyState.buffer, 0, count, AsyncStreamCallback, requestBodyState);
                    }
                    else
                    {
                        if (fileStream != null)
                        {
                            fileStream.Close();
                        }
                        WriteEndLine(outputStream);
                        outputStream.Flush();
                        //write over
                        requestBodyState.endRequestBody(null);
                    }
                }
                catch (Exception ex)
                {
                    if (fileStream != null)
                    {
                        fileStream.Close();
                    }
                    QLog.E(TAG, ex.Message, ex);
                    requestBodyState.endRequestBody(ex);
                }
            }
            else if (data != null)
            {
                requestBodyState.buffer = data;
                try
                {
                    requestBodyState.complete = size;
                    outputStream.BeginWrite(requestBodyState.buffer, 0, size, AsyncStreamCallback, requestBodyState);
                }
                catch (Exception ex)
                {
                    QLog.E(TAG, ex.Message, ex);
                    requestBodyState.endRequestBody(ex);
                }
            }
        }
Beispiel #13
0
        public static void AsyncResponseCallback(IAsyncResult ar)
        {
            RequestState    requestState    = ar.AsyncState as RequestState;
            HttpWebResponse httpWebResponse = null;

            try
            {
                HttpWebRequest httpWebRequest = requestState.httpWebRequest;
                httpWebResponse = (HttpWebResponse)httpWebRequest.EndGetResponse(ar);

                //nofity get response
                requestState.request.onNotifyGetResponse();

                requestState.httpWebResponse = httpWebResponse;
                //handle response headers
                HandleHttpWebResponseHeaders(requestState.response, httpWebResponse);

                Stream responseStream = httpWebResponse.GetResponseStream();

                requestState.response.Body.StartHandleResponseBody(responseStream, delegate(bool isSuccess, Exception ex)
                {
                    PrintResponseInfo(httpWebResponse);
                    requestState.response.OnFinish(isSuccess, ex);
                    requestState.Clear();
                });
            }
            catch (WebException webEx)
            {
                if (webEx.Response != null && webEx.Response is HttpWebResponse)
                {
                    //nofity get response
                    requestState.request.onNotifyGetResponse();

                    //handle response for [400, 500]
                    httpWebResponse = (HttpWebResponse)webEx.Response;

                    requestState.httpWebResponse = httpWebResponse;

                    //handle response headers
                    HandleHttpWebResponseHeaders(requestState.response, httpWebResponse);

                    Stream responseStream = httpWebResponse.GetResponseStream();

                    requestState.response.Body.StartHandleResponseBody(responseStream, delegate(bool isSuccess, Exception ex)
                    {
                        PrintResponseInfo(httpWebResponse);
                        requestState.response.OnFinish(isSuccess, ex);
                        requestState.Clear();
                    });
                }
                else
                {
                    requestState.response.OnFinish(false, webEx);
                    //abort
                    requestState.Clear();
                    QLog.E(TAG, webEx.Message, webEx);
                }
            }
            catch (Exception ex)
            {
                requestState.response.OnFinish(false, ex);
                //abort
                requestState.Clear();
                QLog.E(TAG, ex.Message, ex);
            }
        }
        private void AsyncStreamCallback(IAsyncResult ar)
        {
            ResponseBodyState responseBodyState = ar.AsyncState as ResponseBodyState;
            Stream            inputStream       = responseBodyState.inputStream;

            try
            {
                int recvLen = inputStream.EndRead(ar);
                responseBodyState.completed += recvLen;
                if (recvLen > 0)
                {
                    if (isDownload)
                    {
                        fileStream.Write(responseBodyState.buffer, 0, recvLen);
                        if (progressCallback != null)
                        {
                            progressCallback(responseBodyState.completed, contentLength);
                        }
                    }
                    else
                    {
                        memoryStream.Write(responseBodyState.buffer, 0, recvLen);
                    }

                    inputStream.BeginRead(responseBodyState.buffer, 0, responseBodyState.buffer.Length, AsyncStreamCallback, responseBodyState);
                }
                else if (recvLen == 0)
                {
                    if (isDownload)
                    {
                        fileStream.Flush();
                    }
                    else
                    {
                        memoryStream.Seek(0, SeekOrigin.Begin);
                        parseStream(memoryStream, contentType, responseBodyState.completed);
                    }

                    responseBodyState.endResponseBody(true, null);
                    responseBodyState.Clear();
                    if (fileStream != null)
                    {
                        fileStream.Close();
                        fileStream.Dispose();
                    }
                    if (memoryStream != null)
                    {
                        memoryStream.Close();
                        memoryStream.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                responseBodyState.endResponseBody(false, ex);
                responseBodyState.Clear();
                if (fileStream != null)
                {
                    fileStream.Close();
                    fileStream.Dispose();
                }
                if (memoryStream != null)
                {
                    memoryStream.Close();
                    memoryStream.Dispose();
                }
                QLog.E("ResponseBody", ex.Message, ex);
            }
        }
Beispiel #15
0
        public virtual void ParseResponseBody(Stream inputStream, string contentType, long contentLength)
        {
            StringBuilder contentBuilder = new StringBuilder();
            StreamReader  streamReader   = new StreamReader(inputStream);
            string        line           = streamReader.ReadLine();

            while (line != null)
            {
                contentBuilder.Append(line);
                line = streamReader.ReadLine();
            }
            string content = contentBuilder.ToString();

            QLog.D("STSQCloudCredentialProvider", content);
            JsonTextReader jsonTextReader      = new JsonTextReader(new StringReader(content));
            Dictionary <string, string> result = new Dictionary <string, string>();
            string key   = null;
            string value = null;

            while (jsonTextReader.Read())
            {
                switch (jsonTextReader.TokenType)
                {
                case JsonToken.PropertyName:
                    key = (string)jsonTextReader.Value;
                    break;

                case JsonToken.Integer:
                case JsonToken.Float:
                case JsonToken.String:
                case JsonToken.Boolean:
                    value = jsonTextReader.Value.ToString();
                    result.Add(key, value);
                    break;
                }
            }
            jsonTextReader.Close();
            foreach (KeyValuePair <string, string> pair in result)
            {
                if ("sessionToken".Equals(pair.Key, StringComparison.OrdinalIgnoreCase))
                {
                    this.token = pair.Value;
                }
                else if ("tmpSecretId".Equals(pair.Key, StringComparison.OrdinalIgnoreCase))
                {
                    this.tmpSecretId = pair.Value;
                }
                else if ("tmpSecretKey".Equals(pair.Key, StringComparison.OrdinalIgnoreCase))
                {
                    this.tmpSecretKey = pair.Value;
                }
                else if ("expiredTime".Equals(pair.Key, StringComparison.OrdinalIgnoreCase))
                {
                    this.keyTime = String.Format("{0};{1}", TimeUtils.GetCurrentTime(TimeUnit.SECONDS), pair.Value);
                }
                else if ("code".Equals(pair.Key, StringComparison.OrdinalIgnoreCase))
                {
                    if (!"0".Equals(pair.Value, StringComparison.OrdinalIgnoreCase))
                    {
                        QLog.E("XIAO", "get acm error");
                        break;
                    }
                }
            }
        }