Using the best answer from this question, I could successfully submit a login form and get an authenticated webpage using WebClient:
var loginCredentials = new NameValueCollection
{
{ "username", "user123" },
{ "password", "pass321" },
};
client.UploadValues(
@"http://www.mywebsite.com/User/Login?ReturnUrl=" +
@"http://www.mywebsite.com/Tickets/Search", loginCredentials);
However, when I try submitting another form to search for specific filters (one is a "dropdown" element and the other is an "option" element) on the resulting webpage, I get a webexception on the method UploadValues:
var jitbitSearch = new NameValueCollection
{
{ "fromUserId_username", "447" },
{ "statusId", "-3" },
};
try
{
client.UploadValues(
@"http://www.mywebsite.com/Tickets/Search", jitbitSearch);
}
catch {}
string result = client.DownloadString("http://www.mywebsite.com/Tickets/Search");
textBox1.Text = result;
HTML output of fromUserId_username (dropdown):
<td><script type='text/javascript' src='/js/jquery.autocomplete.js'></script> <input type="text" id="fromUserId_username" name="fromUserId_username" value="" placeholder="Email or user" />
<input type="hidden" id="fromUserId" name="fromUserId" value="" />
<script type="text/javascript">
$(function() {
InitUserBox('fromUserId', "");
});
</script>
HTML output of statusId (select option):
<td>Status:</td>
<td><select name="statusId">
<option value=""></option>
<option value="10" >Undergoing</option>
<option value="5" >Under analysis</option>
<option value="2" >Pending</option>
<option value="6" >On hold</option>
<option value="3" >Closed</option>
<option value="1" >Under testing</option>
<option value="4" >New</option>
<option value="7" >Under development</option>
<option value="8" >Recurring</option>
<option value="">--------</option>
<option value="-10" style="color:#CC6666;">(not) Undergoing</option>
<option value="-5" style="color:#CC6666;">(not) Under analysis</option>
<option value="-2" style="color:#CC6666;">(not) Pending</option>
<option value="-6" style="color:#CC6666;">(not) On hold</option>
<option value="-3" style="color:#CC6666;">(not) Closed</option>
<option value="-1" style="color:#CC6666;">(not) Under testing</option>
<option value="-4" style="color:#CC6666;">(not) New</option>
<option value="-7" style="color:#CC6666;">(not) Under development</option>
<option value="-8" style="color:#CC6666;">(not) Recurring</option>
</select>
Any idea what's causing the exception? Is it because the method UploadValues doesn't support this kind of input? Thanks.
Note: Even if I comment out one of the form inputs, both of them trigger the webexception.
EDIT: The exception I'm getting is the following: "The remote server returned an error: (500) Internal Server Error".