36

I am using ASP.NET.

There is a system that needs to POST data to my site and all they asked for is for me to provide them with a URL. So I gave them my URL http://www.example.com/Test.aspx.

Now I do not know exactly how they POST it but now on my Test.aspx page I need to write code that will save that data to a database.

But how would this work and what must I do on my Test.aspx page?

I wrote some code in my Page Load Event that sends me an email on Page Load to see if they actually hit the page and it does not seem like they are even?

Etienne
  • 7,141
  • 42
  • 108
  • 160

3 Answers3

43

The data from the request (content, inputs, files, querystring values) is all on this object HttpContext.Current.Request
To read the posted content

StreamReader reader = new StreamReader(HttpContext.Current.Request.InputStream);
string requestFromPost = reader.ReadToEnd();

To navigate through the all inputs

foreach (string key in HttpContext.Current.Request.Form.AllKeys)
{
   string value = HttpContext.Current.Request.Form[key];
}
Adrian Iftode
  • 15,465
  • 4
  • 48
  • 73
31

You can get a form value posted to a page using code similiar to this (C#) -

string formValue;
if (!string.IsNullOrEmpty(Request.Form["txtFormValue"]))
{
  formValue= Request.Form["txtFormValue"];
}

or this (VB)

Dim formValue As String
If Not String.IsNullOrEmpty(Request.Form("txtFormValue")) Then
    formValue = Request.Form("txtFormValue")
End If

Once you have the values you need you can then construct a SQL statement and and write the data to a database.

ipr101
  • 24,096
  • 8
  • 59
  • 61
  • So txtFormValue will be one of the parameters that gets passed to my URL? – Etienne Sep 28 '11 at 08:10
  • Yes it will - you'd need to adjust the names of the of the values you were checking for in the `Request.Form` collection depending on what was being posted to you. – ipr101 Sep 28 '11 at 08:12
  • But must I place this code in my PAGE LOAD event? Does it even load the page when a POST to that URL occurs? – Etienne Sep 28 '11 at 08:17
  • PAGE LOAD would be a good place to put the code as that event should fire when data is posted to the page. You could test a post to your page using a tool such as Fiddler. – ipr101 Sep 28 '11 at 08:21
  • 1
    @Etienne, all steps of the page lifecycle (Of which `Load` is one) are called regardless of GET or POST. Some additional ones are also invoked on POST. Would strongly suggest you read the [ASP.NET Page Life Cycle Overview](http://msdn.microsoft.com/en-us/library/ms178472.aspx) to understand how ASP.NET works before progressing much further. – Kasaku Sep 28 '11 at 08:58
  • @PirateKitten, is there a server or web.config setting I need to set to allow POST to my website/page? – Etienne Sep 28 '11 at 09:26
  • There isn't. I would echo @TJHeuvel above and **strongly** suggest you buy an ASP.NET book to read up on the subject, because by the nature of your questions there will be too much to cover in comments. – Kasaku Sep 28 '11 at 09:32
0

You need to examine (put a breakpoint on / Quick Watch) the Request object in the Page_Load method of your Test.aspx.cs file.

Barry Kaye
  • 7,682
  • 6
  • 42
  • 64