c# - Given a HttpResponseMessage, how do I read the content of the request? -
given system.net.http.httpresponsemessage, can quite lot of information request made through response.requestmessage, example
response.requestmessage.requesturi // url of request response.requestmessage.method // http method however, can't figure out way useful from
response.requestmessage.content // stringcontent instance i've looked through property tree of stringcontent, can't figure out how contents regular string in way works in watch window.
any suggestions?
analyzing system.net.http.dll v2.2.29.0 ilspy shows system.net.http.httpclienthandler.createresponsemessage (that initializes httpresponse object) doesn't corresponding httprequest's .content. means if wasn't null in first place, content object itself should still available.
system.objectdisposedexception thrown "when operation performed on disposed object." "disposed object" mean? system.net.http.stringcontent turns out implement system.idisposable through ancestor, system.net.http.httpcontent. so, means "an idisposable after .dispose() method called."
naturally, searching httpcontent.dispose() users brings the culprit - system.net.http.httpclient.sendasync() calls system.net.http.httpclient.disposerequestcontent() after sending data.
now, regarding do. httpcontent.dispose() close object's streams , set flag. however, streamcontent (or rather parent, bytearraycontent) keeps data in .content field - isn't touched disposition!
alas, both methods read directly protected, , public methods use check flag first. so, way read reflection (the illustration in ironpython, notes given c# equivalents):
>>> sc=system.net.http.stringcontent("abcdefghijklmnopqrstuvwxyz") #in c#, following `type(stringcontent)' (this does) >>> scd=system.reflection.typedelegator(system.net.http.stringcontent) >>> print scd.getfield("content",system.reflection.bindingflags.nonpublic|system.reflection.bindingflags.instance) none #because field in bytearraycontent , private >>> bacd=system.reflection.typedelegator(system.net.http.bytearraycontent) >>> bacd.getfield("content",system.reflection.bindingflags.nonpublic|system.reflection.bindingflags.instance) <system.reflection.rtfieldinfo object @ 0x000000000000002c [byte[] content]> # `_' last result >>> _.getvalue(sc) array[byte]((<system.byte object @ 0x000000000000002d [97]>, <system.byte objec t @ 0x000000000000002e [98]>, <system.byte object @ 0x000000000000002f [99]>, <...> in c#, like:
type(bytearraycontent) .getfield("content",bindingflags.nonpublic|bindingflags.instance) .getvalue(content)
Comments
Post a Comment