c# Webrequest Post and GetResponse -


i writing program submit xml website. code written works fine, but stops working reason, throwing system.net.protocolviolationexception. can close program , re-run - starts working again fine.

here code using:

private string summit(string xml) {     string result = string.empty;     stringbuilder sb = new stringbuilder();     try {         webrequest request = webrequest.create(this.targeturl);         request.timeout = 800 * 1000;          requeststate requeststate = new requeststate(xml);         requeststate.request = request;         request.contenttype = "text/xml";          // set 'method' property  'post' post data uri.         requeststate.request.method = "post";         requeststate.request.contenttype = "text/xml";          // start asynchronous 'begingetrequeststream' method call.             iasyncresult r = (iasyncresult)request.begingetrequeststream(new asynccallback(readcallback), requeststate);          // pause current thread until async operation completes.         // console.writeline("main thread waiting...");          alldone.waitone();         // assign response object of 'webrequest' 'webresponse' variable.         webresponse response = null;         try {             response =request.getresponse();         } catch (system.net.protocolviolationexception ex) {             response = null;             request.abort();              request = null;             requeststate = null;             return "";         }         //console.writeline("the string has been posted.");         //console.writeline("please wait response...");          stream streamresponse = response.getresponsestream();         streamreader streamread = new streamreader(streamresponse);         char[] readbuff = new char[256];         int count = streamread.read(readbuff, 0, 256);          //stringbuilder sb = new stringbuilder();         while (count > 0) {             string outputdata = new string(readbuff, 0, count);             sb.append(outputdata);             count = streamread.read(readbuff, 0, 256);         }          // close stream object.         streamresponse.close();         streamread.close();         //alldone.waitone();          // release httpwebresponse resource.         response.close();         //return sb.tostring();     } catch (webexception webex) {         debug.writeline(webex.message);      } catch (system.web.services.protocols.soapexception soapex) {         debug.writeline(soapex.message);     } catch (system.net.protocolviolationexception ex) {         debug.writeline(ex.message);     } catch (exception ex) {         debug.writeline(ex.message);     }     return sb.tostring(); }   private static void readcallback(iasyncresult asyncresult) {     try {         requeststate myrequeststate = (requeststate)asyncresult.asyncstate;         webrequest mywebrequest2 = myrequeststate.request;          // end of asynchronus request.         stream responsestream = mywebrequest2.endgetrequeststream(asyncresult);          //convert  string byte array.         asciiencoding encoder = new asciiencoding();         byte[] bytearray = encoder.getbytes(myrequeststate.xml);          // write data stream.         responsestream.write(bytearray, 0, myrequeststate.xml.length);         responsestream.close();                       } catch (webexception e) {         console.writeline("\nreadcallback exception raised!");         console.writeline("\nmessage:{0}", e.message);         console.writeline("\nstatus:{0}", e.status);     }     alldone.set(); } 

response =request.getresponse() when fails , gives error

you must provide request body if set contentlength>0 or sendchunked==true. calling [begin]getrequeststream before [begin]getresponse.

any appreciated.

this gets tricky since we're doing async calls.

do in following order:

request.begingetrequeststream(new asynccallback(getrequeststreamcallback), request) 

then in 'getrequeststreamcallback(iasyncresult asynchronousresult)' call:

request.begingetresponse(new asynccallback(getresponsecallback), request) 

lastly, in getresponse, sure close stream:

response.close(); alldone.set(); 

msdn job explaining it: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream.aspx


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -