Home All Groups Group Topic Archive Search About

http read contents on source server-write contents to different se

Author
27 Feb 2007 8:30 PM
theWizard1
Need to pass the directory name (path) of where the file should be stored on
the target server.  Currently it is hardcoded.  This app uses webclient,
httpWebRequest and httpWebResponse. 
Both applications were written with vs.net 1.0
Create Console app shown below
Then create Webapp shown below after the console app contents
Create a directory C:\SecondPartAGet
    Place a file in the C:\SecondPartAGet - file name is mybogus.log (it can
contain some bogus text)
Create a directory C:\SecondPartBResult

Console application - SecondPartA
**************************************
using System;
using System.Net;
using System.IO;
using System.Text;
using SecondPartA;

namespace SecondPartA
{
    /// <summary>
    /// Summary description for SecondPartA.
    /// </summary>
    class SecondPartA
    {

        /// <summary>
        /// Default value to use as the HTML User Agent
        /// </summary>
        protected const string userAgent = "My HTTP(S) Message Handler";

        /// <summary>
        /// Indicates web request is a post
        /// </summary>
        protected const string requestMethod = "POST";
        /// <summary>
        /// Specify URL encoding
        /// </summary>
        protected const string contentType = "application/x-www-form-urlencoded";

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            //
            // TODO: Add code to start application here
            //

            // Create the request instance.
            string myURL = "http://localhost/SecondPartB/SecondPartB.aspx";
            //
            WebRequest wReq = WebRequest.Create(myURL);

            // Set the HTTP-specific UserAgent property
            //
            if (wReq is HttpWebRequest)
            {
                ((HttpWebRequest)wReq).UserAgent = userAgent;

                // Turn off connection keep-alives.
                //
                ((HttpWebRequest)wReq).KeepAlive = false;
            }

            // Indicate to ASP that message is a POST
            //
            wReq.Method = requestMethod;

            // Hard code a 3-minute timeout
            wReq.Timeout = 180000;

            // Set content type to url encoded
            //
            wReq.ContentType = contentType;

            // Get the request stream to post the script
            //
            Stream reqStream = wReq.GetRequestStream();

            //new
            string myFileString = string.Empty;
            string completeFileName = @"C:\SecondPartAGet\mybogus.log";
            StreamReader sr = new StreamReader(completeFileName);
            // Read file into a string
            myFileString = sr.ReadToEnd();
            sr.Close();
            //new

            // append "Request=" to the test messgae
            // Submit the message including the html header and trailer tags
            //

            reqStream.Write(Encoding.Default.GetBytes(myFileString), 0,
myFileString.Length);
            // Close the stream to indicate submission is complete
            //
            reqStream.Close();

            wReq.Timeout = Int32.MaxValue;
            // Get the response instance to read the reply
            //
            WebResponse wResp = null;
            try
            {
                wResp = wReq.GetResponse();

                // Get the response stream to read the reply
                //
                Stream respStream = wResp.GetResponseStream();

                // StreamReader to read the entire response
                //
                StreamReader reader = new StreamReader(respStream, Encoding.Default);

                // Read response.  Includes header information
                //
                string htmlReply = reader.ReadToEnd();

            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.Message.ToString());
            }
            finally
            {
                if (null != wResp)
                {
                    // Close the response and response stream.
                    //
                    wResp.Close();

                }
            }
        }
    }
}
**********************************************************************
WebAppliation - SecondPartB

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;

namespace SecondPartB
{
    /// <summary>
    /// Summary description for SecondPartB.
    /// </summary>
    public class SecondPartB : System.Web.UI.Page
    {
        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here

            string messageIn = GetFileFromRequest();

        }

        /// <summary>
        /// Read Request object InputStream and return input message
        /// </summary>
        /// <returns>String containing user input</returns>
        private string GetFileFromRequest()
        {
            string result = string.Empty;
            try
            {
                // Get the request stream
                //
                Stream reqStream = Request.InputStream;

                // StreamReader to read the request stream
                //
                StreamReader reader = new StreamReader(reqStream, Encoding.Default);

                // Read stream.  Includes header information
                //
                result = reader.ReadToEnd();

                // Close the stream
                //
                reqStream.Close();

                //new
                string completeFileName = @"C:\SecondPartBResult\mybogusb.log";
                StreamWriter sw = File.CreateText(completeFileName);
                sw.Write(result);
                sw.Close();

            }
            catch (Exception Ex)
            {
            }
            return result;
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {   
            this.Load += new System.EventHandler(this.Page_Load);
        }
        #endregion
    }
}

AddThis Social Bookmark Button