473,480 Members | 1,625 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Log in to a website using C#

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
5 16380

"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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1635
by: js | last post by:
I am using Visual Studio 2003. An IIS is installed on a separate box with the Remote Debugger server component from VS2003. Initially, I created a new blank project in VS2003 under the IIS's...
5
1653
by: jd | last post by:
I am trying to setup a site using the Personal Website Starter Kit available in VS2005, I can debug my site just fine in Visual Studio. I am also running IIS and am able to connect to my new...
17
2254
by: stubbsie | last post by:
Hi, I have redesigned our official public government website in .net and it has taken me a few months to redo. I have been the sole designer of the website from its humble beginnning a few years...
5
2422
by: Tyler | last post by:
I am developing an application which will allow me to automatically sign into an external website. I can currently do a screen scrape using HTTPWEBREQUEST. However I want to just redirect to the...
19
3134
by: cpnet | last post by:
I'm using VS2005, C#, ASP.NET 2.0. I'm trying to create a report using SQL Reporting Services (to be used in local mode so I don't have to deal with SQL Server). When I create a new report in my...
17
2498
by: =?Utf-8?B?Y2F0aGFyaW51cyB2YW4gZGVyIHdlcmY=?= | last post by:
Hello, I have build a website with approximately 30 html-pages. When I search this website in Google, I see the index.html or home.html on this website, but also other html-pages on this...
7
7654
by: Atul | last post by:
Hi Theres a website that books hotels . user enters the information and according to that results are displayed to the user.Let it be website A. Now I want to create a new project with...
2
1659
by: dogged | last post by:
Website works locally but not when deployed ================================== Can someone please help with what I hope is a common problem (I’m new to .net). I have a simple website generated...
5
2987
by: daveh551 | last post by:
What, from a high level point of view, is the difference (in Visual Studio 2005) between Website (accessed with Open Website or Create Website from the StartPage) that is an ASP.NET Website, and a...
2
1607
by: Wayne Smith | last post by:
Applies to: Visual Studio 2008 Professional Hi everyone, I'm in the middle of creating a website with VS2008 and I'm trying to integrate a user registration/login section on the website but I've...
0
7067
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6925
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7078
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6780
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7065
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4808
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4512
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1325
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
221
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.