8.15Setting Content-Type (MIME)
When sending an HTTP remote request, if the request includes a data body, it is usually necessary to correctly set the Content-Type request header to specify the MIME type of the content being sent, such as application/json or application/x-www-form-urlencoded.
Manually entering MIME types is prone to spelling errors, which can lead to potential problems. To avoid such errors, it is recommended to use the MediaTypeNames static class provided by the framework. This class encapsulates the commonly used standard MIME type constants, effectively improving code readability and robustness.
The following is an example of using MediaTypeNames to set Content-Type:
HttpRequestBuilder.Get("https://furion.net/") .SetContentType(MediaTypeNames.Text.Plain); // text/plainHttpRequestBuilder.Post("https://furion.net/") .SetContentType(MediaTypeNames.Application.Json); // application/jsonHttpRequestBuilder.Post("https://furion.net/") .SetContentType(MediaTypeNames.Application.FormUrlEncoded); // application/x-www-form-urlencodedBy setting Content-Type in the manner above, you not only avoid manual input errors but also improve development efficiency and code maintainability.