public HttpRequestMessage Build() { var httpMessage = new HttpRequestMessage(HttpMethod, UriBuilder.Uri); if (this.PathParams.Any()) { var path = HttpUtility.UrlDecode(UriBuilder.Path); foreach (var p in this.PathParams) { path = path.Replace("{" + p.Key + "}", p.Value, StringComparison.OrdinalIgnoreCase); } UriBuilder.Path = HttpUtility.UrlPathEncode(path); } if (this.QueryStrings.Any()) { UriBuilder.Query = string.Join("&", this.QueryStrings.Select(q => q.Key + "=" + HttpUtility.UrlEncode(q.Value))); } if (this.HttpMethod == HttpMethod.Post || this.HttpMethod.Method == "PATCH" || this.HttpMethod == HttpMethod.Put) { if (this.MultiPartAttribute != null) { var multipleContent = new MultipartContent(this.MultiPartAttribute.MultiPartType, Guid.NewGuid().ToString()); if (FormBodys.Any()) { var content = new FormUrlEncodedContent(this.FormBodys); multipleContent.Add(content); } if (JsonBody != null) { var content = new StringContent(JsonBody.ToString(), Utf8Encoding, "application/json"); multipleContent.Add(content); } if (RawContents.Any()) { foreach (var c in RawContents) { multipleContent.Add(c); } } //if (Files != null) //{ // foreach (var f in Files) // { // var content = new StreamContent(f.Value.OpenRead()); // content.Headers.ContentDisposition = new ContentDispositionHeaderValue(this.MultiPartAttribute.MultiPartType); // content.Headers.ContentDisposition.FileName = f.Value.Name; // content.Headers.ContentDisposition.Name = f.Key; // content.Headers.ContentType = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping(f.Value.Name)); // multipleContent.Add(content); // } //} //if (StreamBodys != null) //{ // foreach (var s in StreamBodys) // { // var content = new StreamContent(s.Item2); // content.Headers.ContentType = new MediaTypeHeaderValue(s.Item1); // multipleContent.Add(content); // } //} httpMessage.Content = multipleContent; } else { if (new[] { FormBodys.Any(), JsonBody != null, RawContents.Any() }.Count(i => i == true) > 1) { throw new NotSupportedException("Not support multiple kinds of http content in a message!"); } if (FormBodys.Any()) { httpMessage.Content = new FormUrlEncodedContent(this.FormBodys); if (httpMessage.Content.Headers != null && httpMessage.Content.Headers.ContentType != null) { httpMessage.Content.Headers.ContentType.CharSet = Utf8Encoding.HeaderName; } } else if (JsonBody != null) { httpMessage.Content = new StringContent(JsonBody.ToString(), Utf8Encoding, "application/json"); if (httpMessage.Content.Headers != null && httpMessage.Content.Headers.ContentType != null) { httpMessage.Content.Headers.ContentType.CharSet = Utf8Encoding.HeaderName; } } if (RawContents.Any()) { httpMessage.Content = RawContents.FirstOrDefault(); } //else if (StreamBodys.Any()) //{ // var stream = StreamBodys.FirstOrDefault(); // httpMessage.Content = new StreamContent(stream.Item2); // httpMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(stream.Item1); //} //else if (Files.Any()) //{ // var f = Files.FirstOrDefault(); // var content = new StreamContent(f.Value.OpenRead()); // content.Headers.ContentDisposition = new ContentDispositionHeaderValue(this.MultiPartAttribute.MultiPartType); // content.Headers.ContentDisposition.FileName = f.Value.Name; // content.Headers.ContentDisposition.Name = f.Key; // content.Headers.ContentType = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping(f.Value.Name)); // httpMessage.Content = content; //} } } httpMessage.RequestUri = this.UriBuilder.Uri; foreach (var h in this.Headers.GroupBy(h => h.Key)) { httpMessage.Headers.TryAddWithoutValidation(h.Key, h.Select(i => i.Value)); } foreach (var c in this.Cookies) { httpMessage.Headers.GetCookies().Add(c); } return(httpMessage); }
public HttpRequestMessage Build() { var httpMessage = new HttpRequestMessage(HttpMethod, UriBuilder.Uri); httpMessage.Version = HttpVersion; if (this.PathParams.Any()) { var path = HttpUtility.UrlDecode(UriBuilder.Path); foreach (var p in this.PathParams) { path = path.Replace("{" + p.Key + "}", p.Value, StringComparison.OrdinalIgnoreCase); } UriBuilder.Path = HttpUtility.UrlPathEncode(path); } if (this.QueryStrings.Any()) { UriBuilder.Query = string.Join("&", this.QueryStrings.Select(q => q.Key + "=" + HttpUtility.UrlEncode(q.Value))); } if (this.HttpMethod == HttpMethod.Post || this.HttpMethod.Method == "PATCH" || this.HttpMethod == HttpMethod.Put) { if (this.MultiPartAttribute != null) { var multipleContent = new MultipartContent(this.MultiPartAttribute.MultiPartType, Guid.NewGuid().ToString()); if (FormBodys.Any()) { var content = new FormUrlEncodedContent(this.FormBodys); multipleContent.Add(content); } if (JsonBody != null) { var stringWriter = new StringWriter(); this.JsonSerializer.Serialize(stringWriter, JsonBody); var content = new StringContent(stringWriter.ToString(), Utf8Encoding, "application/json"); multipleContent.Add(content); } if (RawContents.Any()) { foreach (var c in RawContents) { multipleContent.Add(c); } } httpMessage.Content = multipleContent; } else { if (new[] { FormBodys.Any(), JsonBody != null, RawContents.Any() }.Count(i => i == true) > 1) { throw new NotSupportedException("Not support multiple kinds of http content in a message!"); } if (FormBodys.Any()) { httpMessage.Content = new FormUrlEncodedContent(this.FormBodys); if (httpMessage.Content.Headers != null && httpMessage.Content.Headers.ContentType != null) { httpMessage.Content.Headers.ContentType.CharSet = Utf8Encoding.HeaderName; } } else if (JsonBody != null) { var stringWriter = new StringWriter(); this.JsonSerializer.Serialize(stringWriter, JsonBody); httpMessage.Content = new StringContent(stringWriter.ToString(), Utf8Encoding, "application/json"); if (httpMessage.Content.Headers != null && httpMessage.Content.Headers.ContentType != null) { httpMessage.Content.Headers.ContentType.CharSet = Utf8Encoding.HeaderName; } } if (RawContents.Any()) { httpMessage.Content = RawContents.FirstOrDefault(); } } } httpMessage.RequestUri = this.UriBuilder.Uri; foreach (var h in this.Headers.GroupBy(h => h.Key)) { httpMessage.Headers.TryAddWithoutValidation(h.Key, h.Select(i => i.Value)); } foreach (var c in this.Cookies) { httpMessage.Headers.GetCookies().Add(c); } return(httpMessage); }