Sending a json serialized object containing a escaped string using HttpWebRequest on mono -
Sending a json serialized object containing a escaped string using HttpWebRequest on mono -
the requirement serialize class , send server.
dev environment:
monodevelop 3.0.6
runtime: mono 2.10.9 (tarball)
gtk 2.24.10 gtk# (2.12.0.0)
operating system:
mac os x 10.7.4
the class contains string escaped double quote in it.
class customclass { public string foo = "hi!\""; }
the issue when serialize it, encode , create uri object, backslash used escape double quote in variable foo converted forwards slash, breaking json.
below values of different variables of uri instance
uri: http://myserver/hello_world/0/{"foo":"hi!/""} absoluteuri: http://myserver/hello_world/0/%7b%22foo%22%3a%22hi%21/%22%22%7d originalstring: http://myserver/hello_world/0/%7b%22foo%22%3a%22hi%21%5c%22%22%7d
the httpwebrequest send value "http://myserver/hello_world/0/{"foo":"hi!/""}"
server, requirement should utilize originalstring valid response server.
if test code on .net originalstring beingness sent server httpwebrequest class, there additional code (hack) doesn't work on mono mac
string paq = requesturi.pathandquery; fieldinfo flagsfieldinfo = typeof(uri).getfield("m_flags", bindingflags.instance | bindingflags.nonpublic); ulong flags = (ulong)flagsfieldinfo.getvalue(requesturi); flags &= ~((ulong)0x30); // flags.pathnotcanonical|flags.querynotcanonical flagsfieldinfo.setvalue(requesturi, flags);
the code :
object messageobject = new customclass(); //try 1 //string jsonstring = uri.escapeuristring(jsonconvert.serializeobject(message2)); //try 2 //string jsonstring = uri.escapedatastring(jsonconvert.serializeobject(message2)); //try 3 string jsonstring = system.web.httputility.urlencode(jsonconvert.serializeobject(messageobject)); uri uri = new uri(string.format("http://myserver/hello_world/0/{0}", jsonstring)); //try 4: //httpwebrequest request = (httpwebrequest)webrequest.create(uri.absoluteuri); //try 5 //httpwebrequest request =(httpwebrequest)webrequest.create(string.format("http://myserver/hello_world/0/{0}",jsonstring)); //try 6 httpwebrequest request = (httpwebrequest)webrequest.create(uri.originalstring); seek { using (httpwebresponse resp = request.getresponse() httpwebresponse) { var reader = new streamreader(resp.getresponsestream()); string jsonresponse = reader.readtoend(); } } grab (webexception wex) { debug.writeline(wex.tostring()); }
as can see have tried using uri.escapeuristring, uri.escapedatastring , system.web.httputility.urlencode , using absouluteuri , originalstring create webrequest instance , creating using string.
i have tried using next code in config.
<uri> <schemesettings> <clear/> <add name="http" genericuriparseroptions="dontunescapepathdotsandslashes"/> </schemesettings> </uri>
but none of methods work , encoding lost when request in created.
any ideas work appreciated.
update:
i have tried lot of things including testing 3rd party open source code, still faced same issue when sending in encoded url.
i modified code utilize webclient instead of webrequest still no luck.
so solution worked me on both monomac monotouch(simulator, haven't tested on device) create request using tcpclient.
using (tcpclient tc = new tcpclient()) { tc.connect ("myserver", 80); using (networkstream ns = tc.getstream()) { using (system.io.streamwriter sw = new system.io.streamwriter(ns)) { using (system.io.streamreader sr = new system.io.streamreader(ns)) { sw.write("get /hello_world/0/%7b%22foo%22:%22hi!%5c%22%22%7d http/1.1 host:myserver \r\n\r\n"); sw.flush (); string line; while ((line=sr.readline())!=null) console.write (line); } } } }
json mono monotouch httpwebrequest monomac
Comments
Post a Comment