c# - Send more than one post request -
i want send multiply post requests website. i've search on google , got code:
class program { public static httpwebrequest httpwreq = (httpwebrequest)webrequest.create("http://example.com/login.php"); public static asciiencoding encoding = new asciiencoding(); public static string postdata = ""; public static bool first = true; static void main(string[] args) { string uname = "", pass = "", final = ""; while (1>0) { console.writeline("enter uname ,then password"); uname = console.readline(); pass = console.readline(); if (uname == "0") break; final = letstry(uname, pass); console.writeline(final); console.writeline("finish that"); } } public static string letstry(string uname, string pass) { postdata = "uname=" + uname; postdata += ("&pass=" + pass); byte[] data = encoding.getbytes(postdata); if (first) { httpwreq.method = "post"; httpwreq.contenttype = "application/x-www-form-urlencoded"; httpwreq.contentlength = data.length; first = !first; } stream newstream = httpwreq.getrequeststream(); newstream.write(data, 0, data.length); newstream.close(); webresponse resp = httpwreq.getresponse(); streamreader sr = new system.io.streamreader(resp.getresponsestream()); return sr.readtoend().trim(); } } i'm getting error connection closed (newstream). why can't use same connection send more 1 request?
the idea can think it's send stream var letstry, instead of creating newstream.
i'm no expert, i'm sorry unnecessary mistakes.
tyvm help:)
webrequest designed says: make 1 request. if want make multiple requests, create new webrequest each time.
if keepalive property true, requests try use same connection, if possible. see understanding system.net connection management , servicepointmanager more information.
Comments
Post a Comment