Receive HTTPS Post

A

Amoril

I am currently developing an app that will post an XML file (1 to
100MB+ in size) to an outside vendor application via HTTPS Post (doing
this in a vb.net windows app). The vendor will then at some later date
post an updated return XML file to my webserver (asp.net). The primary
problem I'm having is getting the posted stream to load in an
XMLDocument object. When trying to read the posted stream into the
XMLDocument the following error is kicked back from my webserver (seen
by the vendor), "The remote server returned an error: (500) Internal
Server Error." At minimum I want to make sure that the XML they're
posting to me is well-formed so I don't need to load it into an
XMLDocument object if there's another way to do it.


Also they're expecting to post their info to me via HTTPS, how can I
set up my page so it's looking for credentials to be passed along with
the XML stream? I see how to send credentials to them using
System.Net.NetworkCredential, but I don't see how accept credentials
from the vendor when they post to me.


Code used to accept HTTP post from Vendor:


Dim str As Stream
Dim strmContent As String
Dim strLen As Integer
Dim strRead As Integer


'Request input stream from the poster
str = Request.InputStream


'Get the length of incoming string
strLen = str.Length


'make a Byte array the size of the stream
Dim strArr(strLen) As Byte


'read the stream into the array
strRead = str.Read(strArr, 0, strLen)


'the stream will be ASCII encoded
Dim ascii As ASCIIEncoding = New ASCIIEncoding


'Get ASCII into reg. string here
strmContent = ascii.GetString(strArr)


Dim doc As XmlDocument = New XmlDocument
doc.LoadXml(strmContent)
doc.Save("D:\temp\test5.xml")


'Return Response to poster.
Response.Write("some response")


Any suggestions on what I can do to fix this or possible resources that
might help me out?


Thanks!
 
B

bruce barker

you need a little more research. often when a site post an xml, they are
doing a form post and the xml is stored in a field value. sometimes the xml
is a file attachement (file post), or the body is xml.

check what the content header is, and dump the body to see what they are
sending. you will proably need to send in the same format.

-- bruce (sqlwork.com)


| I am currently developing an app that will post an XML file (1 to
| 100MB+ in size) to an outside vendor application via HTTPS Post (doing
| this in a vb.net windows app). The vendor will then at some later date
| post an updated return XML file to my webserver (asp.net). The primary
| problem I'm having is getting the posted stream to load in an
| XMLDocument object. When trying to read the posted stream into the
| XMLDocument the following error is kicked back from my webserver (seen
| by the vendor), "The remote server returned an error: (500) Internal
| Server Error." At minimum I want to make sure that the XML they're
| posting to me is well-formed so I don't need to load it into an
| XMLDocument object if there's another way to do it.
|
|
| Also they're expecting to post their info to me via HTTPS, how can I
| set up my page so it's looking for credentials to be passed along with
| the XML stream? I see how to send credentials to them using
| System.Net.NetworkCredential, but I don't see how accept credentials
| from the vendor when they post to me.
|
|
| Code used to accept HTTP post from Vendor:
|
|
| Dim str As Stream
| Dim strmContent As String
| Dim strLen As Integer
| Dim strRead As Integer
|
|
| 'Request input stream from the poster
| str = Request.InputStream
|
|
| 'Get the length of incoming string
| strLen = str.Length
|
|
| 'make a Byte array the size of the stream
| Dim strArr(strLen) As Byte
|
|
| 'read the stream into the array
| strRead = str.Read(strArr, 0, strLen)
|
|
| 'the stream will be ASCII encoded
| Dim ascii As ASCIIEncoding = New ASCIIEncoding
|
|
| 'Get ASCII into reg. string here
| strmContent = ascii.GetString(strArr)
|
|
| Dim doc As XmlDocument = New XmlDocument
| doc.LoadXml(strmContent)
| doc.Save("D:\temp\test5.xml")
|
|
| 'Return Response to poster.
| Response.Write("some response")
|
|
| Any suggestions on what I can do to fix this or possible resources that
| might help me out?
|
|
| Thanks!
|
 
A

Amoril

When doing my research here on google and other dev sites, this was the
only method I could find on how to do it. Is there some more specific
information you can give me such as examples or perhaps a link to where
I can read more on your suggestions?

The code attached below (built for testing the above) will post an XML
stream to a webserver running the code I entered above and will display
the remote servers response, which was the only way I could find
described by other people. My question is specifically how do I get
this code to work for HTTPS posts, since I'm sure you need to send
credientals if you're posting to a server with https (I need to know
how to do it for when I post to other servers, and other people post
back to my servers). If there are better ways of doing this please let
me know where/what I need to look at so I can improve this. Thank you!

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim sr As StreamReader = New StreamReader("c:\test.xml")
Dim XMLRead As String = sr.ReadToEnd
Dim stXML As String
stXML = XMLRead
sr.Close()

Try

Dim results As String

Dim request As WebRequest =
WebRequest.Create("http://Localhost/GetPost.aspx")

request.Method = "POST"
request.ContentType = "text/xml; charset=utf-8" 'look for
other content types to see if this is appropriate. Check for headers as
well
results = WriteToURL(request, stXML)

Dim Response As String

Response = RetrieveFromURL(request)

MessageBox.Show(Response, "", MessageBoxButtons.OK)

Catch ex As Exception
MessageBox.Show(ex.Message, "", MessageBoxButtons.OK)
Exit Sub
End Try

End Sub

Private Function WriteToURL(ByVal Request As WebRequest, ByVal data
As String) As String

Try
Dim bytes = System.Text.Encoding.ASCII.GetBytes(data)
Request.ContentLength = bytes.Length

Dim OutPutStream As Stream = Request.GetRequestStream
OutPutStream.Write(bytes, 0, bytes.length)
OutPutStream.Close()
Catch ex As Exception
MessageBox.Show(ex.Message & " - Write To URL", "error",
MessageBoxButtons.OK)
Exit Function
End Try

End Function

Private Function RetrieveFromURL(ByVal Request As WebRequest) As
String

Try
Dim response As WebResponse = Request.GetResponse
Dim stream As Stream = response.GetResponseStream
Dim sr As StreamReader = New StreamReader(stream)
Return sr.ReadToEnd
Catch ex As Exception
MessageBox.Show(ex.Message & " - Retrieve From URL", "",
MessageBoxButtons.OK)
Exit Function
End Try

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top