Share via


How to HTTP POST a document to a BizTalk Receive Location

I've been looking for this sample code on my hard drive again today.

It will be easier to find it on my blog J

Here it is:

        /// <summary>

        /// Sends a document via Http POST

        /// </summary>

        /// <param name="url">URL to send to</param>

        /// <param name="body">content to send</param>

        public static string SendViaHttpPost(string url, string body)

        {

            StringBuilder response = new StringBuilder();

            WebResponse result = null;

 

            try

            {

                WebRequest req = WebRequest.Create(url);

                req.Method = "POST";

                req.ContentType = "text/xml; charset=utf-8";

                req.Credentials = System.Net.CredentialCache.DefaultCredentials; // use current user credentials

                byte[] someBytes = null;

 

                someBytes = Encoding.UTF8.GetBytes(body);

                

                req.ContentLength = someBytes.Length;

                Stream newStream = req.GetRequestStream();

                newStream.Write(someBytes, 0, someBytes.Length);

                newStream.Close();

 

                result = req.GetResponse();

                Stream ReceiveStream = result.GetResponseStream();

                Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

                StreamReader sr = new StreamReader( ReceiveStream, encode );

                Char[] read = new Char[256];

                int count = sr.Read( read, 0, 256 );

                while (count > 0)

                {

                    String str = new String(read, 0, count);

                    response.Append(str);

                    count = sr.Read(read, 0, 256);

                }

            }

            finally

            {

                if ( result != null )

                {

                    result.Close();

                }

            }

            return response.ToString();

        }