简体   繁体   中英

no http-connection possible after occurrence of server error 503

I build a windows-mobile 6.5 application (based on cf 2.0) and have a problem with a special test case of one method. So I hope someone can give me an advice or has a helpful idea what the reason for this behaviour is...

The method is called continuous every 30 seconds from inside a thread, looks for files to be transferred via a HTTP request to a web server (jboss) and brings them on their way. The server url itself is under my control.

Everything works fine ... until I stop the web server and force an 503 server error. So far so good. But after restarting the web server, I would expect, that the next call of the transfer method will end in success - but it does not. Every further try ends in a timeout exception and I have to restart the application to make it work again.

So my question is: where is the problem, when I want to connect to an uri after an earlier try has failed with error 503? It seems, that there is something cached, but what the hell should it be?

Many thanks for every hint you have.

Juergen

public static Boolean HttpUploadFile2(string url, string file)
    {
        HttpWebRequest requestToServer = null;
        WebResponse    response        = null;
        try
        {
            Logger.writeToLogFileCom(string.Format("Uploading {0} to {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);
            // Get the byte array of the 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;
            // Get the byte array of the string part of the 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 the total size of the HTTP request
            long totalRequestBodySize = boundaryStringLineBytes.Length * 2
                + lastBoundaryStringLineBytes.Length
                + myFileDescriptionContentDispositionBytes.Length
                + myFileContentDispositionBytes.Length
                + fileInfo.Length;
            // And indicate the value as the HTTP request content length
            requestToServer.ContentLength = totalRequestBodySize;
            // Write the http request body directly to the server                
            using (Stream s = requestToServer.GetRequestStream())
            {

                //TIMEOUT OCCURED WHEN CALLING GetRequestStream


                // Send the file description content disposition over to the server
                s.Write(boundaryStringLineBytes, 0, boundaryStringLineBytes.Length);
                s.Write(myFileDescriptionContentDispositionBytes, 0,
                    myFileDescriptionContentDispositionBytes.Length);
                // Send the file content disposition over to the server
                s.Write(boundaryStringLineBytes, 0, boundaryStringLineBytes.Length);
                s.Write(myFileContentDispositionBytes, 0,
                    myFileContentDispositionBytes.Length);
                // Send the file binaries over to the 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 the last part of the HTTP request body
                s.Write(lastBoundaryStringLineBytes, 0, lastBoundaryStringLineBytes.Length);
                Logger.writeToLogFileCom("... finished, File closed");
            } // end using        

            // Grab the response from the server. WebException will be thrown
            // when a HTTP OK status is 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);
        }
        finally
        {
            try
            {
                if (response != null)
                {                        
                    response.Close();
                }
            }
            catch (Exception ex) { }
        }
        return false;

    }

I solved the problem.

I added an additional try / catch block inside the finally clause to call getResponse in every situation.

    finally
    {

       try { response = requestToServer.GetResponse(); }
       catch (Exception ex) { } 

    [...]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM