Abhishek
Abhishek

Reputation: 191

Get posted data in string form from another source in ASP.NET

I have a URL and data is being posted on that URL through ERP software from another vendor.....I want to collect the data posted on my URL in page load event from that vendor....What should be done for that in ASP.NET with c#? He does not have a field name and he is auto-generating the string of data and then posting it automatically to my ASP.NET page.

Upvotes: 3

Views: 4739

Answers (5)

Jason
Jason

Reputation: 15931

foreach(var key in Request)
{
    var data = Request[key];
}

will iterate through Request.Querystrung, Request.Form, and Request.Params.

Upvotes: 0

Soham
Soham

Reputation: 1281

First if you know what kind of data you're going to receive then you should add:

Page.Response.ContentType = "text/xml"; //For XML Data

Then read that data in stream reader:

StreamReader sr = new StreamReader(Page.Request.InputStream);

The data in streamreader is Url Encoded. So you've to decode it before you use that further:

string main = Server.UrlDecode(sr.ReadToEnd());

That's all. I hope it helps.

Upvotes: 2

Doozer Blake
Doozer Blake

Reputation: 7797

If they are doing a POST, you can use Request.Form. It will return a NameValueCollection of the elements posted to the Url, and you can loop through it if you don't know the name of what is being posted. If you know the name, then you can do Request.Form["NamedItem"].

Upvotes: 0

Marcel Valdez Orozco
Marcel Valdez Orozco

Reputation: 3015

I understand you said that the Query doesn't have a field name; meaning you can't look for it like a normal QueryString, using the string indexer. If so, then you probably have to access it without knowing the query key.

assuming you know that the 'data' is the first parameter, you could access it like this:

string data = Request.QueryString.getKey(0);

If that won't work, you can access the url directly

string query = Request.Url.Query;

Upvotes: 0

sll
sll

Reputation: 62544

var parameter = Request.QueryString["parameterName"];

if (parameter != null)
{
 //.. use it
}

See HttpRequest.QueryString

Upvotes: 1

Related Questions