c# - GetResponse() Error in the PayPal functionality when trying to checkout -
so i'm following wingtip toy tutorial, , know sort of old error free until got point needed checkout paypal using sandbox developing tool
this code error occurs
//retrieve response returned nvp api call paypal. httpwebresponse objresponse = (httpwebresponse)objrequest.getresponse(); string result; using (streamreader sr = new streamreader(objresponse.getresponsestream())) { result = sr.readtoend(); } return result;
and error im getting when run it
[protocolviolationexception: must write contentlength bytes request stream before calling [begin]getresponse.]
please note i'm beginner
edit: full code here
public string httpcall(string nvprequest) { string url = pendpointurl; string strpost = nvprequest + "&" + buildcredentialsnvpstring(); strpost = strpost + "&buttonsource=" + httputility.urlencode(bncode); httpwebrequest objrequest = (httpwebrequest)webrequest.create(url); objrequest.timeout = timeout; objrequest.method = "post"; objrequest.contentlength = strpost.length; try { using (streamwriter mywriter = new streamwriter(objrequest.getrequeststream())) { mywriter.write(strpost); } } catch (exception) { // no logging tutorial. } //retrieve response returned nvp api call paypal. httpwebresponse objresponse = (httpwebresponse)objrequest.getresponse(); string result; using (streamreader sr = new streamreader(objresponse.getresponsestream())) { result = sr.readtoend(); } return result; }
objrequest.contentlength = strpost.length;
what trying here? framework sets content length automatically. content length in bytes have given number of characters.
that's why error complains: have written different number of characters said write.
delete line.
your code become lot simpler if used httpclient
. should 5 lines.
Comments
Post a Comment