我如何才能发现经典 ASP 页面不响应 HttpWebRequest 的原因?
How can I discover why a Classic ASP page isn't responding to an HttpWebRequest?
Prefix: Unfortunately, rewriting these Classic ASP pages into ASP.NET is not an option. This is a complex legacy application that we're maintaining.
我在将 HttpWebRequest
到 POST 到同一 Web 应用程序中的经典 ASP 页面时遇到问题。该页面在浏览器中快速正确地运行。
预期结果:
HttpWebRequest
应该 POST 到页面并得到 HTML 结果。
实际结果:
HttpWebRequest
等待默认的 100 秒超时,然后失败。
错误信息:
System.Net.WebException
HResult=0x80131509
Message=The operation has timed out
我尝试过的事情:
- 添加了
CookieContainer
,按照建议 here
- 按照建议 here
将 HttpWebResponse
包裹在 Using
块中
- 尝试使用 Telerik Fiddler 检查 HTTP 流量(没有记录请求的流量)
为了排除 ASP 页面本身的罪魁祸首,将其内容减少到最低限度:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Page</title>
</head>
<body>
<p>This is the page.</p>
</body>
</html>
代码如下:(第 32 行失败,GetResponse()
)
---------------------------------------------------
Generic Handler: SignIn.ashx
---------------------------------------------------
1 Imports System.Net
2
3 Public Class Labels
4 Implements IHttpHandler
5
6 Sub ProcessRequest(Context As HttpContext) Implements IHttpHandler.ProcessRequest
7 Dim oFormData As NameValueCollection
8 Dim oResponse As HttpWebResponse
9 Dim sResponse As String
10 Dim aPostData As Byte()
11 Dim oRequest As HttpWebRequest
12 Dim sUrl As String
13
14 sUrl = "http://domain.local/signin.asp"
15
16 oFormData = HttpUtility.ParseQueryString(String.Empty)
17 oFormData.Add("username", "username")
18 oFormData.Add("password", "password")
19
20 aPostData = Encoding.ASCII.GetBytes(oFormData.ToString)
21
22 oRequest = WebRequest.Create(sUrl)
23 oRequest.AllowAutoRedirect = False
24 oRequest.ContentLength = aPostData.Length
25 oRequest.ContentType = "application/x-www-form-urlencoded"
26 oRequest.Method = WebRequestMethods.Http.Post
27
28 Using oStream = oRequest.GetRequestStream
29 oStream.Write(aPostData, 0, aPostData.Length)
30 End Using
31
32 oResponse = oRequest.GetResponse
33
34 Using oReader As New StreamReader(oResponse.GetResponseStream)
35 sResponse = oReader.ReadToEnd
36 End Using
37 End Sub
38
39 ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
40 Get
41 Return False
42 End Get
43 End Property
44 End Class
我如何调试它并确定发生了什么? (请注意,静态 HTML 页面以及 ASPX 和 ASHX 文件加载得很好。这仅发生在经典 ASP 页面上。)
这是一个古怪的人。但在下次重启后它就消失了。
如果您在响应页面中调用同一个会话,您会发现 ASP 是单线程的,如果尝试使用两个单独的线程将会挂起。
不要在响应页面中调用会话,这可能会解决问题。
Prefix: Unfortunately, rewriting these Classic ASP pages into ASP.NET is not an option. This is a complex legacy application that we're maintaining.
我在将 HttpWebRequest
到 POST 到同一 Web 应用程序中的经典 ASP 页面时遇到问题。该页面在浏览器中快速正确地运行。
预期结果:
HttpWebRequest
应该 POST 到页面并得到 HTML 结果。
实际结果:
HttpWebRequest
等待默认的 100 秒超时,然后失败。
错误信息:
System.Net.WebException
HResult=0x80131509
Message=The operation has timed out
我尝试过的事情:
- 添加了
CookieContainer
,按照建议 here - 按照建议 here 将
- 尝试使用 Telerik Fiddler 检查 HTTP 流量(没有记录请求的流量)
为了排除 ASP 页面本身的罪魁祸首,将其内容减少到最低限度:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Page</title> </head> <body> <p>This is the page.</p> </body> </html>
HttpWebResponse
包裹在 Using
块中
代码如下:(第 32 行失败,GetResponse()
)
---------------------------------------------------
Generic Handler: SignIn.ashx
---------------------------------------------------
1 Imports System.Net
2
3 Public Class Labels
4 Implements IHttpHandler
5
6 Sub ProcessRequest(Context As HttpContext) Implements IHttpHandler.ProcessRequest
7 Dim oFormData As NameValueCollection
8 Dim oResponse As HttpWebResponse
9 Dim sResponse As String
10 Dim aPostData As Byte()
11 Dim oRequest As HttpWebRequest
12 Dim sUrl As String
13
14 sUrl = "http://domain.local/signin.asp"
15
16 oFormData = HttpUtility.ParseQueryString(String.Empty)
17 oFormData.Add("username", "username")
18 oFormData.Add("password", "password")
19
20 aPostData = Encoding.ASCII.GetBytes(oFormData.ToString)
21
22 oRequest = WebRequest.Create(sUrl)
23 oRequest.AllowAutoRedirect = False
24 oRequest.ContentLength = aPostData.Length
25 oRequest.ContentType = "application/x-www-form-urlencoded"
26 oRequest.Method = WebRequestMethods.Http.Post
27
28 Using oStream = oRequest.GetRequestStream
29 oStream.Write(aPostData, 0, aPostData.Length)
30 End Using
31
32 oResponse = oRequest.GetResponse
33
34 Using oReader As New StreamReader(oResponse.GetResponseStream)
35 sResponse = oReader.ReadToEnd
36 End Using
37 End Sub
38
39 ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
40 Get
41 Return False
42 End Get
43 End Property
44 End Class
我如何调试它并确定发生了什么? (请注意,静态 HTML 页面以及 ASPX 和 ASHX 文件加载得很好。这仅发生在经典 ASP 页面上。)
这是一个古怪的人。但在下次重启后它就消失了。
如果您在响应页面中调用同一个会话,您会发现 ASP 是单线程的,如果尝试使用两个单独的线程将会挂起。
不要在响应页面中调用会话,这可能会解决问题。