How to Get the HTTP Post data in C#? - c#

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?

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>");
}

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();

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.

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);

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);
}
}

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.

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]];
}

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);

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.

Related

how to change selected value in html using c#?

im trying to get some datas from a webpage. im writing code in c# .net. the webpage has a dropdown list (or combobox) like i showed below. data changes according to selected dropdown list item but url does not change. my question is how can my code changes selected values and get datas from webpage? i parsed and got for only one of items like that:
**WebClient wc = new WebClient();
string kaynak = wc.DownloadString("http://www.diyanet.gov.tr/");
string imsak = "spImsak";
int imindex = kaynak.IndexOf(imsak);
imindex += 9;
System.Console.WriteLine(kaynak.Substring(imindex, 5));**
< span id="spImsak">02:44< /span >
i downloaded html code of webpage as a string. searched "spImsak". finally i got "02:44" as a string. and i wanna do it for all combobox items. can you give me any advice?
sample webpage : http://www.diyanet.gov.tr/
red ones are comboboxes. yellow one is data that i wanna get.
i have followed up webpage's network and saw that when i click any dropdown list element, webpage runs a web service with parameters. i explain how to applied that to my question.
web service and parameters image
all i need send a POST request this web service with these parameters and got string (json). i did that following c# code.
using (WebClient client = new WebClient())
{
int turn;
byte[] response;
string result;
/* gets response for 81 city */
for (turn = 500; turn < 581; ++turn)
{
response =
client.UploadValues("http://diyanet.gov.tr/PrayerTime/MainPrayerTimesSet", new NameValueCollection()
{
{ "countryName", "2" },
{ "name", turn.ToString() }
});
/* without sleep, web service does not response successive requests */
System.Threading.Thread.Sleep(5);
/* turns incoming byte[] -> string */
result = System.Text.Encoding.UTF8.GetString(response);
Console.WriteLine(result);
}
}

How to send Request.Form

I'm doing something really bad with my code. I'm getting all data posted to the actual page and putting into html inputs:
private void GetPostedForm()
{
System.Text.StringBuilder displayValues = new System.Text.StringBuilder();
System.Collections.Specialized.NameValueCollection postedValues = Request.Form;
for (int i = 0; i < postedValues.AllKeys.Length; i++)
{
String nextKey = postedValues.AllKeys[i];
if (nextKey.Substring( 0, 2 ) != "__")
{
displayValues.Append( "<input type='hidden' name='" + nextKey + "' value='" + postedValues[i] + "'/>" );
}
}
hiddensPost.InnerHtml = displayValues.ToString();
}
But the html inputs in this page are useless to me. I'm putting a page between 2 older pages ("A" sent form to "B"). Now I need to send "A" to "X" and then send to "B".
The question is: How can I put the requested form into the actual form to send to the next page without doing all this mess in HTML?
You can put your steps(A,X,B) and it's visible inputs, into separate asp-panels(pnlA,pnlX,pnlB)
then simply toggle panels visibility in which state you want.the ViewState will do it for you (store controls states into one hidden field within the form to post again with inputs)
so you may post user entered data 3 times with one html form( the famous asp.net form)
another solution is here , the asp.net wizard control
If you can, just change the method to GET and pass the QueryString along from page to page.

Getting the referrer's web server name

I am placing a JS file to remote server(s). I d like to know where the request is coming from.
ie : i have a js on google.com and upon user click on the link that s produced by js, it triggers some c# code on my server, but i also have the same js on yahoo.com and i d like to be able to know where the request is coming from.
How to find this ?
System.Environment.MachineName should contain the name of the server.
In ASP.NET, the referring page is given by Request.UrlReferrer as a Uri object.
This is also available as Request.ServerVariables["HTTP_REFERER"] as a string.
Request.ServerVariables["SERVER_NAME"]
Also you may need:
Request.ServerVariables["SERVER_PORT"]
Following on your own lead for "System.Web.HttpContext.Current.Request.ServerVariables", the MSDN documentation for the ServerVariables property contains some sample code on how to retrieve all available named server variables:
int loop1, loop2;
NameValueCollection coll;
// Load ServerVariable collection into NameValueCollection object.
coll=Request.ServerVariables;
// Get names of all keys into a string array.
String[] arr1 = coll.AllKeys;
for (loop1 = 0; loop1 < arr1.Length; loop1++)
{
Response.Write("Key: " + arr1[loop1] + "<br>");
String[] arr2=coll.GetValues(arr1[loop1]);
for (loop2 = 0; loop2 < arr2.Length; loop2++) {
Response.Write("Value " + loop2 + ": " + Server.HtmlEncode(arr2[loop2]) + "<br>");
}
}
Furthermore, it also contains a link to all server variables supported by IIS
IIS Server Variables
For example, the "REMOTE_HOST" variable will give you:
The name of the host that is making the request. If the server does not have this information, it will set REMOTE_ADDR and leave this empty.

