Vb.net http处理程序应该如何填充调用xmlhttp对象';s responsetext属性?
我正在尝试使用asp.net实现一个http句柄(.ashx),用于客户端将使用serverxmlhttp从处理程序请求信息的环境。这是到目前为止的代码 客户端.ASPXVb.net http处理程序应该如何填充调用xmlhttp对象';s responsetext属性?,vb.net,httphandler,xmlhttprequest,serverxmlhttp,Vb.net,Httphandler,Xmlhttprequest,Serverxmlhttp,我正在尝试使用asp.net实现一个http句柄(.ashx),用于客户端将使用serverxmlhttp从处理程序请求信息的环境。这是到目前为止的代码 客户端.ASPX <%@ Page Language="VB" %> <% On Error Resume Next Dim myserver_url As String = "http://mydomain.com/Server.ashx" Dim myparameters As String = "
<%@ Page Language="VB" %>
<%
On Error Resume Next
Dim myserver_url As String = "http://mydomain.com/Server.ashx"
Dim myparameters As String = "one=1&two=2"
Dim xmlhttp As Object
xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP.4.0")
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
xmlhttp.open("POST", myserver_url, False)
xmlhttp.Send(myparameters)
If xmlhttp.Status = 200 Then
Dim myresults As String = ""
myresults = xmlhttp.responsetext
Response.Clear()
Response.Write("<html><body><h1>" & myresults & "</h1></body></html>")
End If
xmlhttp = Nothing
%>
<%@ WebHandler Language="VB" Class="MyServerClass" %>
Imports System
Imports System.Web
Public Class MyServerClass : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "text/plain"
context.Response.Write("hi there")
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
<%@ Page Language="VB" %>
<%
Response.ContentType = "text/plain"
Response.Write("hi there")
%>
SERVER.ASHX
<%@ Page Language="VB" %>
<%
On Error Resume Next
Dim myserver_url As String = "http://mydomain.com/Server.ashx"
Dim myparameters As String = "one=1&two=2"
Dim xmlhttp As Object
xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP.4.0")
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
xmlhttp.open("POST", myserver_url, False)
xmlhttp.Send(myparameters)
If xmlhttp.Status = 200 Then
Dim myresults As String = ""
myresults = xmlhttp.responsetext
Response.Clear()
Response.Write("<html><body><h1>" & myresults & "</h1></body></html>")
End If
xmlhttp = Nothing
%>
<%@ WebHandler Language="VB" Class="MyServerClass" %>
Imports System
Imports System.Web
Public Class MyServerClass : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "text/plain"
context.Response.Write("hi there")
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
<%@ Page Language="VB" %>
<%
Response.ContentType = "text/plain"
Response.Write("hi there")
%>
导入系统
导入系统.Web
公共类MyServerClass:实现IHttpHandler
Public Sub-ProcessRequest(ByVal上下文作为HttpContext)实现IHttpHandler.ProcessRequest
context.Response.ContentType=“text/plain”
context.Response.Write(“你好”)
端接头
公共只读属性IsReusable()作为布尔值实现IHttpHandler.IsReusable
得到
返回错误
结束
端属性
末级
…我的问题是客户端代码中的myresults字符串始终为空。
问题:http句柄应该如何填充调用它的xmlhttp对象的responsetext属性
附录:我还将server.ashx实现为一个aspx文件,但我的结果仍然是空的。这是代码
SERVER.ASPX
<%@ Page Language="VB" %>
<%
On Error Resume Next
Dim myserver_url As String = "http://mydomain.com/Server.ashx"
Dim myparameters As String = "one=1&two=2"
Dim xmlhttp As Object
xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP.4.0")
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
xmlhttp.open("POST", myserver_url, False)
xmlhttp.Send(myparameters)
If xmlhttp.Status = 200 Then
Dim myresults As String = ""
myresults = xmlhttp.responsetext
Response.Clear()
Response.Write("<html><body><h1>" & myresults & "</h1></body></html>")
End If
xmlhttp = Nothing
%>
<%@ WebHandler Language="VB" Class="MyServerClass" %>
Imports System
Imports System.Web
Public Class MyServerClass : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "text/plain"
context.Response.Write("hi there")
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
<%@ Page Language="VB" %>
<%
Response.ContentType = "text/plain"
Response.Write("hi there")
%>
提前感谢您的帮助!
和平,
Henry E.Taylor您的CLIENT.ASPX文件有一些问题。据我所知,您正在使用服务器端代码来实例化ActiveX控件,该控件允许您向server.ASHX发出HTTP请求并读取响应流,而响应流又被写入CLIENT.ASPX页面的响应流。事实上,您使用的是ActiveX控件而不是标准的.NET,这让我觉得您正在将旧的ASP站点迁移到.NET。在这种情况下,要做的第一件事是使用AspCompat=true指令标记页面:
<%@ Page Language="VB" AspCompat="true" %>
另一件需要提及的事情是,您使用了错误的ActiveX名称MSXML2.ServerXMLHTTP.4.0,而不是MSXML2.ServerXMLHTTP。此外,您正在尝试在调用方法之前使用该方法设置请求头。您在错误恢复下一条语句中编写了错误恢复下一条语句,这一事实使您无法看到所有这些错误。代码刚刚通过,您的SERVER.ASHX处理程序从未实际执行,因此您得到了一个空响应。以下是您的CLIENT.ASPX代码的更正版本:
<%@ Page Language="VB" AspCompat="true" %>
<%
Dim myserver_url As String = "http://mydomain.com/Server.ashx"
Dim myparameters As String = "one=1&two=2"
Dim xmlhttp As Object
xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open("POST", myserver_url, False)
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
xmlhttp.Send()
If xmlhttp.Status = 200 Then
Dim myresults As String = ""
myresults = xmlhttp.responseText
Response.Clear()
Response.Write("<html><body><h1>" & myresults & "</h1></body></html>")
End If
xmlhttp = Nothing
%>
当然,实现这一点的首选方法是使用客户端脚本语言(如javascript),或者如果您想在服务器端执行此操作,则使用标准的.NET类而不是ActiveX控件