Problems POSTing data to an asp.net webpage (problem in Desktop C# end) -
thank taking time read post! having problem posting data c# desktop application c# asp.net web page. believe problem lies in desktop application (or @ least 1 of problems does!) post asp.net code using. if asp.net not speciality, don't worry, wondering if there glaringly obvious there well.
i had create asp.net website post data windows forms application. working perfectly.
here code using. not working discussed below. bad @ asp.net stuff, can provide appreciated.
if (system.net.networkinformation.networkinterface.getisnetworkavailable() && result == dialogresult.yes) { string test = "test"; webrequest request = webrequest.create("http://localhost/test.aspx"); byte[] bytearray = encoding.utf8.getbytes(test); request.method = "post"; request.contenttype = "application/x-www-form-urlencoded"; request.contentlength = bytearray.length; // give response using (stream datastream = request.getrequeststream()) { datastream.write(bytearray, 0, bytearray.length); } }
however, when debug application, , put breakpoint after datastream.write() errors in variable watch window. not exceptions except in there.
i cannot upload image website seems, shall upload freewebs site - sorry, embarrassing! watch.jpg
as can see, getting system.notsupported on datastream.length , datastream.position
could please me fix this? thanks!
just in case asp.net programmer sees this, there problem receiving code?:
protected void page_load(object sender, eventargs e) { string test = request.binaryread(request.totalbytes).tostring(); }
thank all, so, time!
richard
edit: in relation gandjustas's comment, providing more information.
something in chain not working. not getting formal exceptions report.
if use code in asp.net webpage:
string test = request.binaryread(request.totalbytes).tostring(); response.clear(); response.write(test); response.end();
i following response back: system.byte[]
this not variable, string containing arbitrary words , symbols 'system.byte[]'
something not working (obviously) see system.notsupportedexception in watch window. make me think there 2 errors: system.notsupportedexception needs fixing in c# desktop application, , asp.net webpage should not displaying system.byte[] before have sent post application.
i kind of need help. thanks!
couple of remarks code:
- you setting
application/x-www-form-urlencoded
content type sending arbitrary byte array. when set content type server expect request encoded using it. - the
notsupportedexception
getting in debug window normal. cannot uselength
property onnetworkstream
.
let me try simplify code in case want use application/x-www-form-urlencoded
:
client:
using (var client = new webclient()) { var values = new namevaluecollection { { "key1", "value1" }, { "key2", "value2" }, }; byte[] result = client.uploadvalues("http://example.com/test.aspx", values); }
server:
protected void page_load(object sender, eventargs e) { string key1 = request["key1"]; string key2 = request["key2"]; }
Comments
Post a Comment