Example #1
0
        public static void Main(string[] args)
        {
            Byte[] bytes = System.IO.File.ReadAllBytes(fileName);
            String file  = Convert.ToBase64String(bytes);

            JsonData data = new JsonData();

            data.model    = "cvlizer_3_0";
            data.language = "de";
            data.filename = fileName;
            data.data     = file;

            string json = JsonConvert.SerializeObject(data);

            byte[] jsonBytes = System.Text.Encoding.UTF8.GetBytes(json);

            System.Net.HttpWebRequest req = System.Net.WebRequest.Create("https://cvlizer.joinvision.com/cvlizer/rest/v1/extract/xml/")
                                            as System.Net.HttpWebRequest;

            req.Method      = "POST";
            req.ContentType = "application/json";
            req.Headers[System.Net.HttpRequestHeader.ContentLength] = jsonBytes.Length.ToString();
            req.Headers[System.Net.HttpRequestHeader.Authorization] = "Bearer " + productToken;

            //req.Headers.Add("Authorization", "Bearer " + < Product - Token >);
            //req.ContentLength = jsonBytes.Length;

            using (System.IO.Stream post = req.GetRequestStreamAsync().Result) // todo please don't block the tread
            {
                post.Write(jsonBytes, 0, jsonBytes.Length);
            }

            string result = null;

            using (System.Net.HttpWebResponse resp = req.GetResponseAsync().Result
                                                     as System.Net.HttpWebResponse)
            {
                System.IO.StreamReader reader =
                    new System.IO.StreamReader(resp.GetResponseStream());
                result = reader.ReadToEnd();
            }
            Console.Write(result);
            File.WriteAllText("cvlizer.xml", result);
            Console.ReadLine();
        }
Example #2
0
        public T ProcessRequest(string token, string logOn, string password, System.Net.Cookie cookie)
        {
            Uri uri = ToUrl(token);

            System.Net.HttpWebRequest wr = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
            wr.Credentials     = new System.Net.NetworkCredential(logOn, password);
            wr.CookieContainer = new System.Net.CookieContainer();
            if (cookie != null)
            {
                var cookiUri = cookie.Domain != null ? new Uri(cookie.Domain) : BaseUrl;
                wr.CookieContainer.SetCookies(uri, cookie.ToString());
            }

            OnProcessingRequest(wr);

#if !PORTABLE
            using (var response = wr.GetResponse())
                using (var stream = response.GetResponseStream())
#else
            using (var stream = wr.GetRequestStreamAsync().Result)
#endif
                {
                    if (stream == null)
                    {
                        throw new InvalidOperationException("Response stream is null");
                    }

                    var sr         = new System.IO.StreamReader(stream);
                    var jsonResult = sr.ReadToEnd();

                    var result = JsonParser.ParseJsonResult(jsonResult);

                    if (result != null && result.CacheId != 0)
                    {
                        _cacheId = result.CacheId;
                    }

                    var ret = new T {
                        Result = result
                    };
                    OnProcessedRequest(ret);
                    return(ret);
                }
        }
