POST using webclient and WebRequest does not work
I want to write a code in c# to upload values on html page that has text boxes and then click button below.
If I view page source there are two attributes of textbox that are name and ID. Which one should be used while uploading values?
I tried using webclient and http response as well but nothing works.
I started with a demo ASP.Net application given with vs 2010.I cannot login to that page using this code
Html is
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>
Log In
</title><link href="../Styles/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form method="post" action="Login.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="ctl01">
<div class="aspNetHidden">
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJMTM4OTQwMDM1ZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAQUmY3RsMDAkTWFpbkNvbnRlbnQkTG9naW5Vc2VyJFJlbWVtYmVyTWXIWDJ/V4XPecqwQe85aLVZC5vuxVoEv8dHC/Mo9pwQZw==" />
</div>
<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['ctl01'];
if (!theForm) {
theForm = document.ctl01;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
</script>
<script src="/WebSite1/WebResource.axd?d=M5W_658FhJm5n8t9pSFvKuIXRL5Jz7-D_eS0HTIT8JulfjzpAVV_rJb8XF2y3yXBzfewu3sSKDiVdWxirFJDeFUBJp7DwWkz9bRlwYT_VkQ1&t=634271460710781250" type="text/javascript"></script>
<script src="/WebSite1/WebResource.axd?d=d41DICiQepBPG6K5sYiG4w6-5Zow9401xGbcRJ_YjjKKZBzgTrTC_CdIv9aUM1wPVO22HhAV6begkxuIulwZFj_WKMwGJvOFNsnT-yQ7hO41&t=634271460710781250" type="text/javascript"></script>
<script src="/WebSite1/WebResource.axd?d=XnFE4lWLk9gTnM0_06V_76Tt3DRk8edc94bUtBQAZMiY1p3YHp03awb6v8Nz44Zo-gdy2g6LBBsaWMuUeNLboK5ebvvrsIwawVtCZfaujlw1&t=634271460710781250" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;
return true;
}
//]]>
</script>
<div class="aspNetHidden">
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWBQKA+oZWAsXK+OQPAtDNs5YCApW7jtAPAqfAqeMFgx5T3T+5v78wSO8pWO0ORp/qKya1HTwMXoYN8GOYDSo=" />
</div>
<div class="page">
<div class="header">
<div class="title">
<h1>
My ASP.NET Application
</h1>
</div>
<div class="loginDisplay">
[ <a href="Login.aspx" id="HeadLoginView_HeadLoginStatus">Log In</a> ]
</div>
<div class="clear hideSkiplink">
<a href="#Navigation开发者_如何转开发Menu_SkipLink"><img alt="Skip Navigation Links" src="/WebSite1/WebResource.axd?d=kJ6t3tHV3cLpBZWByP4-yIKJkygtz3ErE4Tpn7tMIEa4uB2lty0z6Toys6myTCJiM7h32lxUN9nyuc9K6T9RIvRlxGRQvIJjoE2vXGjX6Bw1&t=634271460710781250" width="0" height="0" style="border-width:0px;" /></a><div class="menu" id="NavigationMenu">
<ul class="level1">
<li><a class="level1" href="../Default.aspx">Home</a></li><li><a class="level1" href="../About.aspx">About</a></li>
</ul>
</div><a id="NavigationMenu_SkipLink"></a>
</div>
</div>
<div class="main">
<h2> Log In </h2>
<p>
Please enter your username and password.
<a id="MainContent_RegisterHyperLink" href="Register.aspx?ReturnUrl=">Register</a> if you don't have an account.
</p>
<span class="failureNotification">
</span>
<div id="MainContent_LoginUser_LoginUserValidationSummary" class="failureNotification" style="display:none;">
</div>
<div class="accountInfo">
<fieldset class="login">
<legend>Account Information</legend>
<p>
<label for="MainContent_LoginUser_UserName" id="MainContent_LoginUser_UserNameLabel">Username:</label>
<input name="ctl00$MainContent$LoginUser$UserName" type="text" id="MainContent_LoginUser_UserName" class="textEntry" />
<span id="MainContent_LoginUser_UserNameRequired" title="User Name is required." class="failureNotification" style="visibility:hidden;">*</span>
</p>
<p>
<label for="MainContent_LoginUser_Password" id="MainContent_LoginUser_PasswordLabel">Password:</label>
<input name="ctl00$MainContent$LoginUser$Password" type="password" id="MainContent_LoginUser_Password" class="passwordEntry" />
<span id="MainContent_LoginUser_PasswordRequired" title="Password is required." class="failureNotification" style="visibility:hidden;">*</span>
</p>
<p>
<input id="MainContent_LoginUser_RememberMe" type="checkbox" name="ctl00$MainContent$LoginUser$RememberMe" />
<label for="MainContent_LoginUser_RememberMe" id="MainContent_LoginUser_RememberMeLabel" class="inline">Keep me logged in</label>
</p>
</fieldset>
<p class="submitButton">
<input type="submit" name="ctl00$MainContent$LoginUser$LoginButton" value="Log In" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$MainContent$LoginUser$LoginButton", "", true, "LoginUserValidationGroup", "", false, false))" id="MainContent_LoginUser_LoginButton" />
</p>
</div>
</div>
<div class="clear">
</div>
</div>
<div class="footer">
</div>
<script type="text/javascript">
//<![CDATA[
var Page_ValidationSummaries = new Array(document.getElementById("MainContent_LoginUser_LoginUserValidationSummary"));
var Page_Validators = new Array(document.getElementById("MainContent_LoginUser_UserNameRequired"), document.getElementById("MainContent_LoginUser_PasswordRequired"));
//]]>
</script>
<script type="text/javascript">
//<![CDATA[
var MainContent_LoginUser_LoginUserValidationSummary = document.all ? document.all["MainContent_LoginUser_LoginUserValidationSummary"] : document.getElementById("MainContent_LoginUser_LoginUserValidationSummary");
MainContent_LoginUser_LoginUserValidationSummary.validationGroup = "LoginUserValidationGroup";
var MainContent_LoginUser_UserNameRequired = document.all ? document.all["MainContent_LoginUser_UserNameRequired"] : document.getElementById("MainContent_LoginUser_UserNameRequired");
MainContent_LoginUser_UserNameRequired.controltovalidate = "MainContent_LoginUser_UserName";
MainContent_LoginUser_UserNameRequired.errormessage = "User Name is required.";
MainContent_LoginUser_UserNameRequired.validationGroup = "LoginUserValidationGroup";
MainContent_LoginUser_UserNameRequired.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_LoginUser_UserNameRequired.initialvalue = "";
var MainContent_LoginUser_PasswordRequired = document.all ? document.all["MainContent_LoginUser_PasswordRequired"] : document.getElementById("MainContent_LoginUser_PasswordRequired");
MainContent_LoginUser_PasswordRequired.controltovalidate = "MainContent_LoginUser_Password";
MainContent_LoginUser_PasswordRequired.errormessage = "Password is required.";
MainContent_LoginUser_PasswordRequired.validationGroup = "LoginUserValidationGroup";
MainContent_LoginUser_PasswordRequired.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_LoginUser_PasswordRequired.initialvalue = "";
//]]>
</script>
<script type='text/javascript'>new Sys.WebForms.Menu({ element: 'NavigationMenu', disappearAfter: 500, orientation: 'horizontal', tabIndex: 0, disabled: false });</script>
<script type="text/javascript">
//<![CDATA[
var Page_ValidationActive = false;
if (typeof(ValidatorOnLoad) == "function") {
ValidatorOnLoad();
}
function ValidatorOnSubmit() {
if (Page_ValidationActive) {
return ValidatorCommonOnSubmit();
}
else {
return true;
}
}
//]]>
</script>
</form>
</body>
var strId = UserName.Text;
var strName = UserPass.Text;
var encoding = new ASCIIEncoding();
var postData = "ctl00$MainContent$LoginUser$UserName=" + strId;
postData += ("ctl00$MainContent$LoginUser$Password=" + strName);
byte[] data = encoding.GetBytes(postData);
var myRequest = (HttpWebRequest)WebRequest.Create("http://localhost:6226/WebSite1/Account/Login.aspx");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
var newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
var response = myRequest.GetResponse();
var responseStream = response.GetResponseStream();
var responseReader = new StreamReader(responseStream);
textBox1.Text = responseReader.ReadToEnd();
I do not see a '&' between the post data elements.
精彩评论