c# - httpwebrequest gives timeout until restarted -
i working on desktop application developed in c# (.net environment).
this application connects remote server using httpwebrequest. if due reason pc disconnected internet , re-connect application gives request timeout httpwebrequest until restart whole application , if again add new thread application after network d/c works fine.
is there way reset network or can tell me how work?
//my code is..
public string request(string add, string post, int time, string reff, int id, int rwtime) { try { if (rwtime == 0) { rwtime = 100000; } string result = ""; string location = ""; // create web request httpwebrequest req = webrequest.create(add) httpwebrequest; req.readwritetimeout = rwtime; req.keepalive = true; req.headers.add(httprequestheader.acceptencoding, "gzip,deflate"); req.accept = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; req.contenttype = "application/x-www-form-urlencoded"; req.timeout = time; req.referer = reff; req.allowautoredirect = false; req.cookiecontainer = statictk.cc[id]; req.preauthenticate = true; if (post != "") { req.method = "post"; string postdata = post; asciiencoding encoding = new asciiencoding(); byte[] byte1 = encoding.getbytes(postdata); // set content type of data being posted. req.contenttype = "application/x-www-form-urlencoded"; // set content length of string being posted. req.contentlength = byte1.length; stream newstream = req.getrequeststream(); newstream.write(byte1, 0, byte1.length); newstream.close(); } else { req.method = "get"; } // response try { httpwebresponse response = req.getresponse() httpwebresponse; // response stream location = response.getresponseheader("location"); if (location == "") { stream responsestream = response.getresponsestream(); if (response.contentencoding.tolower().contains("gzip")) responsestream = new gzipstream(responsestream, compressionmode.decompress); else if (response.contentencoding.tolower().contains("deflate")) responsestream = new deflatestream(responsestream, compressionmode.decompress); streamreader reader = new streamreader(responsestream, encoding.default); // read whole contents , return string result = reader.readtoend(); } else { result = location; } response.close(); if (result == "") result = "retry"; return result; } catch (exception e) { log.store("errorinresponce", e.message); if (statictd.status[id] != "removed") { return "retry"; } else { return "error"; } } } catch(exception f) { log.store("networkerrorretry", f.message); if (f.message == "the operation has timed out") { return "retry"; } string ans = messagebox.show("there network error..wish retry ?\nerror msg : "+ f.message, "title", messageboxbuttons.yesno).tostring(); if (ans == "yes") return "retry"; else { invoketk.settxt(id, "not ready"); return "error"; } } }
it sounds application missing error handling. disconnect can happen @ time , application should able handle it. try surround network loop try-catch statement, , catch different kinds of exceptions. depending on exception thrown, can decide if reconnect server silently or if want generate error message.
Comments
Post a Comment