Today encountered a requirement, request to access my API with HTTPS + XML, ordinary webapi is not supported by this request, so do the following code to support
Add a class with the class name Plaintexttypeformatter
Public classplaintexttypeformatter:mediatypeformatter{ PublicPlaintexttypeformatter () {Supportedmediatypes.add (NewMediatypeheadervalue ("Text/plain")); } Public Override BOOLcanreadtype (Type type) {returnType = =typeof(string); } Public Override BOOLcanwritetype (Type type) {returnType = =typeof(string); } Public Override AsyncTask writetostreamasync (Type type,Objectvalue, Stream writestream, httpcontent content, TransportContext transportcontext) { using(varSW =NewStreamWriter (Writestream)) { awaitSW. WriteAsync (value. ToString ()); } } Public Override Asynctask<Object>readfromstreamasync (Type type, Stream readstream, httpcontent content, Iformatterlogger Formatterlogger) { using(varSR =NewStreamReader (Readstream)) { return awaitSr. Readtoendasync (); } }}
Then register the class to Forrmator, (add the following code to the Webapiconfig)
Config. Formatters.add (new plaintexttypeformatter ());
Then do not forget to add the header at the request, otherwise the request cannot be processed
Content-type:text/plain
The following is the request result in fiddler
WEBAPI Support for Text/plain requests