I want to log in to a website using C# code..
The main aim is to check whether the username and password I provide is
correct or not.
I used the following code
bool somefunction()
{
string username = textbox1.text;
string password = textbox2.text;
string url = "http://youtube.com/signup?username=" + textBox2.Text +
"&password=" + textBox3.Text + "¤t_form=loginForm&action_login=1";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(str);
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
CookieCollection cookie;
cookie = resp.Cookies;
bool success = false;
foreach (Cookie c in cookie)
{
if (c.Name == "LOGIN_INFO" && c.Value != "")
{
success = true;
}
}
return success;
}
But however it shows an exception........
System.Net.WebException was unhandled
Message="The server committed a protocol violation.
Section=ResponseStatusLine"
Source="System"
StackTrace:
at System.Net.HttpWebRequest.GetResponse()
at ContentExtractor.form3.button1_Click(Object sender, EventArgs e)
in G:\Docs\Kitty\Visual Studio
2005\Projects\Earnings\ContentExtractor\ContentExtractor\form3.cs:line 38
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&
m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&
msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at ContentExtractor.Program.Main() in G:\Docs\Kitty\Visual Studio
2005\Projects\Earnings\ContentExtractor\ContentExtractor\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
The original login form is located at http://www.youtube.com/index
and however if I happen to type the url
string url = "http://youtube.com/signup?username=" + textBox2.Text +
"&password=" + textBox3.Text + "¤t_form=loginForm&action_login=1";
Can anyone help me how to do this..
And I also need an overview of how to post into any website form if I know
the field names and the action url is known...
Thanks in advance
Regards
Rajkiran
Mr. Arnold - 13 Jan 2008 16:29 GMT
> System.Net.WebException was unhandled
> Message="The server committed a protocol violation.
Maybe, there is a proxy server sitting there, and you can't do it.
Arne Vajhøj - 13 Jan 2008 16:59 GMT
> I used the following code
>
[quoted text clipped - 12 lines]
>
> HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
> System.Net.WebException was unhandled
> Message="The server committed a protocol violation.
[quoted text clipped - 5 lines]
> e) in G:\Docs\Kitty\Visual Studio
> 2005\Projects\Earnings\ContentExtractor\ContentExtractor\form3.cs:line 38
> The original login form is located at http://www.youtube.com/index
>
> and however if I happen to type the url
>
> string url = "http://youtube.com/signup?username=" + textBox2.Text +
> "&password=" + textBox3.Text + "¤t_form=loginForm&action_login=1";
I can see at least two errors in your code:
1) You are doing POST but still try to specify the form fields
in the URL - they should go into the request body.
2) You are missing a hidden field (the one with the name "next").
Arne
Rajkiran R.B. - 13 Jan 2008 17:36 GMT
thanks for the replies.. Here is how the code of the webpage is
<form
method="post" name="loginForm" id="loginFormZ" action="/signup">
<input id="loginNextZ" name="next" type="hidden" value="" />
<input type="hidden" name="current_form" value="loginForm" />
<input type="hidden" name="action_login" value="1">
<table width="270">
<tr>
<td align="right"><label for="loginUserZ"><span
class="smallText"><b>Username:</b></span></label></td>
<td><input id="loginUserZ" class="smallText" type="text" size="16"
name="username" value=""></td>
</tr>
<tr>
<td align="right"><label for="loginPassZ"><span
class="smallText"><b>Password:</b></span></label></td>
<td><input id="loginPassZ" class="smallText" type="password" size="16"
name="password"></td>
</tr>
<tr>
<td></td>
<td>
<span><input type="submit" class="smallText" value="Login"></span> </td>
</tr>
</table>
</form>
As I have mentioned earlier I could login into the site when I manually type
the url in the browser
for example if the username is "useruser" and the password is "pass"
thant is
http://youtube.com/signup?username=useruser&password=pass¤t_form=loginForm
&action_login=1
now ican go to the cookies folder and check out the cookies
and I want it to be done by a program
Mr. Arnold - 13 Jan 2008 19:22 GMT
<http://www.netomatix.com/Development/HeaderProtocolviolation.aspx>
Maybe, your friend is Google.
Misbah Arefin - 13 Jan 2008 22:14 GMT
As Arne has replied the problem is you are using
req.Method = "POST"
but passing params as query string variables; try changing this to
req.Method = "GET"
and it should work

Signature
Misbah Arefin
> thanks for the replies.. Here is how the code of the webpage is
>
[quoted text clipped - 39 lines]
>
> and I want it to be done by a program