edenmachine
asked on
XML over HTTP in VB.NET / ASP.NET
I have two separate applications that talk to each other using basic XML over HTTP (currently written in vbscript/ASP and COM+) I need to be able to add a middle process between the two apps written in ASP.NET/VB.NET that consumes the XML over HTTP from the first process, does some database interactivity and then sends the XML over HTTP on to the second process. I'm pretty educated on how to use the standard ASP.NET Web Services and I have code to "send" XML over HTTP in .NET but not to "consume" it.
My Questions are:
1. How can I consume XML over HTTP with .NET (I have code below that sends it successfully)
2. Can I just use standard ASP.NET Web Services (ASMX files) to do all this somehow?
My Questions are:
1. How can I consume XML over HTTP with .NET (I have code below that sends it successfully)
2. Can I just use standard ASP.NET Web Services (ASMX files) to do all this somehow?
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What exactly are you doing here? Why use the aspx page at all? The ashx will handle the request directly and not have the overhead of all the Page junk that you don't need here. Or is that page something else? As for the 401 error, you evidently have not granted appropriate access to the virtual directory in IIS so you can browse call the app. I'd have to know more about how you configured things to say how to fix it.
ASKER
I don't have any problem accessing the ASHX when I type in http://vm01/sign/testxmlov erhttp/lis tener.ashx directly into the browser so I'm not sure why there would be a permissions problem. The ASPX page is just to mock the posting of the XML that would be posted XML over HTTP from the First Process that the ASHX needs to "intercept" before continuing on to the Second Process.
ASKER
I can definitely agree with you that the ASHX was the solution to my original problem so I'm giving you the full points. I guess maybe I need to ask a separate question about my permissions issue. thanks!
You can continue here with the permissions if you want. How do you have IIS configured for that folder and where are you sending the tests requests from (both browser and program)?
ASKER
I figured it out - I had to grant "Users" perms to that file/directory and then it worked fine - thanks for your help!
ASKER
The remote server returned an error: (401) Unauthorized.
I've posted my code below but I've tried all kinds of things to get any kind of a response and I only get the above error.
Open in new window