using HttpWebResponse to get POST variables

  • Thread starter Thread starter David Billson
  • Start date Start date
D

David Billson

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(HttpWResponse.GetResponseStream(),
System.Text.Encoding.ASCII);
//Convert the stream to a string
string s = sr.ReadToEnd();
sr.Close();
Response.Write(s);

RETURNS:

s ="trnApproved=1&trnId=10000843&messageId=1&messageText=Approved&authCode=TEST&errorType=N&errorFields=&avsProcessed=0&avsId=0&avsResult=0&avsAddrMatch=0&avsPostalMatch=0&avsMessage=Address+Verification+not+performed+for+this+transaction%2E"

I'd just like a way to read Request["trnApproved"] or
Request["messageText"] - Any ideas?

Thanks,
DB
 
I create a helper class , to strore the request array

using System;
using System.Collections;
using System.Collections.Specialized;

namespace Erymuzuan.WinContainer
{
/// <summary>
/// Summary description for BrowserInfo.
/// </summary>
public class BrowserInfo : NameObjectCollectionBase
{
/// <summary> </summary>
private DictionaryEntry m_de = new DictionaryEntry();

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

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

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

}
/// <summary> Gets a key-and-value pair (DictionaryEntry) using an
index.</summary>
public DictionaryEntry this[ int index ]
{
get
{
m_de.Key = BaseGetKey(index);
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.BaseGetAllKeys() ); }
}

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

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


/// <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 said:
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(HttpWResponse.GetResponseStream(),
System.Text.Encoding.ASCII);
//Convert the stream to a string
string s = sr.ReadToEnd();
sr.Close();
Response.Write(s);

RETURNS:

s ="trnApproved=1&trnId=10000843&messageId=1&messageText=Approved&authCode=TES
T&errorType=N&errorFields=&avsProcessed=0&avsId=0&avsResult=0&avsAddrMatch=0
&avsPostalMatch=0&avsMessage=Address+Verification+not+performed+for+this+tra
nsaction%2E"

I'd just like a way to read Request["trnApproved"] or
Request["messageText"] - Any ideas?

Thanks,
DB
 
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 GetResponseStream(). Food for thought...
 
Back
Top