How to Get the HTTP Post data in C#?

C#asp.netHttp Post

C# Problem Overview


I am using Mailgun API. There is a section that I need to provide a URL to them, then they are going to HTTP Post some data to me.

I provide this URL (http://test.com/MailGun/Webhook.aspx) to Mailgun, so they can Post data. I have a list of parameter names that they are sending like (recipient,domain, ip,...).

I am not sure how get that posted data in my page. In Webhook.aspx page I tried some code as follows but all of them are empty.

 lblrecipient.text= Request.Form["recipient"];

 lblip.Text= Request.Params["ip"];

 lbldomain.Text = Request.QueryString["domain"];

Not sure what to try to get the posted data?

C# Solutions


Solution 1 - C#

This code will list out all the form variables that are being sent in a POST. This way you can see if you have the proper names of the post values.

string[] keys = Request.Form.AllKeys;
for (int i= 0; i < keys.Length; i++) 
{
   Response.Write(keys[i] + ": " + Request.Form[keys[i]] + "<br>");
}

Solution 2 - C#

This code reads the raw input stream from the HTTP request. Use this if the data isn't available in Request.Form or other model bindings or if you need access to the bytes/text as it comes.

using(var reader = new StreamReader(Request.InputStream))
    content = reader.ReadToEnd();

Solution 3 - C#

You can simply use Request["recipient"] to "read the HTTP values sent by a client during a Web request"

> To access data from the QueryString, Form, Cookies, or ServerVariables > collections, you can write Request["key"]

Source: MSDN

Update: Summarizing conversation

In order to view the values that MailGun is posting to your site you will need to read them from the web request that MailGun is making, record them somewhere and then display them on your page.

You should have one endpoint where MailGun will send the POST values to and another page that you use to view the recorded values.

It appears that right now you have one page. So when you view this page, and you read the Request values, you are reading the values from YOUR request, not MailGun.

Solution 4 - C#

You are missing a step. You need to log / store the values on your server (mailgun is a client). Then you need to retrieve those values on your server (your pc with your web browser will be a client). These will be two totally different aspx files (or the same one with different parameters).

aspx page 1 (the one that mailgun has):

var val = Request.Form["recipient"];
var file = new File(filename);
file.write(val);
close(file);

aspx page 2:

var contents = "";
if (File.exists(filename))
  var file = File.open(filename);
  contents = file.readtoend();
  file.close()

Request.write(contents);
     

Solution 5 - C#

Use this:

    public void ShowAllPostBackData()
    {
        if (IsPostBack)
        {
            string[] keys = Request.Form.AllKeys;
            Literal ctlAllPostbackData = new Literal();
            ctlAllPostbackData.Text = "<div class='well well-lg' style='border:1px solid black;z-index:99999;position:absolute;'><h3>All postback data:</h3><br />";
            for (int i = 0; i < keys.Length; i++)
            {
                ctlAllPostbackData.Text += "<b>" + keys[i] + "</b>: " + Request[keys[i]] + "<br />";
            }
            ctlAllPostbackData.Text += "</div>";
            this.Controls.Add(ctlAllPostbackData);
        }
    }

Solution 6 - C#

In the web browser, open up developer console (F12 in Chrome and IE), then open network tab and watch the request and response data. Another option - use Fiddler (http://fiddler2.com/).

When you get to see the POST request as it is being sent to your page, look into query string and headers. You will see whether your data comes in query string or as form - or maybe it is not being sent to your page at all.

UPDATE: sorry, had to look at MailGun APIs first, they do not go through your browser, requests come directly from their server. You'll have to debug and examine all members of Request.Params when you get the POST from MailGun.

Solution 7 - C#

Try this

string[] keys = Request.Form.AllKeys;
var value = "";
for (int i= 0; i < keys.Length; i++) 
{
   // here you get the name eg test[0].quantity
   // keys[i];
   // to get the value you use
   value = Request.Form[keys[i]];
}

Solution 8 - C#

In my case because I assigned the post data to the header, this is how I get it:

protected void Page_Load(object sender, EventArgs e){
    ...
    postValue = Request.Headers["Key"];

This is how I attached the value and key to the POST:

var request = new NSMutableUrlRequest(url){
    HttpMethod = "POST", 
    Headers = NSDictionary.FromObjectAndKey(FromObject(value), FromObject("key"))
};
webView.LoadRequest(request);

Solution 9 - C#

You can try to check the 'Request.Form.Keys'. If it will not works well, you can use 'request.inputStream' to get the soap string which will tell you all the request keys.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlmaView Question on Stackoverflow
Solution 1 - C#James LawrukView Answer on Stackoverflow
Solution 2 - C#Fred MauroyView Answer on Stackoverflow
Solution 3 - C#Cloud SMEView Answer on Stackoverflow
Solution 4 - C#Gerard ONeillView Answer on Stackoverflow
Solution 5 - C#Tone ŠkodaView Answer on Stackoverflow
Solution 6 - C#Roman PoluninView Answer on Stackoverflow
Solution 7 - C#gdmanandamohonView Answer on Stackoverflow
Solution 8 - C#Mr.KView Answer on Stackoverflow
Solution 9 - C#vikingView Answer on Stackoverflow