RobLog

Web Design in the World of .NET (C# and VB.NET, XML, and Javascript). I learned how to program from TheDailyWTF.com!
posts - 140, comments - 127, trackbacks - 5

My Links

News

Main Site Cert Corner Goals About Me

Article Categories

Archives

Post Categories

Image Galleries

.NET

Personal

WOW


Using .NET to POST (or send) an XML request file to a URL without XMLHTTP (VB.NET)

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.

posted on Friday, February 25, 2005 1:24 PM

Feedback

# re: Using .NET to POST (or send) an XML request file to a URL without XMLHTTP (VB.NET)

I also forgot, but I have to credit this guy as well: http://p2p.wrox.com/topic.asp?TOPIC_ID=23831
2/25/2005 1:39 PM | Robz

# re: Using .NET to POST (or send) an XML request file to a URL without XMLHTTP (VB.NET)

We offer the largest collection of polyphonic ringtones, monophonic ringtones, mobile videos, color wallpapers, color screensavers, real sounds.
12/8/2007 3:44 PM | free ringtones

# re: Using .NET to POST (or send) an XML request file to a URL without XMLHTTP (VB.NET)

If you are looking for the replica watch and information about it, you came to the right place.
12/21/2007 10:41 AM | Replica Watch

# Very helpful

Very helpful
5/11/2008 8:53 AM | nike air force ones

# re: Using .NET to POST (or send) an XML request file to a URL without XMLHTTP (VB.NET)

Overall Petit Roid 3 is a hilarious hentai manga that has so many merits, which separate it from the average hentai manga; such as its length and the fact that it follows one story throughout. It manages to contain plenty of fetishes (loli, bukkake, S&M, etc.) to satisfy any guy who reads this and what’s more amazing is how it can
http://basker.info/index.php?sm=3
http://kriomotors.info/index.php?sm=6
http://nikeron.info/index.php?sm=9
give you boner and make you laugh at the same time. But nonetheless it still is a hentai manga and there’s only so much that it can accomplish, still it is definitely one any perv should check out.
5/28/2008 2:02 AM | Aron

# re: Using .NET to POST (or send) an XML request file to a URL without XMLHTTP (VB.NET)

http://www.sneakersupplier.com buy air jordan shoes online!!!
8/25/2009 10:20 AM | air jordans

Post Comment

Title  
Name  
Url
Comment   
Protected by Clearscreen.SharpHIPEnter the code you see: