How to fill a web form and submit it [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to simulate browser HTTP POST request and capture result in C#
Send POST request in C# like a web page does?
I'm making a test application in c# and I was wondering how can I fill a webform.
There is just 1 box I want to fill ,Here the details are:
<td>Your name: </td>
<td><input class="text" type="text" name="name" /></td></tr>
<tr><td colspan="2" align="center">
<input class="text" type="submit" name="submitBtn" value="Login" />
So basically I would like to take the text of a textBox(its the easy part :)) inside of my WinForms application and put it into the name field which is on the url address and then send a request or press the Login button.
Is there an easy way to do this?

Take a look at the (Http)WebRequest
How to: Send Data Using the WebRequest Class
How to: Request Data Using the WebRequest Class

Related

How to disable angularjs from capturing my form submit for the second submit button

Thanks for checking.
I am building a web application using angularjs. i have two buttons one will do a Ajax post to server using angularjs and the other will to a normal html post to server. the reason i want the second one to do a direct post to server is that i want to generate an excel document on server and return it tp user to download on response.
please how can i disable angularjs from capturing my form submit for the secont submit button
I'd say that the simplest way is to change the button that shouldn't post the form to not be a form button, but another element that doesn't have that default behaviour. That is, change this:
<form>
<input type="text">
<input type="submit" ng-click="doStuff()" value="Ajaxy things">
<input type="submit" value="Real post">
</form>
to:
<form>
<input type="text">
<a ng-click="doStuff()">Ajaxy things</a>
<input type="submit" value="Real post">
</form>
And style the a to look like a button.

DELETE method not working

I have written a WebApi controller that contains the following method for deleting a client...
[HttpDelete]
public void DeleteClient(int id) {
// do stuff here
}
...and am trying to test it by using the following HTML on a web page...
<form method="DELETE" action="/api/ClientsXml/">
<table style="padding: 5px">
<tr>
<td>ID</td>
<td><input type="text" size="20" name="id" id="id" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Send" /></td>
</tr>
</table>
</form>
However, the DeleteClient method is never called. It passes the request through to the GetClient method instead.
Anyone any idea why? I've tried all sorts of variations, but I just can't get the delete method called.
#DimitryS's answer is correct, but I thought I'd build on it a little.
HTML forms only allow GET and POST operations. This is current in the HTML 5 spec as well as the HTML < 4 spec.
Other HTTP methods are allowed when using XMLHttpRequest which is what underlies jQuery's ajax functionality. So a good option could be to use jQuery for your PUTs, DELETEs, and it should work in all major browsers (some discussion of that in this pretty definitive SO question: Are the PUT, DELETE, HEAD, etc methods available in most web browsers?).
Lastly, I'll say that if you are just using the form to test your API, then why not try a browser extension instead:
e.g.
POSTMan for chrome
RESTClient for firefox
There are many more, but most allow you to save a test suite, set different headers, and so forth.
Browsers usually can only perform GET or POST for the form elements (unless you are submitting it using AJAX).
You should change the form method to POST and add the following HTML element:
<input name="X-HTTP-Method-Override" type="hidden" value="DELETE" />
That is the way MVC allows to override HTTP methods for synchronous POSTs.
Edit: this post explains how to make the Web API support the same convention:
https://stackoverflow.com/a/13135848/395359

Passing ASP.NET values into an external web servers iframe using GET

I will be putting a big bounty on this question as it's a service I think people would really be able to leverage.
I have recently wrote a script using ASP.NET MVC/C#. It is simple enough - it displays a frontend for a database of users on a webpage. The entire code can be posted below, it doesn't really need to be read as it is quite simple, but just to give a full background of what I am doing -
Here is the main page of the site :
#model IEnumerable<WhoIs.Models.Employee>
#{
ViewBag.Title = "Contoso Employees";
}
#section featured {
<section class="featured">
<div class="content-wrapper">
<hgroup class="title">
<h1>#ViewBag.Title.</h1>
</hgroup>
</div>
</section>
}
<label for="filter">Enter employee details here : </label>
<input type="text" name="filter" value="" id="filter" />
<h2><strong>Users</strong> (create new)</h2>
<br />
<div style="min-height: 150px; font-size: 1.25em">
<div style="margin-bottom: .5em">
<table><thead><tr><th>Name</th><th>Branch</th><th>Phone No.</th><th>Username</th><th>Email</th></tr></thead>
<tbody>
#foreach ( var prod in Model )
{
<tr>
<td>#prod.FullName</td>
<td>#prod.Branch</td>
<td>#prod.PhoneNo</td>
<td>#prod.DomainAC</td>
<td>#prod.Email</td>
#if (User.IsInRole(#"Admins") || User.Identity.Name == prod.DomainAC) {
<td>edit</td>
}else{
<td>User => #User.ToString()</td>
}
<td><input type="checkbox" name="message" value="#prod.PhoneNo">Message<br></td>
</tr>
}
</tbody>
</table>
</div>
</div>
<div id="iframe2">
<br />
<iframe src="http://webaddress.com/web/login.htm" width="400" height="400" />
</div>
This renders a simple page with a list of employees at Contoso, giving Admins and users themselves the ability to edit their details. After this, I have an iframe of a remote web server I do not have control of. The iframe has some input boxes which pass values into a PHP function with a GET. Where the checkboxes are selected above, in my ASP.NET/MVC, I would like to pass the value of the checkbox (the users phone number) into my url/get command.
How can I do this ?
For completeness, although this cannot be edited, the HTML of the iframe looks like :
echo "<body>";
echo "<form action=d.php method=get>";
echo "<input type=hidden name=u value=$u>"; <!--Username passed from previous page of iFrame-->
echo "<input type=hidden name=p value=$p>";<!--Password passed from previous page of iFrame-->
echo "<input type=text name=s value=$s>";<!--List of phone numbers-->
The PHP in d.php simplifies to :
$u = $_GET['u'];
$p = $_GET['p'];
$s = $_GET['s'];
So if I put in some values, the URL changes to :
http://webaddress.com/web/d.php?u=112233&p=1234&s=12345678910&m=test
What I want to do is, for each checkbox selected above, append to &s a comma followed by the phone number of the row of the selected user.
So, for instance, if I have this in my ASP.NET MVC
Full Name Branch PhoneNo DomainAC Email Checkbox
Test1 Test1 7777777 DOMAIN\TEST1 test1#test1.com Ticked
I would like to run http://webaddress.com/web/d.php?u=112233&p=1234&s=7777777&m=test
If I have another user named "John" with a phone number of 121212, then :
http://webaddress.com/web/d.php?u=112233&p=1234&s=7777777,121212&m=test
Is this possible? How can I do this ?
If I understand your question correctly, you are trying to manipulate the values of input text fields and checkboxes inside the iframe.
The simple answer is:
If the remote domain is different from your hosting domain, it is not possible to call methods or access the iframe's content document directly using javascript. You can use cross-document messaging, but that won't work in this scenario as you cannot change the remote site's source.
If both were on the same domain, this could be accomplished by using javascript. An example of accessing the iframe's document would be:
var iframe_doc = document.getElementById('iframe_id').contentWindow.document;
From there you could transverse the DOM using js as you normally would.

