By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

Log in to a website using C#

P: n/a
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 + "&current_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\ContentExt ractor\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(MouseEventAr gs 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.O nMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message&
m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG&
msg)
at
System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoop(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\ContentExt ractor\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.Run UsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context( Object state)
at System.Threading.ExecutionContext.Run(ExecutionCon text
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 + "&current_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


Jan 13 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Rajkiran R.B." <ra*********@hotmail.comwrote in message
news:DA**********************************@microsof t.com...
>
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.

Jan 13 '08 #2

P: n/a
Rajkiran R.B. wrote:
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 + "&current_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();
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\ContentExt ractor\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 + "&current_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
Jan 13 '08 #3

P: n/a
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=u...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

Jan 13 '08 #4

P: n/a

"Rajkiran R.B." <ra*********@hotmail.comwrote in message
news:D1**********************************@microsof t.com...
<http://www.netomatix.com/Development/HeaderProtocolviolation.aspx>
Maybe, your friend is Google.

Jan 13 '08 #5

P: n/a
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
--
Misbah Arefin

"Rajkiran R.B." wrote:
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=u...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
Jan 13 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.