2017-09-30 4 views
0

を使用した場合FlurlのPostUrlEncodedAsyncでデータを投稿するときは、次のContent-Typeが自動的に設定されているコンテンツタイプからのcharset = UTF-8を削除するには:どのようPostUrlEncodedAsync

アプリケーション/ x-www-form-urlencodedで。 charset = utf-8

charset = utf-8の部分を削除するにはどうすればよいですか?

は、私はすでに試した:

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

が、それは仕事をdoesntの。提案は次のとおりです。https://stackoverflow.com/a/44548514/915414

答えて

0

Flurlを使用する代わりに、HttpClientで同じ目標を達成しようとしました。それで、私はFlurlの拡張メソッドを作成しました。

https://stackoverflow.com/a/44543016/915414は、Content-Typeのを変更するにはStringContentを使用することを提案:

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>(); 

これは、コンテンツタイプを変更していますが、文字セット= UTF-8が残っています。

System.Net.Http.StringContentを逆コンパイルして動作を確認しました。この文字セットデフォルト:

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

そして、その中核となるのは、PostUrlEncodedAsyncはStringContentを使用するもの...と思います。

したがって、Flurlの拡張メソッドを作成しました。これは、StringContentの同様の実装を使用しています。ここで、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); 
     } 
    } 

これでPostUrlEncodedAsyncWithoutCharsetを呼び出すことができ、charset = utf-8は表示されません。