R
Rajkiran R.B.
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
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