Screen scraping HTTPS using C#

How to screen scrape HTTPS using C#?
You can use System.Net.WebClient to start an HTTPS connection, and pull down the page to scrape with that.
Look into the Html Agility Pack.
You can use System.Net.WebClient to grab web pages. Here is an example: http://www.codersource.net/csharp_screen_scraping.html
If for some reason you're having trouble with accessing the page as a web-client or you want to make it seem like the request is from a browser, you could use the web-browser control in an app, load the page in it and use the source of the loaded content from the web-browser control.
Here's a concrete (albeit trivial) example. You can pass a ship name to VesselFinder in the querystring, but even if it only finds one ship with that name it still shows you the search results screen with one ship. This example detects that case and takes the user straight to the tracking map for the ship.
string strName = "SAFMARINE MAFADI";
string strURL = "https://www.vesselfinder.com/vessels?name=" + HttpUtility.UrlEncode(strName);
string strReturnURL = strURL;
string strToSearch = "/?imo=";
string strPage = string.Empty;
byte[] aReqtHTML;
WebClient objWebClient = new WebClient();
objWebClient.Headers.Add("User-Agent: Other"); //You must do this or HTTPS won't work
aReqtHTML = objWebClient.DownloadData(strURL); //Do the name search
UTF8Encoding utf8 = new UTF8Encoding();
strPage = utf8.GetString(aReqtHTML); // get the string from the bytes
if (strPage.IndexOf(strToSearch) != strPage.LastIndexOf(strToSearch))
{
//more than one instance found, so leave return URL as name search
}
else if (strPage.Contains(strToSearch) == true)
{
//find the ship's IMO
strPage = strPage.Substring(strPage.IndexOf(strToSearch)); //cut off the stuff before
strPage = strPage.Substring(0, strPage.IndexOf("\"")); //cut off the stuff after
}
strReturnURL = "https://www.vesselfinder.com" + strPage;

How to post data from a webform page to an HTTPHandler.ashx file?

