This a very efficient (plus maybe the only) way to use .NET to post (or send) an XML request file to a URL. I have to pass some credit to
Andre Torrez because he built
XML Pad where I found what I was missing (manipulating the webRequest's RequestStream()).
''' -----------------------------------------------------------------------------
''' <summary>
''' A way to post files to other servers and receive responses.
''' This also includes utilizing a proxy server.
''' If no URL is sent, it calls GetURL() to get the posting URL.
''' </summary>
''' <param name="strURL">URL to access.</param>
''' <param name="fileToSend">Relative path to file to POST to the server. Does not have to be an XML file.</param>
''' <param name="postingContentType">MIME Type</param>
''' <param name="encoding">Type of System.Text.Encoding. Use UTF8 encoding for XML files.</param>
''' <returns>System.IO.Stream of the response from the remote server.</returns>
''' <example>
''' This is the code you will have to set prior to calling this function.
''' <code>
''' Private Const DOMAIN As String = ""
''' Private Const USERNAME As String = ""
''' Private Const PSSWRD As String = ""
''' Private Const PROXY_ADDRESS As String = ""
''' Private Const PROXY_PORT As Integer = 80
''' </code>
''' This sample shows how to call the method and POST a file to the remote server:
''' <code>
''' Dim strm As System.IO.Stream = GetResponseStream(url, "xml/getMeetings.xml","text/xml", New System.Text.UTF8Encoding)
''' </code>
''' This sample shows how to call the method without posting:
''' <code>
''' Dim strm As System.IO.Stream = GetResponseStream(strURL, Nothing, Nothing, Nothing)
''' </code>
''' Once you have the stream, this is how you load it into XML:
''' <code>
''' Dim xml As System.Xml.XmlDocument = New System.Xml.XmlDocument
''' xml.Load(strm)
''' </code>
''' </example>
''' <remarks>
''' </remarks>
''' <history>
''' [robz] 2/25/2005 Created
''' </history>
''' -----------------------------------------------------------------------------
Private Function GetResponseStream(ByVal strURL As String, ByVal fileToSend As String, ByVal postingContentType As String, ByVal encoding As System.Text.Encoding) As System.IO.Stream
If IsNothing(strURL) Then
strURL = GetURL()
End If
' set up the proxy and credentials that you will need to get out of the network -optional
Dim proxy As New System.Net.WebProxy(PROXY_ADDRESS, PROXY_PORT)
Dim creds As New Net.NetworkCredential(USERNAME, PSSWRD, DOMAIN)
' create a new webRequest to send off for information.
Dim wR As System.Net.WebRequest = System.Net.WebRequest.Create(strURL)
' apply the credentials - optional
proxy.Credentials = creds
wR.Credentials = creds
' set the timeout property in milliseconds
wR.Timeout = 10000
' give the webRequest the Proxy to use - optional
If (System.Configuration.ConfigurationSettings.AppSettings("useProxy").ToString() = "true") Then
wR.Proxy = proxy
End If
' if passed xmlfile, then post it in the web request
If Not (IsNothing(fileToSend)) Then
' set request stream to the xml file content
Dim strR As System.IO.StreamReader = New System.IO.StreamReader(_programLocation & fileToSend, encoding)
' read it into a byte array to use in the post
Dim postBuffer As Byte()
Dim encode As System.Text.Encoding = strR.CurrentEncoding()
postBuffer = encode.GetBytes(strR.ReadToEnd())
encode = Nothing
strR.Close()
' set up the webRequest to POST an xml file
wR.Method = "POST"
wR.ContentType = postingContentType
wR.ContentLength = postBuffer.Length
' set the stream to the webRequest's request stream
Dim postStream As System.IO.Stream = wR.GetRequestStream()
postStream.Write(postBuffer, 0, postBuffer.Length)
postStream.Close()
End If
' pass the response from webRequest to a webResponse.
Dim wResponse As System.Net.WebResponse = wR.GetResponse()
wR = Nothing
Return wResponse.GetResponseStream()
End Function
By the way Andre, that one part was pretty fricken important. =)
Update: Added the ability to use any kind of file, such as a ZIP file.