Displaying error messages using C# with Razor

I'm new to C# and am trying to add some simple server side validation to my site. I've tried to Google this, but information is a little thing on the ground.
So for instance, if I values inside of a form like such :
<table>
<tr>
<td>FredF</td>
<td>Fred Flintstone</td>
<td><input type="checkbox" name="userId" value="#user.UserId" /></td>
</tr>
<tr>
<td>BarneyR</td>
<td>Barney Rubble</td>
<td><input type="checkbox" name="userId" value="#user.UserId" /></td>
</tr>
<tr>
<td>WilmaF</td>
<td>Wilma Flintstone</td>
<td><input type="checkbox" name="userId" value="#user.UserId" /></td>
</tr>
</table>​
And I want to verify that the user has ticked a checkbox, and if they haven't, I want a message to be displayed saying they must check the box.
What is best practice for doing this?
Assuming you're using MVC, best practice for server-side is to use a ViewModel with your form that has an attribute specifying if the property is required or not.
So it would be something like
class User
{
[Required]
public bool IsChecked{get;set;}
}
Then in your Controller Post Action you check Model.IsValid and re-show the form if not.
That depends on many factors, like are you using a model, are you using jquery and do you have client side validation enabled.
Read this article:
http://www.codeproject.com/Articles/344292/ASP-NET-MVC3-Razor-With-JQuery-For-Beginners
http://www.asp.net/mvc/tutorials/overview/creating-a-mvc-3-application-with-razor-and-unobtrusive-javascript

How do I read value from code behind on html page? Or any better way of doing that?

Solution: I end up creatting a WCF that accepts a get/post request, then place JQuery within the html page that retrieves the value and hands it off to the web service
I have a html page like below where I will be doing posting to a web site for registrations and my credentials are not suppose to show on the client side.
My question is how/what is the best way of reading the values from code behind or web service or any other way ?
<FORM NAME="web_form" ACTION="https://website.com/registration.php" METHOD="POST">
<TABLE WIDTH=961 BORDER=0 CELLPADDING=2 CELLSPACING=0>
<TR>
<TH WIDTH=380>
<P ALIGN=RIGHT><i>Encrypted Username:</i>
</P>
</TH>
<TD WIDTH=573>
<P><A NAME="username"></A><INPUT TYPE=TEXT NAME="username" VALUE="HOW TO GET VALUE???" SIZE=20 STYLE="width: 1.69in; height: 0.23in"></P>
</TD>
</TR>
<TR>
..............
.................
Switch the input box in to an ASP control?
<asp:TextBox runat="server" ID="username" ClientIDMode="Static" />
Then in your code behind:
this.username.Text
There's probably a little more that we need to know to make a perfect suggestion, but there's tons of different ways that you could do this.
If you have access to the registration page -- which would be ideal -- you could send a base64encoded string via the URL or via hidden input in the form field posted on submit to the reg page and then base64 decode it before passing in. Similarly, you could salt the value with any number of different methods and "un-salt" it at the application.
Merely hiding it in a hidden input field probably won't suffice here as that code is certainly available on a view source, unless the data inside it is sufficiently obfuscated.
There are javascript obfuscators that will do a sufficient job, but they'll be unavailable if the user has javascript turned off. It won't affect 99% of the users out there, but it is something to think about.
Perhaps you could set this up as a dynamic page (not HTML) and set some sort of a constant? Perhaps a session variable that you then call in at the registration page?
You can do it on client side with JavaScript:
INPUT TYPE="text" NAME="username" VALUE="some value" onclick="CopyValue()" />
function CopyValue()
{
var myValue = document.getElementById('username').value;
}
Not an answer but just a suggestion. In HTML always try to keep your values in quotes. Always a good programming practice. Will definitely help you if you are trying to move from HTML to XHTML or XML. Good Luck!

Categories

Resources