如何解决c#.net windows服务中的操作超时问题?

编程入门 行业动态 更新时间:2024-10-27 13:27:45
本文介绍了如何解决c# windows服务中的操作超时问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好, 如何解决c# windows服务中的操作超时问题? 我正在编写此代码: -

Hello, How to resolve operation timed out problem in c# windows service? I am writing this code:-

private void DownloadFile(string file) { FTPSettings.IP = "abc/xyz"; FTPSettings.UserID = "xxxxx"; FTPSettings.Password = "xxxxx"; string uri = "ftp://" + FTPSettings.IP + "/" + "Testing" + "/" + file; Uri serverUri = new Uri(uri); if (serverUri.Scheme != Uri.UriSchemeFtp) { return; } FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + FTPSettings.IP + "/" + "Testing" + "/" + file)); reqFTP.Credentials = new NetworkCredential(FTPSettings.UserID, FTPSettings.Password); reqFTP.KeepAlive = false; reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; reqFTP.UseBinary = true; reqFTP.Proxy = null; reqFTP.UsePassive = false; FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); Stream responseStream = response.GetResponseStream(); FileStream writeStream = new FileStream(@"D:\ABC\XYZ\bin\" + file, FileMode.Create); int Length = 2048; Byte[] buffer = new Byte[Length]; int bytesRead = responseStream.Read(buffer, 0, Length); while (bytesRead > 0) { writeStream.Write(buffer, 0, bytesRead); bytesRead = responseStream.Read(buffer, 0, Length); } writeStream.Close(); response.Close(); }

我收到错误操作已经超时。 请帮助我。 先谢谢。 Ankit Agarwal 软件工程师

My is getting error "Operation has timed out. Please help me. Thanks in Advance. Ankit Agarwal Software Engineer

推荐答案

以下代码经过测试,没有遇到超时 The following code was tested and did not encounter a timeout // ********************************************* download_file /// <summary> /// uses FTP to down load a file from a web location to a /// local file /// /// following discussions are based on /// /// www.host.domain/path/file.ext /// /// </summary> /// <param name="host"> /// host server; in the above "www.server.domain" /// </param> /// <param name="path"> /// path within the host; in the above "path" /// </param> /// <param name="filename"> /// full file name, including extension, if applicable; in the /// above "file.ext" /// </param> /// <param name="user_name"> /// username of user making request; if the request is /// anonymous, supply either "anonymous" or a null or empty /// string /// </param> /// <param name="password"> /// password of the user making the request; if the request is /// anonymous, supply the email address of the user making the /// request /// </param> /// <param name="target_path"> /// supply the fully qualified name of the target location on /// the local machine; insure the location is writeable /// </param> void download_file ( string host, string path, string filename, string user_name, string password, string target_path ) { StringBuilder sb = new StringBuilder ( ); sb.Append ( "ftp://" ); sb.Append ( host ); sb.Append ( "/" ); sb.Append ( path ); sb.Append ( "/" ); sb.Append ( filename ); try { FtpWebRequest request; request = ( FtpWebRequest ) WebRequest.Create ( sb.ToString ( ) ); request.Method = WebRequestMethods.Ftp.DownloadFile; if ( String.IsNullOrEmpty ( user_name ) ) { user_name = "anonymous"; } request.Credentials = new NetworkCredential ( user_name, password ); using ( FtpWebResponse response = ( FtpWebResponse ) request. GetResponse ( ) ) { using ( Stream rs = response. GetResponseStream ( ) ) { using ( StreamReader sr = new StreamReader ( rs ) ) { File.WriteAllText ( target_path, sr.ReadToEnd ( ) ); } } MessageBox.Show ( String.Format ( "Download Complete {0}status{0}{1}", Environment.NewLine, response.StatusDescription ) ); } } catch ( Exception ex ) { MessageBox.Show ( String.Format ( "Download Failed{0}{1}{2}", Environment.NewLine, ex.Message, ex.StackTrace ) ); } }

用法是:

The Usings were:

using System; using System.IO; using System.Net; using System.Text; using System.Windows.Forms;

希望有所帮助。

Hope that helps.

更多推荐

如何解决c#.net windows服务中的操作超时问题?

本文发布于:2023-07-16 03:43:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117478.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何解决   操作   net   windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!