c# - MVC website doesn't receive string from client -
client:
webclient wc = new webclient(); try { string json = wc.uploadstring("http://localhost:50001/client/index", "1"); dynamic receiveddata = jsonconvert.deserializeobject(json); console.writeline("result: {0};",receiveddata.data); } catch (exception e) { console.writeline("oh bother"); console.writeline(); console.writeline(e.message); }
basically sends "1" index
action in client
controller.
here controller:
[httppost] public actionresult index(string k) { debug.writeline(string.format("result: {0};", k)); return json(new { data = k}, jsonrequestbehavior.denyget); }
the result client
"result: ;". debug output controller "result: ;". means data lost somewhere between client , site. when debug, visual studio says there 1 request.
by adding header , specifying parameter name, i've managed work (in calling method):
static void main(string[] args) { webclient wc = new webclient(); try { wc.headers[httprequestheader.contenttype] = "application/x-www-form-urlencoded"; string json = wc.uploadstring("http://localhost:49847/home/index", "k=1"); dynamic receiveddata = jsonconvert.deserializeobject(json); console.writeline("result: {0};", receiveddata.data); } catch (exception e) { console.writeline("oh bother"); console.writeline(); console.writeline(e.message); } }
from msdn:
...set http content-type header application/x-www-form-urlencoded, notify server form data attached post.
i haven't run fiddler check (if any) headers sent through default suspect reason doesn't work without header receiving client doesn't know find query string parameters passed through.
from another answer on stackoverflow:
when receiving post request, should expect "payload", or, in http terms: message body. message body in pretty useless, there no standard (as far can tell. maybe application/octet-stream?) format. body format defined content-type header. when using html form element method="post", application/x-www-form-urlencoded.
Comments
Post a Comment