c# - Receiving application/octet-stream post with webapi2 -
i working on solution send xml content our api on post.
i using below code , works when xml-content delivered php-script using curl. when try same client using c#, not getting correct xml content out.
server code:
using system; using system.io; using system.net.http; using system.net.http.formatting; using system.net.http.headers; using system.text; using system.threading.tasks; using log4net; public class binarytextmediatypeformatter : mediatypeformatter { /// <summary> /// _log. /// </summary> private readonly ilog log = logmanager.getlogger(typeof(binarytextmediatypeformatter)); /// <summary> /// initializes new instance of <see cref="binarytextmediatypeformatter"/> class. /// </summary> public binarytextmediatypeformatter() { this.supportedmediatypes.add(new mediatypeheadervalue("application/octet-stream")); } /// <summary> /// read stream async. /// </summary> /// <param name="type"> /// type. /// </param> /// <param name="readstream"> /// read stream. /// </param> /// <param name="content"> /// content. /// </param> /// <param name="formatterlogger"> /// formatter logger. /// </param> /// <returns> /// <see cref="task"/>. /// </returns> public override task<object> readfromstreamasync(type type, stream readstream, httpcontent content, iformatterlogger formatterlogger) { var taskcompletionsource = new taskcompletionsource<object>(); try { var memorystream = new memorystream(); readstream.copyto(memorystream); var streamvalues = encoding.default.getstring(memorystream.toarray()); taskcompletionsource.setresult(streamvalues.trim()); } catch (exception e) { this.log.error(e); taskcompletionsource.setexception(e); } return taskcompletionsource.task; } /// <summary> /// can read type. /// </summary> /// <param name="type"> /// type. /// </param> /// <returns> /// <see cref="bool"/>. /// </returns> public override bool canreadtype(type type) { return type == typeof(string); } /// <summary> /// can write type. /// </summary> /// <param name="type"> /// type. /// </param> /// <returns> /// <see cref="bool"/>. /// </returns> public override bool canwritetype(type type) { return false; } }
client code (this 1 has 1 textbox can add xml, response-text-box print out response , send-button):
/// <summary> /// initializes new instance of <see cref="form1"/> class. /// </summary> public form1() { this.initializecomponent(); } /// <summary> /// btn send_ click. /// </summary> /// <param name="sender"> /// sender. /// </param> /// <param name="e"> /// e. /// </param> private void btnsendclick(object sender, eventargs e) { try { var bytes = system.text.encoding.default.getbytes(this.txtcontent.text); var postdata = getbytes(system.text.encoding.utf8.getstring(bytes)); const string uri = "http://dev.sportsdata.no/api/api/fileclient/odf"; byte[] response; using (var client = new webclient()) { response = client.uploaddata(uri, "post", postdata); } this.txtresonse.text += environment.newline; this.txtresonse.text += @"response: " + response; } catch (exception exception) { this.txtresonse.text += environment.newline + @"starting loop" + environment.newline; foreach (keyvaluepair<string, string> kvp in exception.data) { this.txtresonse.text += @"key: " + kvp.key + @", value: " + kvp.value + environment.newline; } this.txtresonse.text += @"ending loop" + environment.newline; this.txtresonse.text += @"exception: " + exception.message + environment.newline + exception.stacktrace; } } /// <summary> /// bytes. /// </summary> /// <param name="str"> /// str. /// </param> /// <returns> /// <see cref="byte[]"/>. /// </returns> private static byte[] getbytes(string str) { var bytes = new byte[str.length * sizeof(char)]; buffer.blockcopy(str.tochararray(), 0, bytes, 0, bytes.length); return bytes; }
some output log:
this content: <2016-02-08 13:35:56,673 [13] error 166ogger [(null)] [(null)] - system.xml.xmlexception: name cannot begin '.' character, hexadecimal value 0x00. line 1, position 2. @ system.xml.xmltextreaderimpl.throw(exception e) @ system.xml.xmltextreaderimpl.throw(string res, string[] args) @ system.xml.xmltextreaderimpl.parseqname(boolean isqname, int32 startoffset, int32& colonpos) @ system.xml.xmltextreaderimpl.parseelement() @ system.xml.xmltextreaderimpl.parsedocumentcontent() @ system.xml.xmltextreaderimpl.read() @ system.xml.xmlloader.load(xmldocument doc, xmlreader reader, boolean preservewhitespace) @ system.xml.xmldocument.load(xmlreader reader) @ system.xml.xmldocument.loadxml(string xml)
Comments
Post a Comment