@@ -500,7 +500,7 @@ public static string SendStringToUrl(this HttpClient client, string url, string
500
500
{
501
501
httpReq . Content = new StringContent ( requestBody , UseEncoding ) ;
502
502
if ( contentType != null )
503
- httpReq . Content . Headers . ContentType = new MediaTypeHeaderValue ( contentType ) ;
503
+ httpReq . Content . Headers . ContentType = MediaTypeHeaderValue . Parse ( contentType ) ;
504
504
}
505
505
requestFilter ? . Invoke ( httpReq ) ;
506
506
@@ -531,7 +531,7 @@ public static async Task<string> SendStringToUrlAsync(this HttpClient client, st
531
531
{
532
532
httpReq . Content = new StringContent ( requestBody , UseEncoding ) ;
533
533
if ( contentType != null )
534
- httpReq . Content . Headers . ContentType = new MediaTypeHeaderValue ( contentType ) ;
534
+ httpReq . Content . Headers . ContentType = MediaTypeHeaderValue . Parse ( contentType ) ;
535
535
}
536
536
requestFilter ? . Invoke ( httpReq ) ;
537
537
@@ -612,7 +612,7 @@ public static byte[] SendBytesToUrl(this HttpClient client, string url, string m
612
612
{
613
613
httpReq . Content = new ReadOnlyMemoryContent ( requestBody ) ;
614
614
if ( contentType != null )
615
- httpReq . Content . Headers . ContentType = new MediaTypeHeaderValue ( contentType ) ;
615
+ httpReq . Content . Headers . ContentType = MediaTypeHeaderValue . Parse ( contentType ) ;
616
616
}
617
617
requestFilter ? . Invoke ( httpReq ) ;
618
618
@@ -643,7 +643,7 @@ public static async Task<byte[]> SendBytesToUrlAsync(this HttpClient client, str
643
643
{
644
644
httpReq . Content = new ReadOnlyMemoryContent ( requestBody ) ;
645
645
if ( contentType != null )
646
- httpReq . Content . Headers . ContentType = new MediaTypeHeaderValue ( contentType ) ;
646
+ httpReq . Content . Headers . ContentType = MediaTypeHeaderValue . Parse ( contentType ) ;
647
647
}
648
648
requestFilter ? . Invoke ( httpReq ) ;
649
649
@@ -725,7 +725,7 @@ public static Stream SendStreamToUrl(this HttpClient client, string url, string
725
725
{
726
726
httpReq . Content = new StreamContent ( requestBody ) ;
727
727
if ( contentType != null )
728
- httpReq . Content . Headers . ContentType = new MediaTypeHeaderValue ( contentType ) ;
728
+ httpReq . Content . Headers . ContentType = MediaTypeHeaderValue . Parse ( contentType ) ;
729
729
}
730
730
requestFilter ? . Invoke ( httpReq ) ;
731
731
@@ -756,7 +756,7 @@ public static async Task<Stream> SendStreamToUrlAsync(this HttpClient client, st
756
756
{
757
757
httpReq . Content = new StreamContent ( requestBody ) ;
758
758
if ( contentType != null )
759
- httpReq . Content . Headers . ContentType = new MediaTypeHeaderValue ( contentType ) ;
759
+ httpReq . Content . Headers . ContentType = MediaTypeHeaderValue . Parse ( contentType ) ;
760
760
}
761
761
requestFilter ? . Invoke ( httpReq ) ;
762
762
@@ -1058,10 +1058,7 @@ public static HttpRequestMessage WithHeader(this HttpRequestMessage httpReq, str
1058
1058
{
1059
1059
if ( httpReq . Content == null )
1060
1060
throw new NotSupportedException ( "Can't set ContentType before Content is populated" ) ;
1061
- httpReq . Content . Headers . ContentType = new MediaTypeHeaderValue ( value . LeftPart ( ';' ) ) ;
1062
- var charset = value . RightPart ( ';' ) ;
1063
- if ( charset != null && charset . IndexOf ( "charset" , StringComparison . OrdinalIgnoreCase ) >= 0 )
1064
- httpReq . Content . Headers . ContentType . CharSet = charset . RightPart ( '=' ) ;
1061
+ httpReq . Content . Headers . ContentType = MediaTypeHeaderValue . Parse ( value ) ;
1065
1062
}
1066
1063
else if ( name . Equals ( HttpHeaders . Referer , StringComparison . OrdinalIgnoreCase ) )
1067
1064
{
@@ -1097,7 +1094,11 @@ public static HttpRequestMessage With(this HttpRequestMessage httpReq, Action<Ht
1097
1094
if ( config . UserAgent != null )
1098
1095
headers . Add ( new ( HttpHeaders . UserAgent , config . UserAgent ) ) ;
1099
1096
if ( config . ContentType != null )
1100
- headers . Add ( new ( HttpHeaders . ContentType , config . ContentType ) ) ;
1097
+ {
1098
+ if ( httpReq . Content == null )
1099
+ throw new NotSupportedException ( "Can't set ContentType before Content is populated" ) ;
1100
+ httpReq . Content . Headers . ContentType = MediaTypeHeaderValue . Parse ( config . ContentType ) ;
1101
+ }
1101
1102
if ( config . Referer != null )
1102
1103
httpReq . Headers . Referrer = new Uri ( config . Referer ) ;
1103
1104
if ( config . Authorization != null )
0 commit comments