HttpWebRequest.GetResponse 服务器上的计划任务 运行 超时问题

HttpWebRequest.GetResponse Timeout issue when running from scheduled task on a server

目前我有一个 .NET 应用程序使用他们的 API 登录到 SalesForce。从这里我查询一个 object 来提取报告信息并构建一个 URL 来导出一个文件。代码是这样的。

Dim request As HttpWebRequest
Dim response As HttpWebResponse
Dim stream as Stream
request = CType(WebRequest.Create(URL), HttpWebRequest)
request.Method = "GET"
request.Headers.Add("Cookie", "sid=" & header.sessionId)
response = CType(request.GetResponse(), HttpWebResponse)
stream = response.GetResponseStream()

我的 header.sessionId 上面是 SalesForce Session Header 它捕获了我的 session 成功登录。

此应用程序在本地测试或 运行 在生产服务器上测试时成功运行(在 Visual Studio 2010 年)。我已将其设置为 运行 作为导致问题的计划任务。该任务设置为 运行 作为服务器上的管理员帐户,如果手动启动(右键单击任务 -> 运行)或在 运行 时间内保持与服务器的连接,则该任务可以正常工作.如果我与服务器和任务 运行s 断开连接,我每次都会收到 "System.Net.WebException: The operation has timed out at System.Net.HttpWebRequest.GetResponse()" 错误。然后手动登录并启动应用程序。

如前所述,这已设置为 运行 作为管理员帐户,我已授予该帐户完全的安全控制权。我没有检查 "run only if logged in",也没有在空闲时将其设置为仅 运行。

据我所知,网络请求的默认超时是 100,000 毫秒或 100 秒。该任务在达到该限制之前就超时了。在显示的代码之前,我检查以确认我的 session 仍然有效,如果无效则更新我的 session。此超时总是发生在尝试下载第一个报告时,但是当手动 运行ning 时,我能够在 2 分钟内下载大约 25 份报告。

有什么想法吗?

这不是 "the answer" 但我创建了一个相当粗糙的 HttpWebRequest 测试应用程序并将其设置为 运行 作为另一个未登录 Windows 的用户] 7(抱歉,我无法访问服务器 atm)。我尝试过的每一种测试方式都完美无缺。可能是因为 Windows 7 做事的方式不同于(您想让它在哪个操作系统上运行?),或者实际上是您的代码中的某些东西导致了这个问题。由于这段代码对我有用,我建议您尝试一下,至少消除您的代码问题。

创建一个新的 vb.net 应用程序并确保删除所有表单并创建一个模块 (Module1) 并在其中添加此代码:

Imports System.Net
Imports System.IO

Module Module1
    Const sUserAgent As String = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0"
    Const sMainURL As String = "http://www.whosebug.com/"
    Private gsLogFile As String = System.IO.Path.Combine(Application.StartupPath, "logfile.txt")

    Sub Main()
        StartScrape()
    End Sub

    Private Sub StartScrape()
        Try
            GetMethod(sMainURL)
            SaveTextToFile("Finished", gsLogFile)
        Catch ex As Exception
            SaveTextToFile("Error: " & ex.Message, gsLogFile)
        End Try
    End Sub

    Private Function GetMethod(ByVal sPage As String) As Boolean
        Dim req As HttpWebRequest
        Dim resp As HttpWebResponse
        Dim stw As StreamReader
        Dim bReturn As Boolean = True

        Try
            req = HttpWebRequest.Create(sPage)
            req.Method = "GET"
            req.AllowAutoRedirect = False
            req.UserAgent = sUserAgent
            req.Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
            req.Headers.Add("Accept-Language", "en-us,en;q=0.5")
            req.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7")
            req.Headers.Add("Keep-Alive", "300")

            resp = req.GetResponse        ' Get the response from the server 

            If req.HaveResponse Then
                resp = req.GetResponse        ' Get the response from the server 
                stw = New StreamReader(resp.GetResponseStream)
                stw.ReadToEnd()    ' Read the response from the server, but we do not save it
            Else
                SaveTextToFile("No response received from host " & sPage, gsLogFile)
                bReturn = False
            End If
        Catch exc As WebException
            SaveTextToFile("Network Error: " & exc.Message.ToString & " Status Code: " & exc.Status.ToString & " from " & sPage, gsLogFile)
            bReturn = False
        End Try

        Return bReturn
    End Function

    Public Function SaveTextToFile(ByVal strData As String, ByVal FullPath As String) As Boolean
        Dim bAns As Boolean = False
        Dim objReader As IO.StreamWriter

        Try
            objReader = New IO.StreamWriter(FullPath, System.IO.File.Exists(FullPath))
            objReader.Write(Format(Now, "dd-MMM-yyyy hh:mm:ss tt") & " - " & strData)
            objReader.Close()
            bAns = True
        Catch Ex As Exception

        End Try
        Return bAns
    End Function
End Module

日志文件logfile.txt 将在执行完成后创建在与 exe 相同的目录中。如果有错误,它将被记录到文件中,最后它会显示 "Finished" 和时间戳。我 运行 在任务调度程序中使用不同的选项使用几种不同的方式,每次我只有一个日志条目显示 "Finished"。如果你决定测试这个,请报告你的结果,就好像这个测试失败了我肯定会说你的计划任务有问题。

根据我之前的评论,似乎涉及一个代理,要求用户登录才能使请求成功。

您的回复:

We use an internet security web proxy that works via Single Sign On for users. Basically the tool pulls the users network credentials any time they try and access a site behind the scenes before either allowing or blocking the traffic. The issue was once the admin account was disconnected, this web proxy would attempt to gather credentials from a logged in user but would find nothing. The request would then be denied. This is why any user being logged in worked but it failed every other time.