Solved

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

Posted on 2008-10-30
1
2,943 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:edenmachine
1 Comment
 
LVL 1

Accepted Solution

by:
edenmachine earned 0 total points
ID: 22844209
Ah - I had to allow read perms to LocalMachineName\Users to the file/directory and that fixed it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now