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,969 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

730 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