http 处理程序应如何填充调用 xmlhttp 对象的 responsetext 属性?

发布于 2024-07-10 22:31:02 字数 1867 浏览 7 评论 0原文

我正在尝试使用 asp.net 为客户端将使用 serverxmlhttp 从处理程序请求信息的环境实现 http 句柄 (.ashx)。 这是到目前为止的代码...

CLIENT.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   
%>

SERVER.ASHX

<%@ 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

...我的问题是客户端代码中的 myresults 字符串始终为空。 问题:http 句柄应该如何填充调用它的 xmlhttp 对象的responsetext 属性?

附录:我也将 server.ashx 实现为 aspx 文件,但我的结果仍然是空白。 这是该代码。

SERVER.ASPX

<%@ Page Language="VB" %>

<%
    Response.ContentType = "text/plain"
    Response.Write("hi there")
%>

提前感谢您的帮助! 和平, 亨利·泰勒

I'm trying to implement a http handle (.ashx) using asp.net for an environment where the clients will be using serverxmlhttp to request information from the handler. Here is the code so far...

CLIENT.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   
%>

SERVER.ASHX

<%@ 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

...my problem is that the myresults string in the client code is always blank.
Question: How should the http-handle populate the responsetext property of the xmlhttp object which called it?

Addendum: I have also implemented the server.ashx as an aspx file, but myresults was still blank. Here is that code.

SERVER.ASPX

<%@ Page Language="VB" %>

<%
    Response.ContentType = "text/plain"
    Response.Write("hi there")
%>

Thanks in advance for the assistance!
Peace,
Henry E. Taylor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀念你的温柔 2024-07-17 22:31:02

您的 CLIENT.ASPX 文件存在一些问题。 据我所知,您正在使用服务器端代码来实例化 ActiveX 控件,该控件允许您向 SERVER.ASHX 发出 HTTP 请求并读取响应流,该响应流又写入 CLIENT.ASPX 页面的响应流。 事实上,您使用的是 ActiveX 控件而不是标准 .NET 让我认为您正在将旧的 ASP 站点迁移到 .NET。 在这种情况下,要做的第一件事是使用 AspCompat=true 指令标记您的页面:

<%@ Page Language="VB" AspCompat="true" %>

另一件事要提到的是您使用了错误的 ActiveX 名称 MSXML2.ServerXMLHTTP.4.0 em> 而不是 MSXML2.ServerXMLHTTP。 此外,您还尝试使用 setRequestHeader 方法,然后再调用 open 方法。 事实上,您编写了 On Error Resume Next 语句,因此您无法看到所有这些错误。 代码刚刚执行完毕,您的 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 控件。

There are a few things wrong with your CLIENT.ASPX file. From what I can see you are using server side code in order to instantiate an ActiveX control allowing you to make an HTTP request to SERVER.ASHX and read the response stream which in turn is written to the response stream of the CLIENT.ASPX page. The fact that you are using an ActiveX control instead of the standard .NET classes makes me think that you are migrating an old ASP site to .NET. In this case the first thing to do is to mark your pages with the AspCompat=true directive:

<%@ Page Language="VB" AspCompat="true" %>

Another thing to mention is that you are using the wrong ActiveX name MSXML2.ServerXMLHTTP.4.0 instead of MSXML2.ServerXMLHTTP. Also you are trying to set request headers using the setRequestHeader method before calling the open method. The fact that you wrote the On Error Resume Next statement prevented you from seeing all these errors. The code just went through and your SERVER.ASHX handler never actually executed, so you got an empty response. Here's a corrected version of your CLIENT.ASPX code:

<%@ 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   
%>

Of course the preferred way to achieve this is either using a client scripting language such as javascript or if you want to do it server side, then use the standard .NET classes instead of ActiveX controls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文