c# - no http-connection possible after occurrence of server error 503 -
i build windows-mobile 6.5 application (based on cf 2.0) , have problem special test case of 1 method. hope can give me advice or has helpful idea reason behaviour is...
the method called continuous every 30 seconds inside thread, looks files transferred via http request web server (jboss) , brings them on way. server url under control.
everything works fine ... until stop web server , force 503 server error. far good. after restarting web server, expect, next call of transfer method end in success - not. every further try ends in timeout exception , have restart application make work again.
so question is: problem, when want connect uri after earlier try has failed error 503? seems, there cached, hell should be?
many every hint have.
juergen
public static boolean httpuploadfile2(string url, string file) { httpwebrequest requesttoserver = null; webresponse response = null; try { logger.writetologfilecom(string.format("uploading {0} {1}", file, url)); requesttoserver = (httpwebrequest)webrequest.create(url); requesttoserver. timeout = 40000; string boundarystring = "----sslblabla"; requesttoserver.allowwritestreambuffering = false; requesttoserver.method = "post"; requesttoserver.contenttype = "multipart/form-data; boundary=" + boundarystring; requesttoserver.keepalive = false; asciiencoding ascii = new asciiencoding(); string boundarystringline = "\r\n--" + boundarystring + "\r\n"; byte[] boundarystringlinebytes = ascii.getbytes(boundarystringline); string lastboundarystringline = "\r\n--" + boundarystring + "--\r\n"; byte[] lastboundarystringlinebytes = ascii.getbytes(lastboundarystringline); // byte array of myfiledescription content disposition string myfiledescriptioncontentdisposition = string.format( "content-disposition: form-data; name=\"{0}\"\r\n\r\n{1}", "myfiledescription", "a sample file description"); byte[] myfiledescriptioncontentdispositionbytes = ascii.getbytes(myfiledescriptioncontentdisposition); string fileurl = file; // byte array of string part of myfile content // disposition string myfilecontentdisposition = string.format( "content-disposition: form-data;name=\"{0}\"; " + "filename=\"{1}\"\r\ncontent-type: {2}\r\n\r\n", "myfile", path.getfilename(fileurl), path.getextension(fileurl)); byte[] myfilecontentdispositionbytes = ascii.getbytes(myfilecontentdisposition); fileinfo fileinfo = new fileinfo(fileurl); // calculate total size of http request long totalrequestbodysize = boundarystringlinebytes.length * 2 + lastboundarystringlinebytes.length + myfiledescriptioncontentdispositionbytes.length + myfilecontentdispositionbytes.length + fileinfo.length; // , indicate value http request content length requesttoserver.contentlength = totalrequestbodysize; // write http request body directly server using (stream s = requesttoserver.getrequeststream()) { //timeout occured when calling getrequeststream // send file description content disposition on server s.write(boundarystringlinebytes, 0, boundarystringlinebytes.length); s.write(myfiledescriptioncontentdispositionbytes, 0, myfiledescriptioncontentdispositionbytes.length); // send file content disposition on server s.write(boundarystringlinebytes, 0, boundarystringlinebytes.length); s.write(myfilecontentdispositionbytes, 0, myfilecontentdispositionbytes.length); // send file binaries on server, in 1024 bytes chunk filestream filestream = new filestream(fileurl, filemode.open, fileaccess.read); byte[] buffer = new byte[1024]; int bytesread = 0; logger.writetologfilecom("writing data..."); while ((bytesread = filestream.read(buffer, 0, buffer.length)) != 0) { s.write(buffer, 0, bytesread); } // end while filestream.close(); logger.writetologfilecom("... finished, file closed"); // send last part of http request body s.write(lastboundarystringlinebytes, 0, lastboundarystringlinebytes.length); logger.writetologfilecom("... finished, file closed"); } // end using // grab response server. webexception thrown // when http ok status not returned logger.writetologfilecom("lese response"); response = requesttoserver.getresponse(); streamreader responsereader = new streamreader(response.getresponsestream()); string replyfromserver = responsereader.readtoend(); response.close(); if (regex.split(regex.split(replyfromserver, "content\\:response\"\\>")[1], "\\</span\\>")[0].equals("ok")) { return true; } else { return false; } } catch (exception ex) { logger.writetologfilecom("fehler im html sender"); logger.writetologfilecom(ex.message); logger.writetologfilecom(ex.stacktrace); } { try { if (response != null) { response.close(); } } catch (exception ex) { } } return false; }
i solved problem.
i added additional try / catch block inside clause call getresponse in every situation.
{ try { response = requesttoserver.getresponse(); } catch (exception ex) { } [...]
Comments
Post a Comment