示例#1
0
        public HttpWebRequest CreateHttpWebRequest(object[] args)
        {
            var url = Url;

            if (IsWithPath && PathKeys.Count > 0)
            {
                var strs = new string[PathKeys.Count];
                foreach (var item in PathKeys)
                {
                    strs[item.Key] = args[item.Value.ArgIndex].ToString();
                }
                url = String.Format(url, strs);
            }

            var querys = new List <string>();

            foreach (var item in QueryKeys)
            {
                querys.Add(item.GetRequestString(args));
            }
            if (querys.Count > 0)
            {
                url += (url.IndexOf('?') == -1 ? "?" : "&") + String.Join("&", querys);
            }

            var request = (HttpWebRequest)WebRequest.Create(url);

            if (Timeout > 0)
            {
                request.Timeout = Timeout;
            }
            request.CookieContainer = CookieContainer;
            request.Method          = Method.ToString();
            if (FiexdHeaders.ContainsKey(Headers.Accept))
            {
                request.Accept = FiexdHeaders[Headers.Accept];
            }
            if (FiexdHeaders.ContainsKey(Headers.ContentType))
            {
                request.ContentType = FiexdHeaders[Headers.ContentType];
            }
            if (FiexdHeaders.ContainsKey(Headers.CacheControl))
            {
                request.Headers[Headers.CacheControl] = FiexdHeaders[Headers.CacheControl];
            }
            if (FiexdHeaders.ContainsKey(Headers.AcceptLanguage))
            {
                request.Headers[Headers.AcceptLanguage] = FiexdHeaders[Headers.AcceptLanguage];
            }
            if (FiexdHeaders.ContainsKey(Headers.AcceptEncoding))
            {
                request.Headers[Headers.AcceptEncoding] = FiexdHeaders[Headers.AcceptEncoding];
            }
            if (Host != null)
            {
                var uri = new Uri(Host);
                request.Host = uri.Host;
            }

            foreach (var item in HeaderKeys)
            {
                request.Headers[item.Key] = args[item.ArgIndex].ToString();
            }

            var fields = new List <string>();

            foreach (var item in FieldKeys)
            {
                fields.Add(item.GetRequestString(args));
            }
            if (BodyKey != null)
            {
                fields.Add(BodyKey.GetRequestString(args));
            }

            if (MethodKind.GET != Method)
            {
                var fieldContent = fields.Count == 0 ? String.Empty : String.Join("&", fields);
                if (Method == MethodKind.PUT || Method == MethodKind.DELETE)
                {
                    var contentLength = 0;
                    if (!String.IsNullOrEmpty(fieldContent) && FiexdHeaders[Headers.ContentType] != MultipartAttribute.MultipartContentType)
                    {
                        contentLength = fieldContent.Length;
                    }
                    request.ContentLength = contentLength;
                }
                if (FiexdHeaders[Headers.ContentType] != MultipartAttribute.MultipartContentType)
                {
                    var    dataWriter = request.GetRequestStream();
                    byte[] d          = Encoding.GetBytes(fieldContent);
                    dataWriter.Write(d, 0, d.Length);
                    dataWriter.Flush();
                }
                else
                {
                    var boundary = Headers.CreateBoundary();
                    request.ContentType += ";boundary=" + boundary;
                    request.KeepAlive    = true;
                    var dataWriter = request.GetRequestStream();
                    PartKeys.CopyTo(dataWriter, args, boundary);
                    dataWriter.Flush();
                }
            }
            else
            {
                request.ContentLength = 0;
            }

            return(request);
        }