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

Send() public method

Send HTTP request for the full content from the specified server.
public Send ( System.Version httpVersion, string>.Dictionary httpHeader, byte httpBodyData, HttpMethod httpMethod, int timeOut ) : void
httpVersion System.Version The HTTP version.
httpHeader string>.Dictionary The HTTP header.
httpBodyData byte The HTTP body data.
httpMethod HttpMethod The HTTP method.
timeOut int The number of milliseconds to wait before the request times out.
return void
        public void Send(
            Version httpVersion,
            Dictionary<string, string> httpHeader,
            byte[] httpBodyData,
            HttpMethod httpMethod,
            int timeOut)
        {
            this.httpWebRequest = (HttpWebRequest)WebRequest.Create(this.httpUri);
            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}, tshe 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;
                }
            }

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

Same methods

HttpClientTransport::Send ( System.Version httpVersion, string>.Dictionary httpHeader, byte httpBodyData, HttpMethod httpMethod, int timeOut, int rangeFrom, int rangeTo ) : 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;
        }