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,992 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

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

717 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