Example #3
0
        public async System.Threading.Tasks.Task <SoftmakeAll.SDK.Communication.REST.File> DownloadFileAsync()
        {
            if (System.String.IsNullOrWhiteSpace(this.URL))
            {
                throw new System.Exception(SoftmakeAll.SDK.Communication.REST.EmptyURLErrorMessage);
            }

            this.HasRequestErrors = false;

            this.Method = SoftmakeAll.SDK.Communication.REST.DefaultMethod;

            SoftmakeAll.SDK.Communication.REST.File Result = null;
            try
            {
                System.Net.HttpWebRequest HttpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(this.URL);
                HttpWebRequest.Method = this.Method;

                if (this.Headers.Count > 0)
                {
                    foreach (System.Collections.Generic.KeyValuePair <System.String, System.String> Header in this.Headers)
                    {
                        HttpWebRequest.Headers.Add(Header.Key, Header.Value);
                    }
                }
                this.AddAuthorizationHeader(HttpWebRequest);

                if (this.Body.ValueKind == System.Text.Json.JsonValueKind.Undefined)
                {
                    HttpWebRequest.ContentLength = 0;
                }
                else
                {
                    System.Byte[] BodyBytes = new System.Text.UTF8Encoding().GetBytes(Body.ToString());
                    HttpWebRequest.ContentType   = SoftmakeAll.SDK.Communication.REST.DefaultContentType;
                    HttpWebRequest.ContentLength = BodyBytes.Length;
                    System.IO.Stream RequestStream = await HttpWebRequest.GetRequestStreamAsync();

                    await RequestStream.WriteAsync(BodyBytes, 0, BodyBytes.Length);
                }

                if (this.Timeout > 0)
                {
                    HttpWebRequest.Timeout = this.Timeout;
                }

                using (System.Net.HttpWebResponse HttpWebResponse = (System.Net.HttpWebResponse) await HttpWebRequest.GetResponseAsync())
                {
                    this._StatusCode = HttpWebResponse.StatusCode;

                    System.Net.WebClient WebClient = new System.Net.WebClient();
                    foreach (System.Collections.Generic.KeyValuePair <System.String, System.String> Header in this.Headers)
                    {
                        WebClient.Headers.Add(Header.Key, Header.Value);
                    }
                    this.AddAuthorizationHeader(WebClient);
                    System.Byte[] FileContents = WebClient.DownloadData(this.URL);

                    if ((FileContents == null) || (FileContents.Length == 0))
                    {
                        return(null);
                    }

                    System.String FileName = "";

                    try
                    {
                        const System.String Key = "filename=";
                        FileName = HttpWebResponse.Headers.Get("Content-Disposition");
                        FileName = FileName.Substring(FileName.IndexOf(Key) + Key.Length);
                        FileName = FileName.Substring(0, FileName.IndexOf(';'));
                    }
                    catch
                    {
                        FileName = "";
                    }

                    return(new SoftmakeAll.SDK.Communication.REST.File()
                    {
                        Name = FileName, Contents = FileContents
                    });
                }
            }
            catch (System.Net.WebException ex)
            {
                this.HasRequestErrors = true;
                System.Net.HttpWebResponse HttpWebResponse = ex.Response as System.Net.HttpWebResponse;
                if (HttpWebResponse == null)
                {
                    this._StatusCode = System.Net.HttpStatusCode.InternalServerError;
                    Result           = null;
                    return(Result);
                }
                this._StatusCode = HttpWebResponse.StatusCode;
                Result           = null;
            }
            catch
            {
                this.HasRequestErrors = true;
                this._StatusCode      = System.Net.HttpStatusCode.InternalServerError;
                Result = null;
            }

            return(Result);
        }
Example #4
0
        public async System.Threading.Tasks.Task <System.Text.Json.JsonElement> SendAsync()
        {
            if (System.String.IsNullOrWhiteSpace(this.URL))
            {
                throw new System.Exception(SoftmakeAll.SDK.Communication.REST.EmptyURLErrorMessage);
            }

            this.HasRequestErrors = false;

            if (System.String.IsNullOrWhiteSpace(this.Method))
            {
                this.Method = SoftmakeAll.SDK.Communication.REST.DefaultMethod;
            }

            System.Text.Json.JsonElement Result = new System.Text.Json.JsonElement();
            try
            {
                System.Net.HttpWebRequest HttpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(this.URL);
                HttpWebRequest.Method = this.Method;

                if (this.Headers.Count > 0)
                {
                    foreach (System.Collections.Generic.KeyValuePair <System.String, System.String> Header in this.Headers)
                    {
                        if (Header.Key != "Content-Type")
                        {
                            HttpWebRequest.Headers.Add(Header.Key, Header.Value);
                        }
                        else
                        {
                            HttpWebRequest.ContentType = Header.Value;
                        }
                    }
                }
                this.AddAuthorizationHeader(HttpWebRequest);

                if (this.Body.ValueKind == System.Text.Json.JsonValueKind.Undefined)
                {
                    HttpWebRequest.ContentLength = 0;
                }
                else
                {
                    System.Byte[] BodyBytes = new System.Text.UTF8Encoding().GetBytes(Body.ToString());
                    HttpWebRequest.ContentType   = SoftmakeAll.SDK.Communication.REST.DefaultContentType;
                    HttpWebRequest.ContentLength = BodyBytes.Length;
                    await(await HttpWebRequest.GetRequestStreamAsync()).WriteAsync(BodyBytes, 0, BodyBytes.Length);
                }

                if (this.Timeout > 0)
                {
                    HttpWebRequest.Timeout = this.Timeout;
                }

                using (System.Net.HttpWebResponse HttpWebResponse = (System.Net.HttpWebResponse) await HttpWebRequest.GetResponseAsync())
                {
                    this._StatusCode = HttpWebResponse.StatusCode;
                    Result           = await this.ReadResponseStreamAsync(HttpWebResponse);
                }
            }
            catch (System.Net.WebException ex)
            {
                this.HasRequestErrors = true;
                System.Net.HttpWebResponse HttpWebResponse = ex.Response as System.Net.HttpWebResponse;
                if (HttpWebResponse == null)
                {
                    this._StatusCode = System.Net.HttpStatusCode.InternalServerError;
                    Result           = new { Error = true, Message = ex.Message }.ToJsonElement();
                    return(Result);
                }
                this._StatusCode = HttpWebResponse.StatusCode;
                Result           = await this.ReadResponseStreamAsync(HttpWebResponse);
            }
            catch (System.Exception ex)
            {
                this.HasRequestErrors = true;
                this._StatusCode      = System.Net.HttpStatusCode.InternalServerError;
                Result = new { Error = true, Message = ex.Message }.ToJsonElement();
            }

            return(Result);
        }
