c# - How do I include content with a GET request? -
edit: please note, know heart of problem lies in service need communicate not following protocol. software not capable of touching , not changed anytime soon. thus, need circumventing problem , breaking protocol. development @ finest!
i'm attempting communicate external service. whomever made decided split various calls not different folders, http request types. problem here, need send request includes content.
yes, violates protocol. yes, works if formulate call using linux commands. yes, works if manually craft call in fiddler (although fiddler gets angry @ breach of protocol)
when craft call, it's wrapped in async method. sending it, however, results in error:
exception thrown: 'system.net.protocolviolationexception' in mscorlib.dll ("cannot send content-body verb-type.")
code call:
/// <summary> /// gets reading sensor /// </summary> /// <param name="query">data query set data with</param> /// <returns></returns> public async task<string> getdata(string query) { var result = string.empty; try { // send request content containing query. don't ask, accept var msg = new httprequestmessage(httpmethod.get, _dataapiurl) { content = new stringcontent(query) }; var response = await _httpclient.sendasync(msg).configureawait(false); // throws exception if baby broke response.ensuresuccessstatuscode(); // convert less useless result = await response.content.readasstringasync(); } catch (exception exc) { // broke ¯\_(ツ)_/¯ _logger.errorexception("something broke in getdata(). borked connection.", exc); } return result; }
_httpclient created in constructor , system.net.http.httpclient.
does have idea how override regular protocols httpclient , force make call call, content containing query server?
to me less devastating way achieve set contentbodynotallowed
field of get
knownhttpverb
false
using reflection. can try this:
public async task<string> getdata(string query) { var result = string.empty; try { var knownhttpverbtype = typeof(system.net.authenticationmanager).assembly.gettypes().where(t => t.name == "knownhttpverb").first(); var getverb = knownhttpverbtype.getfield("get", bindingflags.nonpublic | bindingflags.static); var contentbodynotallowedfield = knownhttpverbtype.getfield("contentbodynotallowed", bindingflags.nonpublic | bindingflags.instance); contentbodynotallowedfield.setvalue(getverb.getvalue(null), false); var msg = new httprequestmessage(httpmethod.get, _dataapiurl) { content = new stringcontent(query) }; var response = await _httpclient.sendasync(msg).configureawait(false); response.ensuresuccessstatuscode(); result = await response.content.readasstringasync(); } catch (exception exc) { _logger.errorexception("something broke in getdata(). borked connection.", exc); } return result; }
Comments
Post a Comment