如何解决c#.net windows服务中的操作超时问题? [英] How to resolve operation timed out problem in c# .net windows service?

查看:119
本文介绍了如何解决c#.net windows服务中的操作超时问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



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



我正在编写此代码: -

Hello,

How to resolve operation timed out problem in c# .net windows service?

I am writing this code:-

private void DownloadFile(string file)
       {

 FTPSettings.IP = "abc.com/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服务中的操作超时问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