Posting Form Data with c#

Letztes Jahr habe ich folgenden Blog-Eintrag verfasst. Darum ging es, wie man ein HTML-Form innerhalb von .NET simulieren kann.

Jetzt ist mir gerade aufgefallen, dass der Sourcecode der ausführenden Klasse dafür fehlt. Natürlich ist das der spannende und entscheidene Part für das Post der HTML-Form. Das möchte ich natürlich nicht so stehen lassen und hier kommt der fehlende Part!

using System;
using System.Net;
using System.Web;
using System.IO;
using System.Text ;

namespace GaliNeo.Framework
{
    public class RemotePost
    {
        #region Private Members
        private System.Collections.Specialized.NameValueCollection Inputs = null ;
        private string _sUrl = "" ;

        #endregion
        
        public RemotePost()
        {
            //
            // TODO: Fügen Sie hier die Konstruktorlogik hinzu
            //
            Inputs = new System.Collections.Specialized.NameValueCollection() ;
        }

        #region Public Methods

        public void Add(string sName, double dValue)
        {
            this.Add(sName, dValue.ToString()) ;
        }

        public void Add(string sName,int iValue)
        {
            this.Add(sName, iValue.ToString() ) ;
        }
        
        public void Add(string sName,string sValue)
        {
            Inputs.Add(sName,sValue) ;
        }

        public string Url
        {
            get { return _sUrl ; }
            set { _sUrl = value ; }
        }
        
        public string Send()
        {
            string sPostData = "" ;

            Uri sUri = new Uri(this.Url) ;
        
            //Wird für evtl. SSL - Verbindungen benötigt
            System.Net.ServicePointManager.CertificatePolicy = new trustedCertificatePolicy();

            HttpWebRequest request = (HttpWebRequest) WebRequest.Create(sUri);

            //User-Agent
            request.UserAgent = "GaliNeo OnlineStore" ;

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            
            //Zusammensetzen der Daten für die Übermittlung
            sPostData = GetContentToPost() ;
            
            request.ContentLength = sPostData.Length ;            

            Stream writeStream = request.GetRequestStream();
            
            //Encoding im UTF-8 Format um kompatible zu sein
            //UTF8Encoding encoding = new UTF8Encoding(); //UFT-8 funktioniert unter ASP.NET nicht :(
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding() ;            
            byte[] bytes = encoding.GetBytes(sPostData);
            writeStream.Write(bytes, 0, bytes.Length);
            writeStream.Close();

            HttpWebResponse response = (HttpWebResponse) request.GetResponse();
            
            //Überprüft den HTTP-Statuscode
            if (response.StatusCode != HttpStatusCode.OK)
            {
                return "N,-1, HTTPStatuscode:" +response.StatusCode.ToString() ;
            }

            Stream responseStream = response.GetResponseStream();
            StreamReader readStream = new StreamReader (responseStream, Encoding.UTF8);
            return readStream.ReadToEnd();
        
        }


        public string GetContentToPost()
        {
            string sReturn = "" ;

            for(int i=0; i< Inputs.Keys.Count;i++)
            {
                if (sReturn.Length == 0)
                {
                    sReturn = Inputs.Keys[i] + "=" + Inputs[Inputs.Keys[i]] ;
                }
                else
                {
                    sReturn += "&" + Inputs.Keys[i] + "=" + Inputs[Inputs.Keys[i]] ;
                }//    if (sPostData.Length == 0)

            }//for(int i=0; i< Inputs.Keys.Count;i++)

            return sReturn ;

        }

        #endregion

    }
}

Kommentare (1) -

Ujoyaedo
21.01.2009 15:48:36 #

1taWWM Thanks!, <a href="voppatexge.iquebec.com/...y-porn.html">fox gay porn </a>,  cyru, <a href="voppatexge.iquebec.com/...n-name.html">gay porn name </a>,  :-(, <a href="voppatexge.iquebec.com/...porn.html">adult submitted porn </a>,  8)), <a href="voppatexge.iquebec.com/...on-porn.html">hi defination porn </a>,  :], <a href="voppatexge.iquebec.com/...celeb.html">porn free celeb </a>,  %-))), <a href="voppatexge.iquebec.com/...titan.html">porn tean titan </a>,  %-), <a href="voppatexge.iquebec.com/...m-porn.html">fat medium porn </a>,  5578,

Kommentar schreiben