2017-09-30 15 views
0

lors de la publication des données avec PostUrlEncodedAsync de Flurl, le type de contenu suivant est automatiquement:Comment supprimer charset = utf-8 de Content-Type PostUrlEncodedAsync lors de l'utilisation

application/x-www-form-urlencoded; charset = utf-8

Comment supprimer la partie charset = utf-8?

Je l'ai déjà essayé:

flurlClient.ConfigureHttpClient(c => c.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded")); 

mais cela ne fonctionne pas. Suggestion vient de: https://stackoverflow.com/a/44548514/915414

Répondre

0

Au lieu d'utiliser Flurl, j'ai essayé d'atteindre le même objectif avec HttpClient. Cela n'a pas fonctionné, j'ai donc créé une méthode d'extension pour Flurl à la place.

https://stackoverflow.com/a/44543016/915414 suggère d'utiliser StringContent pour modifier le Content-Type:

var jobInJson = JsonConvert.SerializeObject(job); 
var json = new StringContent(jobInJson, Encoding.UTF8); 
json.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; odata=verbose"); 

var flurClient = GetBaseUrlForOperations("Jobs"); 

return await flurClient.PostAsync(json).ReceiveJson<Job>(); 

Bien que cela ne change le Content-Type, le charset = utf-8 reste encore.

J'ai décompilé System.Net.Http.StringContent pour voir comment cela fonctionne. par défaut à un charset:

this.Headers.ContentType = new MediaTypeHeaderValue(mediaType == null ? "text/plain" : mediaType) 
{ 
    CharSet = encoding == null ? HttpContent.DefaultStringEncoding.WebName : encoding.WebName 
}; 

Et devinez quoi ... Au cœur, PostUrlEncodedAsync utilise StringContent. J'ai donc créé une méthode d'extension pour Flurl, qui utilise une implémentation similaire de StringContent, où CharSet = "";

PostUrlEncodedAsyncWithoutCharset:

public static class HttpExtensions 
{ 
    public static Task<HttpResponseMessage> PostUrlEncodedAsyncWithoutCharset(this IFlurlClient client, object data, CancellationToken cancellationToken = default(CancellationToken), HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) 
    { 
     CapturedUrlContentCustom urlEncodedContent = new CapturedUrlContentCustom(client.Settings.UrlEncodedSerializer.Serialize(data)); 
     return client.SendAsync(HttpMethod.Post, (HttpContent)urlEncodedContent, new CancellationToken?(cancellationToken), completionOption); 
    } 
} 

CapturedUrlContentCustom:

public class CapturedUrlContentCustom : CapturedStringContentCustom 
{ 
    public CapturedUrlContentCustom(string data) 
    : base(data, (Encoding) null, "application/x-www-form-urlencoded") 
    { 
    } 
} 

CapturedStringContentCustom:

public class CapturedStringContentCustom : CustomStringContent 
{ 
    public string Content { get; } 
    public CapturedStringContentCustom(string content, Encoding encoding = null, string mediaType = null) 
     : base(content, encoding, mediaType) 
    { 
     this.Content = content; 
    } 
} 

CustomStringContent:

public class CustomStringContent : ByteArrayContent 
    { 
     private const string defaultMediaType = "application/x-www-form-urlencoded"; 


     public CustomStringContent(string content) 
     : this(content, (Encoding)null, (string)null) 
     { 
     } 


     public CustomStringContent(string content, Encoding encoding) 
     : this(content, encoding, (string)null) 
     { 
     } 


     public CustomStringContent(string content, Encoding encoding, string mediaType) 
     : base(CustomStringContent.GetContentByteArray(content, encoding)) 
     { 
      this.Headers.ContentType = new MediaTypeHeaderValue(mediaType == null ? "application/x-www-form-urlencoded" : mediaType) 
      { 
       CharSet = "" 
      }; 
     } 

     private static byte[] GetContentByteArray(string content, Encoding encoding) 
     { 
      if (content == null) 
       throw new ArgumentNullException(nameof(content)); 
      if (encoding == null) 
       encoding = Encoding.UTF8; 
      return encoding.GetBytes(content); 
     } 
    } 

Maintenant, vous pouvez appeler PostUrlEncodedAsyncWithoutCharset et charset = utf-8 n'apparaîtra pas.