示例#1
0
        private HttpWebRequest CreateRequest(string restRelativeUri, RequestType requestType, RequestConfiguration additionalRequestConfiguration)
        {
            string         url     = Host + restRelativeUri;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            if (NetworkSettings.CustomProxy != null)
            {
                request.Proxy = NetworkSettings.CustomProxy;
            }

            //Add authentication cookies/headers
            authenticationStrategy.PrepareRequest(request);


            //add internal API token
            request.Headers.Add("HPECLIENTTYPE", "HPE_CI_CLIENT");


            //set content type/accept/method
            switch (requestType)
            {
            case RequestType.Get:
                request.Accept = CONTENT_TYPE_JSON;
                request.Method = METHOD_GET;
                break;

            case RequestType.GetOctet:
                request.Accept = CONTENT_TYPE_STREAM;
                request.Method = METHOD_GET;
                break;

            case RequestType.Post:
                request.ContentType = CONTENT_TYPE_JSON;
                request.Accept      = CONTENT_TYPE_JSON;
                request.Method      = METHOD_POST;
                break;

            case RequestType.PostOctet:
                request.ContentType = CONTENT_TYPE_STREAM;
                request.Method      = METHOD_POST;
                break;

            case RequestType.Delete:
                request.Accept = CONTENT_TYPE_JSON;
                request.Method = METHOD_DELETE;
                break;

            case RequestType.Update:
                request.ContentType = CONTENT_TYPE_JSON;
                request.Accept      = CONTENT_TYPE_JSON;
                request.Method      = METHOD_PUT;
                break;

            case RequestType.MultiPart:
                request.ContentType = CONTENT_TYPE_MULTIPART;
                request.Accept      = CONTENT_TYPE_JSON;
                request.Method      = METHOD_POST;
                break;

            default:
                break;
            }

            if (additionalRequestConfiguration != null)
            {
                if (additionalRequestConfiguration.Timeout.HasValue)
                {
                    request.Timeout = additionalRequestConfiguration.Timeout.Value;
                }
                if (additionalRequestConfiguration.Headers != null)
                {
                    foreach (KeyValuePair <string, string> header2value in additionalRequestConfiguration.Headers)
                    {
                        switch (header2value.Key.ToLower())
                        {
                        case "contenttype":
                            request.ContentType = header2value.Value;
                            break;

                        case "accept":
                            request.Accept = header2value.Value;
                            break;

                        default:
                            request.Headers.Add(header2value.Key, header2value.Value);
                            break;
                        }
                    }
                }
                if (additionalRequestConfiguration.GZipCompression)
                {
                    request.Headers.Add("Content-Encoding", "gzip");
                }
            }

            return(request);
        }