using HttpWebResponse to get POST variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Billson

    using HttpWebResponse to get POST variables

    Hi all,

    Fairly straight forward problem. I have a Post using HttpWebRequest
    that posts data to a server. That server then posts data back to me.
    Posting the data's not a problem, however I'm wondering if there is an
    easy way to read back the variables that are returned. If not, I'm
    left creating a parse routine which is not the end of the world but I
    just want to be as programatically correct as I can be.

    Code:

    StreamReader sr = new StreamReader(Ht tpWResponse.Get ResponseStream( ),
    System.Text.Enc oding.ASCII);
    //Convert the stream to a string
    string s = sr.ReadToEnd();
    sr.Close();
    Response.Write( s);

    RETURNS:

    s ="trnApproved=1 &trnId=10000843 &messageId=1&me ssageText=Appro ved&authCode=TE ST&errorType=N& errorFields=&av sProcessed=0&av sId=0&avsResult =0&avsAddrMatch =0&avsPostalMat ch=0&avsMessage =Address+Verifi cation+not+perf ormed+for+this+ transaction%2E"

    I'd just like a way to read Request["trnApprove d"] or
    Request["messageTex t"] - Any ideas?

    Thanks,
    DB
  • Erymuzuan Mustapa

    #2
    Re: using HttpWebResponse to get POST variables

    I create a helper class , to strore the request array

    using System;
    using System.Collecti ons;
    using System.Collecti ons.Specialized ;

    namespace Erymuzuan.WinCo ntainer
    {
    /// <summary>
    /// Summary description for BrowserInfo.
    /// </summary>
    public class BrowserInfo : NameObjectColle ctionBase
    {
    /// <summary> </summary>
    private DictionaryEntry m_de = new DictionaryEntry ();

    /// <summary>
    /// ctor
    /// </summary>
    /// <example>
    /// new BrowserInfo("my q=msa&sw=12&id= 1232");
    /// </example>
    /// <param name="postData" >query string pairs
    /// </param>
    public BrowserInfo(str ing postData)
    {
    string name;
    string val;

    foreach ( string s in postData.Split( '&'))
    {
    name = s.Substring(0, s.IndexOf("=") );
    val = s.Substring(s.I ndexOf("=") + 1 , s.Length - (name.Length + 1));

    // add it
    BaseAdd(name, val);
    }

    }
    /// <summary> Gets a key-and-value pair (DictionaryEntr y) using an
    index.</summary>
    public DictionaryEntry this[ int index ]
    {
    get
    {
    m_de.Key = BaseGetKey(inde x);
    m_de.Value = BaseGet(index);
    return( m_de );
    }
    }

    //
    /// <summary> Gets or sets the value associated with the specified
    key.</summary>
    public string this[ string key ]
    {
    get { return( BaseGet( key ).ToString() ); }
    set { BaseSet( key, value ); }
    }

    //
    /// <summary> Gets a String array that contains all the keys in the
    collection.</summary>
    public String[] AllKeys
    {
    get { return( this.BaseGetAll Keys() ); }
    }

    //
    /// <summary> Gets an Object array that contains all the values in the
    collection.</summary>
    public Array AllValues
    {
    get { return( this.BaseGetAll Values() ); }
    }

    //
    /// <summary> Gets a value indicating if the collection contains keys that
    are not null.</summary>
    public Boolean HasKeys
    {
    get { return( this.BaseHasKey s() ); }
    }


    /// <summary>
    /// Adds an entry to the collection.
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    public void Add( String key, Object value )
    {
    this.BaseAdd( key, value );
    }



    }// class
    }


    --
    Erymuzuan Mustapa
    Inter Virtual Sdn. Bhd.
    See MIND at http://www.MIND.com.my

    "David Billson" <david.billson@ rtraction.com> wrote in message
    news:9bf45cea.0 308211816.77cdb f87@posting.goo gle.com...[color=blue]
    > Hi all,
    >
    > Fairly straight forward problem. I have a Post using HttpWebRequest
    > that posts data to a server. That server then posts data back to me.
    > Posting the data's not a problem, however I'm wondering if there is an
    > easy way to read back the variables that are returned. If not, I'm
    > left creating a parse routine which is not the end of the world but I
    > just want to be as programatically correct as I can be.
    >
    > Code:
    >
    > StreamReader sr = new StreamReader(Ht tpWResponse.Get ResponseStream( ),
    > System.Text.Enc oding.ASCII);
    > //Convert the stream to a string
    > string s = sr.ReadToEnd();
    > sr.Close();
    > Response.Write( s);
    >
    > RETURNS:
    >
    > s[/color]
    ="trnApproved=1 &trnId=10000843 &messageId=1&me ssageText=Appro ved&authCode=TE S
    T&errorType=N&e rrorFields=&avs Processed=0&avs Id=0&avsResult= 0&avsAddrMatch= 0
    &avsPostalMatch =0&avsMessage=A ddress+Verifica tion+not+perfor med+for+this+tr a
    nsaction%2E"[color=blue]
    >
    > I'd just like a way to read Request["trnApprove d"] or
    > Request["messageTex t"] - Any ideas?
    >
    > Thanks,
    > DB[/color]


    Comment

    • Frank Drebin

      #3
      Re: using HttpWebResponse to get POST variables

      Since you are programatically going to another web page, chances are you are
      doing this to get data? If so, and if you have control over it - the server
      you are calling - should return XML. That way all your data will be nice and
      organized when it do a GetResponseStre am(). Food for thought...


      "David Billson" <david.billson@ rtraction.com> wrote in message
      news:9bf45cea.0 308211816.77cdb f87@posting.goo gle.com...[color=blue]
      > Hi all,
      >
      > Fairly straight forward problem. I have a Post using HttpWebRequest
      > that posts data to a server. That server then posts data back to me.
      > Posting the data's not a problem, however I'm wondering if there is an
      > easy way to read back the variables that are returned. If not, I'm
      > left creating a parse routine which is not the end of the world but I
      > just want to be as programatically correct as I can be.
      >
      > Code:
      >
      > StreamReader sr = new StreamReader(Ht tpWResponse.Get ResponseStream( ),
      > System.Text.Enc oding.ASCII);
      > //Convert the stream to a string
      > string s = sr.ReadToEnd();
      > sr.Close();
      > Response.Write( s);
      >
      > RETURNS:
      >
      > s[/color]
      ="trnApproved=1 &trnId=10000843 &messageId=1&me ssageText=Appro ved&authCode=TE S
      T&errorType=N&e rrorFields=&avs Processed=0&avs Id=0&avsResult= 0&avsAddrMatch= 0
      &avsPostalMatch =0&avsMessage=A ddress+Verifica tion+not+perfor med+for+this+tr a
      nsaction%2E"[color=blue]
      >
      > I'd just like a way to read Request["trnApprove d"] or
      > Request["messageTex t"] - Any ideas?
      >
      > Thanks,
      > DB[/color]


      Comment

      Working...