Link to home
Start Free TrialLog in
Avatar of edenmachine
edenmachine

asked on

I get this error: The remote server returned an error: (401) Unauthorized. when posting to ASHX file with XML over HTTP

I get this error: The remote server returned an error: (401) Unauthorized. when posting to ASHX file with XML over HTTP.

The purpose of this code is to be able to post XML over HTTP (I have to do this because this is how a product I am using communicates) to an Generic Handler (ASHX file) in ASP.NET/VB.NET but I am getting this error: The remote server returned an error: (401) Unauthorized.

I have attached the code for review.  The site is set up properly in IIS and I can browse other ASP.NET pages just fine in the site and I can also just browse the ASHX page directly and it displays "Hello World" as expected.  But when I try to post to the ASHX page from the posting ASPX page, I get that error.
ASPX PAGE:
 
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Response.Write(PostXml("http://vm01/sign/testxmloverhttp/listener.ashx", My.Computer.FileSystem.ReadAllText(Server.MapPath("XMLFile.xml"), Encoding.UTF8)))
    End Sub
    
    Public Function PostXml(ByVal url As String, ByVal xml As String) As String
        Dim returnValue As String = ""
        Try
            Dim bytes As Byte() = Encoding.UTF8.GetBytes(xml)
            Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
            request.Method = "POST"
            request.ContentLength = bytes.Length
            request.ContentType = "text/xml"
            Using requestStream As Stream = request.GetRequestStream()
                requestStream.Write(bytes, 0, bytes.Length)
            End Using
 
            Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
            If response.StatusCode <> HttpStatusCode.OK Then
                Dim message As String = [String].Format("POST failed. Received HTTP {0}", response.StatusCode)
                Throw New ApplicationException(message)
            End If
            Dim rs As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
            Dim receiveStream As Stream = rs.GetResponseStream()
            Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")
            Dim readStream As New StreamReader(receiveStream, encode)
            Dim read(256) As [Char]
            Dim count As Integer = readStream.Read(read, 0, 256)
            While count > 0
                Dim str As New [String](read, 0, count)
                returnValue += str
                count = readStream.Read(read, 0, 256)
            End While
            readStream.Close()
            rs.Close()
        Catch ex As Exception
            Return ex.Message
        End Try
        Return returnValue
    End Function
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>
 
ASHX PAGE:
 
<%@ WebHandler Language="VB" Class="Listener" %>
 
Imports System
Imports System.Web
 
Public Class Listener : Implements IHttpHandler
    
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Response.ContentType = "text/HTML"
        context.Response.Write("Hello World")
    End Sub
 
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
 
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of edenmachine
edenmachine

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial