Beispiel #1
0
        /// <summary>
        /// post a data to server
        /// </summary>
        /// <param name="url"></param>
        /// <param name="parameterInfoes"></param>
        /// <param name="streamInfo"></param>
        /// <returns></returns>
        public HttpClientResponse Post(string url, ParameterInfo[] parameterInfoes, BaseStreamInfo streamInfo = null)
        {
#if (NETSTANDARD1_6)
            throw new NotSupportedException();
#else
            HttpClientResponseBase response = PostHead(url, parameterInfoes, streamInfo);
            try
            {
                HttpClientResponse httpClientResponse = new HttpClientResponse
                {
                    Status          = response.Status,
                    ResponseHeaders = response.ResponseHeaders,
                    Stream          = response.Stream,
                    TcpClient       = response.TcpClient,
                };
                int    length    = int.Parse(httpClientResponse.ResponseHeaders["content-length"]);
                byte[] result    = new byte[length];
                int    readCount = 0;
                while (readCount < length)
                {
                    byte[] bytes       = new byte[512];
                    int    readedCount = 0;
                    readedCount = response.Stream.Read(bytes, bytes.Length);

                    for (int i = 0; i < readedCount; i++)
                    {
                        result[i + readCount] = bytes[i];
                    }
                    readCount += readedCount;
                }
                httpClientResponse.Data = Encoding.GetString(result);
                response = httpClientResponse;
                return(httpClientResponse);
            }
            finally
            {
                response.TcpClient.Close();
            }
#endif
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="url"></param>
        /// <param name="parameterInfoes"></param>
        /// <returns></returns>
        public async Task <HttpClientResponse> PostAsync(string url, ParameterInfo[] parameterInfoes, BaseStreamInfo streamInfo = null)
        {
            HttpClientResponseBase response = await PostHeadAsync(url, parameterInfoes, streamInfo);

            try
            {
                HttpClientResponse httpClientResponse = new HttpClientResponse
                {
                    Status          = response.Status,
                    ResponseHeaders = response.ResponseHeaders,
                    Stream          = response.Stream,
                    TcpClient       = response.TcpClient,
                };
                int    length    = int.Parse(httpClientResponse.ResponseHeaders["content-length"]);
                byte[] result    = new byte[length];
                int    readCount = 0;
                while (readCount < length)
                {
                    byte[] bytes       = new byte[512];
                    int    readedCount = 0;
                    readedCount = await response.Stream.ReadAsync(bytes, bytes.Length);

                    for (int i = 0; i < readedCount; i++)
                    {
                        result[i + readCount] = bytes[i];
                    }
                    readCount += readedCount;
                }
                httpClientResponse.Data = Encoding.GetString(result);
                response = httpClientResponse;
                return(httpClientResponse);
            }
            finally
            {
                response.TcpClient.Close();
            }
        }