views:

1113

answers:

1
Hi Everyone,

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

+1  A: 

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.

Darin Dimitrov