Microsoft.Protocols.TestTools.StackSdk.CommonStack.HttpClientTransport.Send C# (CSharp) Method

Send() public method

Send HTTP request for the partial content from the specified server.
public Send ( System.Version httpVersion, string>.Dictionary httpHeader, byte httpBodyData, HttpMethod httpMethod, int timeOut, int rangeFrom, int rangeTo ) : void
httpVersion System.Version The HTTP version.
httpHeader string>.Dictionary The HTTP header.
httpBodyData byte The HTTP payload.
httpMethod HttpMethod The HTTP method.
timeOut int The number of milliseconds to wait before the request times out.
rangeFrom int The start position in bytes at which to the requested data.
rangeTo int The end position in bytes at which to the requested data.
return void
        public void Send(
            Version httpVersion,
            Dictionary<string, string> httpHeader,
            byte[] httpBodyData,
            HttpMethod httpMethod,
            int timeOut,
            int rangeFrom,
            int rangeTo)
        {
            this.httpWebRequest = (HttpWebRequest)WebRequest.Create(this.httpUri);
            this.httpWebRequest.AddRange(rangeFrom, rangeTo);
            this.httpWebRequest.Timeout = timeOut;
            this.httpWebRequest.Method = httpMethod.ToString();

            if (userName != null)
            {
                this.httpWebRequest.Credentials = new NetworkCredential(userName, userPassword, domainName);
            }

            // The default version of HTTP to use for the request is HTTP 1.1.
            if (HttpVersion.Version10 == httpVersion)
            {
                this.httpWebRequest.ProtocolVersion = HttpVersion.Version10;
            }

            if (this.logger != null)
            {
                this.logger.AddDebug(string.Format(
                    @"The HTTP WEB request is created:
                    The requested HTTP URI is:{0}, the HTTP method used:{1}, the HTTP version used:{2}",
                    this.httpUri.ToString(),
                    httpMethod.ToString(),
                    this.httpWebRequest.ProtocolVersion.ToString()));
            }

            if (httpHeader != null)
            {
                foreach (string key in httpHeader.Keys)
                {
                    this.httpWebRequest.Headers.Add(key, httpHeader[key]);
                }
            }

            if (httpMethod != HttpMethod.GET && httpBodyData != null)
            {
                this.httpWebRequest.ContentLength = httpBodyData.Length;
                try
                {
                    this.httpBodyStream = this.httpWebRequest.GetRequestStream();
                    this.httpBodyStream.Write(httpBodyData, 0, httpBodyData.Length);
                    this.httpBodyStream.Close();
                }
                catch (ObjectDisposedException e)
                {
                    if (this.logger != null)
                    {
                        this.logger.AddDebug(
                            string.Format("Object disposed exception is catched, detailed information: {0}.", e.Message));
                    }

                    throw;
                }
                catch (WebException e)
                {
                    if (this.logger != null)
                    {
                        this.logger.AddDebug(
                            string.Format("Web exception is catched, detailed information: {0}.", e.Message));
                    }

                    throw;
                }
            }

            if (this.logger != null)
            {
                this.logger.AddInfo("The HTTP partial content request is created and sent successfully.");
            }
        }

Same methods

HttpClientTransport::Send ( System.Version httpVersion, string>.Dictionary httpHeader, byte httpBodyData, HttpMethod httpMethod, int timeOut ) : void

Usage Example

        protected bool SetupHttpsConnection()
        {
            sutControlAdapter.ClearCache(testConfig.HostedCacheServerComputerFQDNOrNetBiosName);

            int timeout = 20000;
            byte[] content = TestUtility.GenerateRandomArray(10);

            HttpClientTransport testClient = new HttpClientTransport(
                TransferProtocol.HTTPS,
                testConfig.HostedCacheServerComputerName,
                testConfig.HostedCacheServerHTTPSListenPort,
                PchcConsts.HttpsUrl,
                testConfig.DomainName,
                testConfig.UserName,
                testConfig.UserPassword);
            try
            {
                testClient.Send(HttpVersion.Version10, null, content, HttpMethod.POST, timeout);
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                testClient.Dispose();
            }

            return true;
        }