c# - HttpWebRequest Response Stream only returns 64k of data -
we're using web api provide restful service our application, @ moment using wpf , requires service client.
the problem we're having when response size exceeds 64k, underlying stream stops @ 64k , throws exception if more data requested, despite fact contentlength greater (in test case ~125k)
here our method calls service:
private httpwebresponse callservicemethod<tdto>(uri serviceurl, string method, out webexception requestexceptiondetail, tdto dto = null) tdto : dtobase { httpwebrequest request; httpwebresponse response; encoding enc; string encodeddto; byte[] encodeddtoarray; stream writestream; request = webrequest.createhttp(serviceurl); request.method = method; if (dto != null) { enc = utf8encoding.default; encodeddto = string.concat("=", jsonconvert.serializeobject(dto)); encodeddtoarray = enc.getbytes(encodeddto); request.contentlength = encodeddtoarray.length; request.contenttype = "application/x-www-form-urlencoded"; writestream = request.getrequeststream(); writestream.write(encodeddtoarray, 0, encodeddtoarray.length); writestream.close(); } requestexceptiondetail = null; try { response = (httpwebresponse)request.getresponse(); } catch (webexception wex) { response = (httpwebresponse)wex.response; requestexceptiondetail = wex; } return response; } ...and here's method decodes response, glue logic in between passes response through.
private tobjecttype decoderesponse<tobjecttype>(httpwebresponse webresponse) { encoding enc; streamreader responsereader; string responsetext; tobjecttype retval; // obtain string underlying response string enc = utf8encoding.default; responsereader = new streamreader(webresponse.getresponsestream(), enc); responsetext = responsereader.readtoend(); // use json deserialise string retval = jsonconvert.deserializeobject<tobjecttype>(responsetext); return retval; } we have tried sort of thing, no avail:
private tobjecttype decoderesponse<tobjecttype>(httpwebresponse webresponse) { encoding enc; streamreader responsereader; string responsetext; tobjecttype retval; char[] buffer; int buffersize, startpos, totallength, readlength; stringbuilder sbx; // obtain string underlying response string enc = utf8encoding.default; responsereader = new streamreader(webresponse.getresponsestream(), enc); totallength = (int)webresponse.contentlength; buffersize = 65536; startpos = 0; buffer = new char[buffersize]; sbx = new stringbuilder(totallength); //webresponse.getresponsestream(). while (startpos < totallength) { readlength = math.min(buffersize, totallength - startpos); responsereader.read(buffer, startpos, readlength); sbx.append(buffer, 0, readlength); startpos += readlength; } //responsetext = responsereader.readtoend(); responsetext = sbx.tostring(); // use json deserialise string retval = jsonconvert.deserializeobject<tobjecttype>(responsetext); return retval; } any appreciated ;-)
further investigation revealed httpwebrequest has static property called defaultmaximumerrorresponselength set default 65536.
it conspire .net framework includes fiendish property works long response text/plain, if requires closure - such xml or json - breaks.
for needs more information, please refer to:
Comments
Post a Comment