【问题标题】:HttpRequest Timeout exception when making http request from a class inherited from System.Configuration.Install.Installer从继承自 System.Configuration.Install.Installer 的类发出 http 请求时出现 HttpRequest 超时异常
【发布时间】:2011-10-18 08:14:35
【问题描述】:

我想在安装完成后启动一个EXE,所以我写了一个自定义启动条件,如下所示:

    [RunInstaller(true)]
        public class InstallerClass : System.Configuration.Install.Installer
        {    
            public InstallerClass() : base()
            {            
                this.AfterInstall += new InstallEventHandler(InstallerClass_AfterInstall);
            }        
            void InstallerClass_AfterInstall(object sender, InstallEventArgs e)
            {
                Directory.SetCurrentDirectory(
                Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
                ProcessStartInfo psi = new ProcessStartInfo(
                Path.GetDirectoryName(
                Assembly.GetExecutingAssembly().Location) + "\\MyApp.exe");

                psi.WorkingDirectory = Path.GetDirectoryName(
                Assembly.GetExecutingAssembly().Location);
                psi.Verb = "runas";

                Process p = new Process();
                p.StartInfo = psi;

                p.Start();            
            }        
.
.
.        }

问题:MyApp.exe 正在创建 http 请求以从服务器获取一些数据。如果 MyApp.exe 从此处启动,我每次都会收到 超时异常。如果我单独运行 MyApp.exe,它会成功创建 http 请求而不会超时。下面是http请求的代码:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.Timeout = TimeOut;                    
request.Credentials = CredentialCache.DefaultCredentials;
request.Proxy = WebRequest.DefaultWebProxy;
request.UseDefaultCredentials = true;
request.AllowAutoRedirect = true;
request.KeepAlive = false;
request.Method = "HEAD";
request.SendChunked = true;                    

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
returnValue = response.StatusCode;
}

为什么会出现超时异常?我哪里做错了?

【问题讨论】:

    标签: c# installation httpwebrequest timeout launch-condition


    【解决方案1】:

    当安装程序运行时,它会以特殊权限/权限运行...

    也许您可以记录成功运行时request.Credentials = CredentialCache.DefaultCredentials;request.Proxy = WebRequest.DefaultWebProxy; 的值与超时情况的对比

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多