Example #5
0
        public void RequestWork(object state)
        {
            try
            {
#if NETFX_CORE
                System.Net.HttpWebRequest req = System.Net.HttpWebRequest.CreateHttp(new Uri(_Url));
#else
#if (UNITY_5 || UNITY_5_3_OR_NEWER)
                System.Net.HttpWebRequest req = System.Net.HttpWebRequest.Create(new Uri(_Url)) as System.Net.HttpWebRequest;
#else
                System.Net.HttpWebRequest req = new System.Net.HttpWebRequest(new Uri(_Url));
#endif
                req.KeepAlive = false;
#endif

                try
                {
                    lock (_CloseLock)
                    {
                        if (_Closed)
                        {
#if !HTTP_REQ_DONOT_ABORT
                            req.Abort();
#endif
                            if (_Status != RequestStatus.Finished)
                            {
                                _Error  = "Request Error (Cancelled)";
                                _Status = RequestStatus.Finished;
                            }
                            return;
                        }
                        _InnerReq = req;
                    }

#if !NETFX_CORE
                    req.Timeout          = int.MaxValue;
                    req.ReadWriteTimeout = int.MaxValue;
#if !HTTP_REQ_DONOT_ABORT
                    if (_Timeout > 0)
                    {
                        req.Timeout          = _Timeout;
                        req.ReadWriteTimeout = _Timeout;
                    }
#endif
#endif
                    if (_Headers != null)
                    {
                        foreach (var kvp in _Headers.Data)
                        {
                            var key = kvp.Key;
                            var val = (kvp.Value ?? "").ToString();
                            if (key.IndexOfAny(new[] { '\r', '\n', ':', }) >= 0)
                            {
                                continue; // it is dangerous, may be attacking.
                            }
                            if (val.IndexOfAny(new[] { '\r', '\n', }) >= 0)
                            {
                                continue; // it is dangerous, may be attacking.
                            }
                            else
                            {
                                req.Headers[key] = val;
                            }
                        }
                    }
                    if (_RangeEnabled)
                    {
                        long filepos = 0;
                        if (_Dest != null)
                        {
                            using (var stream = PlatExt.PlatDependant.OpenRead(_Dest))
                            {
                                if (stream != null)
                                {
                                    try
                                    {
                                        filepos = stream.Length;
                                    }
                                    catch (Exception e)
                                    {
                                        if (GLog.IsLogErrorEnabled)
                                        {
                                            GLog.LogException(e);
                                        }
                                    }
                                }
                            }
                        }
                        if (filepos <= 0)
                        {
                            if (_DestStream != null)
                            {
                                try
                                {
                                    if (_DestStream.CanSeek)
                                    {
                                        filepos = _DestStream.Length;
                                    }
                                }
                                catch (Exception e)
                                {
                                    if (GLog.IsLogErrorEnabled)
                                    {
                                        GLog.LogException(e);
                                    }
                                }
                            }
                        }
                        if (filepos > 0)
                        {
                            if (filepos > int.MaxValue)
                            {
                                _RangeEnabled = false;
                            }
                            else
                            {
                                req.AddRange((int)filepos);
                            }
                        }
                        else
                        {
                            _RangeEnabled = false;
                        }
                    }
                    if (_Data != null && (_Data.Count > 0 || _Data.Encoded != null))
                    {
                        req.Method = "POST";
                        var data = _Data.Encoded;
                        if (data == null)
                        {
                            req.ContentType = _Data.ContentType;
                            data            = _Data.Encode();
                        }
                        else
                        {
                            req.ContentType = "application/octet-stream";
                        }

#if NETFX_CORE
                        var tstream = req.GetRequestStreamAsync();
                        if (_Timeout > 0)
                        {
                            if (!tstream.Wait(_Timeout))
                            {
                                throw new TimeoutException();
                            }
                        }
                        else
                        {
                            tstream.Wait();
                        }
                        var stream = tstream.Result;
#else
                        req.ContentLength = data.Length;
                        var stream = req.GetRequestStream();
#endif

                        lock (_CloseLock)
                        {
                            if (_Closed)
                            {
#if !HTTP_REQ_DONOT_ABORT
                                req.Abort();
#endif
                                if (_Status != RequestStatus.Finished)
                                {
                                    _Error  = "Request Error (Cancelled)";
                                    _Status = RequestStatus.Finished;
                                }
                                return;
                            }
                        }
                        if (stream != null)
                        {
#if NETFX_CORE
                            if (_Timeout > 0)
                            {
                                stream.WriteTimeout = _Timeout;
                            }
                            else
                            {
                                stream.WriteTimeout = int.MaxValue;
                            }
#endif

                            try
                            {
                                stream.Write(data, 0, data.Length);
                                stream.Flush();
                            }
                            finally
                            {
                                stream.Dispose();
                            }
                        }
                    }
                    else
                    {
                    }
                    lock (_CloseLock)
                    {
                        if (_Closed)
                        {
#if !HTTP_REQ_DONOT_ABORT
                            req.Abort();
#endif
                            if (_Status != RequestStatus.Finished)
                            {
                                _Error  = "Request Error (Cancelled)";
                                _Status = RequestStatus.Finished;
                            }
                            return;
                        }
                    }
#if NETFX_CORE
                    var tresp = req.GetResponseAsync();
                    if (_Timeout > 0)
                    {
                        if (!tresp.Wait(_Timeout))
                        {
                            throw new TimeoutException();
                        }
                    }
                    else
                    {
                        tresp.Wait();
                    }
                    var resp = tresp.Result;
#else
                    var resp = req.GetResponse();
#endif
                    lock (_CloseLock)
                    {
                        if (_Closed)
                        {
#if !HTTP_REQ_DONOT_ABORT
                            req.Abort();
#endif
                            if (_Status != RequestStatus.Finished)
                            {
                                _Error  = "Request Error (Cancelled)";
                                _Status = RequestStatus.Finished;
                            }
                            return;
                        }
                    }
                    if (resp != null)
                    {
                        try
                        {
                            _Total = (ulong)resp.ContentLength;
                        }
                        catch
                        {
                        }
                        try
                        {
                            _RespHeaders = new HttpRequestData();
                            foreach (var key in resp.Headers.AllKeys)
                            {
                                _RespHeaders.Add(key, resp.Headers[key]);
                            }

                            if (_RangeEnabled)
                            {
                                bool rangeRespFound = false;
                                foreach (var key in resp.Headers.AllKeys)
                                {
                                    if (key.ToLower() == "content-range")
                                    {
                                        rangeRespFound = true;
                                    }
                                }
                                if (!rangeRespFound)
                                {
                                    _RangeEnabled = false;
                                }
                            }

                            var stream = resp.GetResponseStream();
                            lock (_CloseLock)
                            {
                                if (_Closed)
                                {
#if !HTTP_REQ_DONOT_ABORT
                                    req.Abort();
#endif
                                    if (_Status != RequestStatus.Finished)
                                    {
                                        _Error  = "Request Error (Cancelled)";
                                        _Status = RequestStatus.Finished;
                                    }
                                    return;
                                }
                            }
                            if (stream != null)
                            {
#if NETFX_CORE
                                if (_Timeout > 0)
                                {
                                    stream.ReadTimeout = _Timeout;
                                }
                                else
                                {
                                    stream.ReadTimeout = int.MaxValue;
                                }
#endif
                                Stream streamd = null;
                                try
                                {
                                    byte[] buffer   = new byte[1024 * 1024];
                                    ulong  totalcnt = 0;
                                    int    readcnt  = 0;

                                    bool mem = false;
                                    if (_Dest != null)
                                    {
                                        if (_RangeEnabled)
                                        {
                                            streamd  = Capstones.PlatExt.PlatDependant.OpenAppend(_Dest);
                                            totalcnt = (ulong)streamd.Length;
                                        }
                                        else
                                        {
                                            streamd = Capstones.PlatExt.PlatDependant.OpenWrite(_Dest);
                                        }
#if HTTP_REQ_DONOT_ABORT
                                        if (streamd != null)
                                        {
                                            _CloseList.Add(streamd);
                                        }
#endif
                                    }
                                    if (streamd == null)
                                    {
                                        if (_DestStream != null)
                                        {
                                            if (_RangeEnabled)
                                            {
                                                _DestStream.Seek(0, SeekOrigin.End);
                                                totalcnt = (ulong)_DestStream.Length;
                                            }
                                            streamd = _DestStream;
                                        }
                                        else
                                        {
                                            mem     = true;
                                            streamd = new MemoryStream();
#if HTTP_REQ_DONOT_ABORT
                                            _CloseList.Add(streamd);
#endif
                                        }
                                    }

                                    if (_Total > 0)
                                    {
                                        _Total += totalcnt;
                                    }

                                    do
                                    {
                                        lock (_CloseLock)
                                        {
                                            if (_Closed)
                                            {
#if !HTTP_REQ_DONOT_ABORT
                                                req.Abort();
#endif
                                                if (_Status != RequestStatus.Finished)
                                                {
                                                    _Error  = "Request Error (Cancelled)";
                                                    _Status = RequestStatus.Finished;
                                                }
                                                return;
                                            }
                                        }
                                        try
                                        {
                                            readcnt = 0;
                                            readcnt = stream.Read(buffer, 0, 1024 * 1024);
                                            if (readcnt <= 0)
                                            {
                                                stream.ReadByte(); // when it is closed, we need read to raise exception.
                                                break;
                                            }

                                            streamd.Write(buffer, 0, readcnt);
                                            streamd.Flush();
                                        }
                                        catch (TimeoutException te)
                                        {
                                            if (GLog.IsLogErrorEnabled)
                                            {
                                                GLog.LogException(te);
                                            }
                                            _Error = "timedout";
                                        }
                                        catch (System.Net.WebException we)
                                        {
                                            if (GLog.IsLogErrorEnabled)
                                            {
                                                GLog.LogException(we);
                                            }
#if NETFX_CORE
                                            if (we.Status.ToString() == "Timeout")
#else
                                            if (we.Status == System.Net.WebExceptionStatus.Timeout)
#endif
                                            {
                                                _Error = "timedout";
                                            }
                                            else
                                            {
                                                _Error = "Request Error (Exception):\n" + we.ToString();
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            if (GLog.IsLogErrorEnabled)
                                            {
                                                GLog.LogException(e);
                                            }
                                            _Error = "Request Error (Exception):\n" + e.ToString();
                                        }
                                        lock (_CloseLock)
                                        {
                                            if (_Closed)
                                            {
#if !HTTP_REQ_DONOT_ABORT
                                                req.Abort();
#endif
                                                if (_Status != RequestStatus.Finished)
                                                {
                                                    _Error  = "Request Error (Cancelled)";
                                                    _Status = RequestStatus.Finished;
                                                }
                                                return;
                                            }
                                        }
                                        totalcnt += (ulong)readcnt;
                                        _Length   = totalcnt;
                                        //Capstones.PlatExt.PlatDependant.LogInfo(readcnt);
                                    } while (readcnt > 0);

                                    if (mem)
                                    {
                                        _Resp = ((MemoryStream)streamd).ToArray();
                                    }
                                }
                                finally
                                {
                                    stream.Dispose();
                                    if (streamd != null)
                                    {
                                        if (streamd != _DestStream)
                                        {
                                            streamd.Dispose();
                                        }
                                    }
                                }
                            }
                        }
                        finally
                        {
#if NETFX_CORE
                            resp.Dispose();
#else
                            resp.Close();
#endif
                        }
                    }
                }
                catch (TimeoutException te)
                {
                    if (GLog.IsLogErrorEnabled)
                    {
                        GLog.LogException(te);
                    }
                    _Error = "timedout";
                }
                catch (System.Net.WebException we)
                {
                    if (GLog.IsLogErrorEnabled)
                    {
                        GLog.LogException(we);
                    }
#if NETFX_CORE
                    if (we.Status.ToString() == "Timeout")
#else
                    if (we.Status == System.Net.WebExceptionStatus.Timeout)
#endif
                    {
                        _Error = "timedout";
                    }
                    else
                    {
                        if (we.Response is System.Net.HttpWebResponse && ((System.Net.HttpWebResponse)we.Response).StatusCode == System.Net.HttpStatusCode.RequestedRangeNotSatisfiable)
                        {
                        }
                        else
                        {
                            _Error = "Request Error (Exception):\n" + we.ToString();
                        }
                    }
                }
                catch (Exception e)
                {
                    if (GLog.IsLogErrorEnabled)
                    {
                        GLog.LogException(e);
                    }
                    _Error = "Request Error (Exception):\n" + e.ToString();
                }
                finally
                {
                    if (_Error == null)
                    {
                        lock (_CloseLock)
                        {
                            _Closed   = true;
                            _InnerReq = null;
                        }
                    }
                    else
                    {
                        StopRequest();
                    }
                }
            }
            catch (TimeoutException te)
            {
                if (GLog.IsLogErrorEnabled)
                {
                    GLog.LogException(te);
                }
                _Error = "timedout";
            }
            catch (System.Net.WebException we)
            {
                if (GLog.IsLogErrorEnabled)
                {
                    GLog.LogException(we);
                }
#if NETFX_CORE
                if (we.Status.ToString() == "Timeout")
#else
                if (we.Status == System.Net.WebExceptionStatus.Timeout)
#endif
                {
                    _Error = "timedout";
                }
                else
                {
                    _Error = "Request Error (Exception):\n" + we.ToString();
                }
            }
            catch (Exception e)
            {
                if (GLog.IsLogErrorEnabled)
                {
                    GLog.LogException(e);
                }
                _Error = "Request Error (Exception):\n" + e.ToString();
            }
            finally
            {
                lock (_CloseLock)
                {
                    _Status = RequestStatus.Finished;
                    if (_OnDone != null)
                    {
                        var ondone = _OnDone;
                        _OnDone = null;
                        ondone();
                    }
                }
            }
        }
        protected override void OnProcessingRequest(System.Net.HttpWebRequest wr)
        {
            if (wr == null)
            {
                throw new ArgumentNullException("wr");
            }

            if (InputStream != null)
            {
                string boundary      = "---------------------------" + DateTime.Now.Ticks.ToString("x", System.Globalization.CultureInfo.InvariantCulture);
                byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

                wr.ContentType = "multipart/form-data; boundary=" + boundary;
                wr.Method      = "POST";

                using (var ms = new Tools.ChunkedMemoryStream())
                {
                    ms.Write(boundarybytes, 0, boundarybytes.Length);
                    const string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n";
                    string       header         = string.Format(System.Globalization.CultureInfo.InvariantCulture, headerTemplate, "torrent_file", "file.torrent", "application/octet-stream");
                    byte[]       headerbytes    = Encoding.UTF8.GetBytes(header);
                    ms.Write(headerbytes, 0, headerbytes.Length);

                    byte[] contenttypebytes = Encoding.ASCII.GetBytes("Content-Type: application/x-bittorrent\r\n\r\n");
                    ms.Write(contenttypebytes, 0, contenttypebytes.Length);

                    byte[] buffer = new byte[4096];
                    int    bytesRead;
                    while ((bytesRead = InputStream.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        ms.Write(buffer, 0, bytesRead);
                    }
                    //request.InputStream.Close();

                    byte[] trailer = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
                    ms.Write(trailer, 0, trailer.Length);

#if !PORTABLE
                    wr.ContentLength = ms.Length;
#else
                    wr.Headers["Content-Length"] = ms.Length.ToString(CultureInfo.InvariantCulture);
#endif

                    // Debug
                    //ms.Position = 0;
                    //var srMs = new System.IO.StreamReader(ms);
                    //string post = srMs.ReadToEnd();



#if !PORTABLE
                    System.IO.Stream rs = wr.GetRequestStream();
#else
                    System.IO.Stream rs = wr.GetRequestStreamAsync().Result;
#endif
                    if (rs != null)
                    {
                        using (rs)
                        {
                            ms.Position = 0;
                            while ((bytesRead = ms.Read(buffer, 0, buffer.Length)) != 0)
                            {
                                rs.Write(buffer, 0, bytesRead);
                            }

                            rs.Flush();
                        }
                    }
                }
            }
        }
Example #7
0
        private static async System.Threading.Tasks.Task SystemNetAsync(ServiceResponse response, string method, Uri uri, List <Tuple <string, string> > headers, System.Net.Http.HttpContent requestContent, bool binaryResponse, int maxResponseLength)
        {
            long t = 0;

            response.stopWatch.Start();
            try
            {
                System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(uri);

                request.Method = method;


                if (requestContent != null)
                {
                    if (requestContent.GetType() == typeof(System.Net.Http.MultipartFormDataContent))
                    {
                        request.ContentType = requestContent.Headers.ContentType.ToString();
                    }
                    using (System.IO.Stream requestStream = await request.GetRequestStreamAsync())
                    {
#if false
                        System.IO.Stream s = await requestContent.ReadAsStreamAsync();

                        long?l = requestContent.Headers.ContentLength;
                        if (l > int.MaxValue)
                        {
                            throw new ArgumentOutOfRangeException();
                        }
                        int    i      = (int)l;
                        byte[] buffer = new byte[i];
                        int    len    = await s.ReadAsync(buffer, 0, i);

                        //System.IO.StreamReader sr = new System.IO.StreamReader(requestContent.ReadAsStreamAsync());
                        //await requestContent.CopyToAsync(sr);
#endif
                        await requestContent.CopyToAsync(requestStream);
                    }
                }

                if (headers != null)
                {
                    foreach (Tuple <string, string> h in headers)
                    {
                        switch (h.Item1)
                        {
                        case "Accept":     // must use explicity Accept property otherwise an error is thrown
                            request.Accept = h.Item2;
                            break;

                        case "Content-Type":     // must use explicity ContentType property otherwise an error is thrown
                            request.ContentType = h.Item2;
                            break;

                        case "User-Agent":     // must use explicity ContentType property otherwise an error is thrown
                            request.UserAgent = h.Item2;
                            break;

                        default:
                            request.Headers[h.Item1] = h.Item2;
                            break;
                        }
                    }
                }

                t = response.stopWatch.ElapsedMilliseconds;
                using (System.Net.WebResponse wr = await request.GetResponseAsync())
                {
                    response.RequestElapsedMilliseconds = response.stopWatch.ElapsedMilliseconds - t;
                    Log.WriteLine("Request Elapsed milliseconds:" + response.RequestElapsedMilliseconds);
                    using (System.Net.HttpWebResponse hwr = (System.Net.HttpWebResponse)wr)
                    {
                        response.StatusCode = (int)hwr.StatusCode;
                        Log.WriteLine("Request StatusCode:" + response.StatusCode);
                        if (hwr.StatusCode == System.Net.HttpStatusCode.OK)
                        {
                            response.ResponseJson = null;
                            using (System.IO.Stream rr = wr.GetResponseStream())
                            {
                                if (binaryResponse)
                                {
                                    await PostSystemNetAsyncBinaryReader(rr, maxResponseLength, response);
                                }
                                else
                                {
                                    await PostSystemNetAsyncStreamReader(rr, response);
                                }
                            }
                        }
                        else
                        {
                            response.ResponseResult = hwr.StatusDescription;
                            Log.WriteLine("PostAsync Failed: StatusCode:" + hwr.StatusDescription + "(" + response.StatusCode.ToString() + ")");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteLine("Exception:" + ex.Message);
                if (ex.InnerException != null)
                {
                    Log.WriteLine("InnerException:" + ex.InnerException);
                }
            }
            response.FileName = "curl-" + httpCallCount;
            response.stopWatch.Stop();
            response.TotalElapsedMilliseconds = response.stopWatch.ElapsedMilliseconds;
            Log.WriteLine("Total Elapsed milliseconds:" + response.TotalElapsedMilliseconds);
        }