在发生服务器错误503之后,不可能进行http连接



我构建了一个windows mobile 6.5应用程序(基于cf 2.0),但有一个方法的特殊测试用例存在问题。所以我希望有人能给我一个建议,或者有一个有用的想法,这种行为的原因是什么…

该方法每隔30秒从线程内部调用一次continuous,查找要通过HTTP请求传输到web服务器(jboss)的文件,并将其发送到web服务器。服务器url本身在我的控制之下。

一切都很好。。。直到我停止web服务器并强制出现503服务器错误。到目前为止还不错。但是,在重新启动web服务器后,我预计传输方法的下一次调用将以成功告终,但事实并非如此。每一次尝试都会以超时异常结束,我必须重新启动应用程序才能使其再次工作。

所以我的问题是:当我想在之前的尝试失败并出现错误503后连接到uri时,问题在哪里?似乎有什么东西被缓存了,但它到底应该是什么?

非常感谢你的每一个提示。

于尔根

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 = "rn--" + boundaryString + "rn";
            byte[] boundaryStringLineBytes = ascii.GetBytes(boundaryStringLine);
            string lastBoundaryStringLine = "rn--" + boundaryString + "--rn";
            byte[] lastBoundaryStringLineBytes =  ascii.GetBytes(lastBoundaryStringLine);
            // Get the byte array of the myFileDescription content disposition
            string myFileDescriptionContentDisposition = String.Format(
                "Content-Disposition: form-data; name="{0}"rnrn{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}"rnContent-Type: {2}rnrn",
                "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;
    }

我解决了这个问题。

我在finally子句中添加了一个额外的try/catch块,以便在任何情况下调用getResponse。

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

相关内容

  • 没有找到相关文章

最新更新