I have a web application project to support file transfer operations to vendor product backend. It's composed of 2 HTTPHandler files compiled into a website on a Win2003 server with IIS 6.0:
UploadHandler.ashx
DownloadHandler.ashx
These files get "POSTed to" from a ColdFusion website that exposes the user interface. In a way, my job is done because these handlers work and have to be called from ColdFusion.
Yet, I am very frustrated with my inability to get my own "test UI" (default.aspx) to use in my testing/refinement independent of ColdFusion.
<asp:Button ID="DownloadButton" PostBackUrl="~/DownloadHandler.ashx" runat="server" Text="Download"/>
Using a PostBackUrl for Download works nicely - when the DownloadHandler.ashx is entered, it finds its key input value in context.Request.Form["txtRecordNumber"];
But I cannot use this technique for Upload because I have to do some processing (somehow read the bytes from the chosen fileupload1.postedfile into a FORM variable so my UploadHandler.ashx file can obtain its input from Request.Form as with Download).
My first approach tried using HTTPWebRequest which seemed overly complex and I could never get to work. Symptoms began with a HTTP 401 status code and then morphed into a 302 status code so I researched other ideas.
Here is my latest code snippet from my default.aspx:
protected void UploadHandlerButton_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
BuildFormData();
//Server.Transfer("UploadHandler.ashx", true);
Response.Redirect("~/UploadHandler.ashx");
}
catch (Exception someError)
{
LogText("FAILURE: " + someError.Message);
}
}
}
protected void BuildFormData()
{
BinaryReader b = new BinaryReader(FileUpload1.PostedFile.InputStream);
int numBytes = FileUpload1.PostedFile.ContentLength;
byte[] fileContent = b.ReadBytes(numBytes);
objBinaryData.Text = System.Text.Encoding.UTF8.GetString(fileContent);
b64fileName.Text = FileUpload1.PostedFile.FileName;
// create arbitrary MetaData in a string
strMetaData.Text = "recAuthorLoc=anyname1~udf:OPEAnalyst=anyname2~udf:Grant Number=0102030405";
}
Attempts to use Server.Transfer (above) to my .ashx file result in an error:
error executing child request for UploadHandler.ashx
Attempts to use Response.Redirect (above) to my .ashx file result in GET (not POST) and Trace.axd of course shows nothing in the Form collection so that seems wrong too.
I even tried clone-ing my .ashx file and created UploadPage.aspx (a webform with no HTML elements) and then tried:
Server.Transfer("UploadPage.aspx", true);
//Response.Redirect("~/UploadPage.aspx");
Neither of those allow me to see the form data I need to see in Request.Form within my code that processes the Upload request. I am clearly missing something here...thanks in advance for helping.
EDIT-UPDATE:
I think I can clarify my problem. When the UploadHandler.ashx is posted from ColdFusion, all of the input it needs is available in the FORM collection (e.g. Request.Form["fileData"] etc.)
But when I use this control it generates a postback to my launching web page (i.e. default.aspx). This enables me to refer to the content by means of FileUpload1.PostedFile as in:
protected void BuildFormData()
{
BinaryReader b = new BinaryReader(FileUpload1.PostedFile.InputStream);
int numBytes = FileUpload1.PostedFile.ContentLength;
byte[] fileContent = b.ReadBytes(numBytes);
objBinaryData.Text = System.Text.Encoding.UTF8.GetString(fileContent);
b64fileName.Text = FileUpload1.PostedFile.FileName;
}
Yet I am not using the FileUpload1.PostedFile.SaveAs method to save the file somewhere on my web server. I need to somehow - forgive the language here - "re-post" this data to an entirely different file - namely, my UploadHandler.ashx handler. All the goofy techniques I've tried above fail to accomplish what I need.
EDIT-UPDATE (20 Aug 2009) - my final SOLUTION using Javascript:
protected void UploadHandlerButton_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
ctlForm.Text = BuildFormData();
String strJS = InjectJS("_xclick");
ctlPostScript.Text = strJS;
}
catch (Exception someError)
{
LogText("FAILURE: " + someError.Message);
}
}
}
private String InjectJS(String strFormId)
{
StringBuilder strScript = new StringBuilder();
strScript.Append("<script language='javascript'>");
strScript.Append("var ctlForm1 = document.forms.namedItem('{0}');");
strScript.Append("ctlForm1.submit();");
strScript.Append("</script>");
return String.Format(strScript.ToString(), strFormId);
}
protected string BuildFormData()
{
BinaryReader b = new BinaryReader(FileUpload1.PostedFile.InputStream);
int numBytes = FileUpload1.PostedFile.ContentLength;
byte[] fileContent = b.ReadBytes(numBytes);
// Convert the binary input into Base64 UUEncoded output.
string base64String;
base64String =
System.Convert.ToBase64String(fileContent,
0,
fileContent.Length);
objBinaryData.Text = base64String;
b64fileName.Text = FileUpload1.PostedFile.FileName;
// create arbitrary MetaData in a string
strMetaData.Text = "recAuthorLoc=Patterson, Fred~udf:OPEAnalyst=Tiger Woods~udf:Grant Number=0102030405";
StringBuilder strForm = new StringBuilder();
strForm.Append("<form id=\"_xclick\" name=\"_xclick\" target=\"_self\" action=\"http://localhost/HTTPHandleTRIM/UploadHandler.ashx\" method=\"post\">");
strForm.Append("<input type=\"hidden\" name=\"strTrimURL\" value=\"{0}\" />");
strForm.Append("<input type=\"hidden\" name=\"objBinaryData\" value=\"{1}\" />");
strForm.Append("<input type=\"hidden\" name=\"b64fileName\" value=\"{2}\" />");
strForm.Append("<input type=\"hidden\" name=\"strDocument\" value=\"{3}\" />");
strForm.Append("<input type=\"hidden\" name=\"strMetaData\" value=\"{4}\" />");
strForm.Append("</form>");
return String.Format(strForm.ToString()
, txtTrimURL.Text
, objBinaryData.Text
, b64fileName.Text
, txtTrimRecordType.Text
, strMetaData.Text);
}
Sorry if I'm missing something, but can't you simply use a plain HTML form to upload files to your handler:
<form action="UploadHandler.ashx" method="post" enctype="multipart/form-data">
Choose file to upload:
<input name="file" type="file" size="50">
</form>
What worked for me was to inject a new FORM and some Javascript to submit the FORM to the UploadHandler.ashx. This (for me) was easier to grasp than the HTTPWebRequest technique.
John Galt,
The only way to do what you want is using HttpWebRequest.
Here is good example of a Class that do what you want. I've made to send image and form values to picassa serve sometime ago (I know I could use Picassa API, but I did it for fun).
You only need to pay attention to the 'SendPhoto' function to get hints on what you have to do to make HttpWebRequest to do the work.

Categories