C# Login

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

C# Login

Post by ezah on Mon Mar 01, 2010 8:37 pm
([msg=35958]see C# Login[/msg])

I just started learning C#, i have pretty good programming concepts after about 7 years of experience so i should be able to do this, but i cant. How would i go about logging into HTS with HttpWebRequest?

I assume it uses cookies so i tried importing those, not sure if that's right, i've messed quite a bit with it but it still says im not logged in. At times i do see my username in the source, not logged in, but i do see it and other times i don't.

Here's a super basic part of my code and hopefully one of you can find a mistake i may be making? programatically or with the way HTS logs in.

Code: Select all
CookieContainer cooks = new CookieContainer;
            HttpWebRequest webR = (HttpWebRequest)WebRequest.Create("http://www.hackthissite.org/missions/prog/2");
            webR.CookieContainer = cooks;
            webR.Method = "Post";
            // tried both get and post methods.
            HttpWebResponse webRs = (HttpWebResponse) webR.GetResponse();
            System.IO.StreamReader str = new System.IO.StreamReader( webRs.GetResponseStream());


and dont comment about my naming conventions, i know i just wanted something quick to show :p
ezah
New User
New User
 
Posts: 29
Joined: Sat Feb 13, 2010 12:59 pm
Blog: View Blog (0)


Re: C# Login

Post by BhaaL on Tue Mar 02, 2010 12:26 pm
([msg=35970]see Re: C# Login[/msg])

You need to use a different url to log in, store the cookie, and then submit your answer. The cookie container is correct for the latter, but you still need to go to login first.
And you'll need to write your HTTP to the request stream you obtain from the HttpWebRequest object before getting the response.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: C# Login

Post by ezah on Tue Mar 02, 2010 6:04 pm
([msg=35980]see Re: C# Login[/msg])

Ok, but one how would i go about logging in? i have no experience at all with httpwebrequests. a web client see,s like it would be fine for this but from the research i have found it does not seem possible. also how would i store the cookie? ive tried adding phpsessid and its already included. im just lost with this whole thing, i actually wanna do this for hellbound hackers timed challenges because its not possible to do those in any other way. Thanks alot for the response but if you you can explain that a bit better it would be great.

im not asking for a script or anything, just some more detail.
ezah
New User
New User
 
Posts: 29
Joined: Sat Feb 13, 2010 12:59 pm
Blog: View Blog (0)


Re: C# Login

Post by BhaaL on Thu Mar 04, 2010 12:09 pm
([msg=36105]see Re: C# Login[/msg])

You dont need to take care of cookies yourself, the Cookie Container does that for your; just make sure to include the same container for all requests.
Just call GetRequestStream() on the Request object, and use whatever method you like to write to it (StreamWriter for example). Then, simply close the stream and call GetResponse() to get whatever the server sends back.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: C# Login

Post by ezah on Thu Mar 04, 2010 4:53 pm
([msg=36115]see Re: C# Login[/msg])

Ive tried to write some data, i just dont know the format hts uses for login data, i set method to post and tried posting "username=user&password=pass" and stuff along those lines, i have no network experience but alot of programming but i cant seem to get this and i've found some good sources of info on google. so does anybody know what i should post to login?

also i did this with the webBrowser control and it works but its too slow for what i need this for, with the webBrowser i can use my active session but with this im lost :/ Thanks for the help so far too :]
ezah
New User
New User
 
Posts: 29
Joined: Sat Feb 13, 2010 12:59 pm
Blog: View Blog (0)


Re: C# Login

Post by BhaaL on Fri Mar 05, 2010 9:09 am
([msg=36146]see Re: C# Login[/msg])

It works the same as for any other Website. Maybe google a bit for what you are trying to do (like, POST some form fields in C#) or look at RFC 2616 and related.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: C# Login

Post by prizzy on Fri Mar 09, 2012 10:46 pm
([msg=64917]see Re: C# Login[/msg])

Hey ezah, did you get this to work? I am learning programming c# via the challenges, and would like to make it turn key.
prizzy
New User
New User
 
Posts: 5
Joined: Tue Feb 09, 2010 10:19 pm
Blog: View Blog (0)


Re: C# Login

Post by centip3de on Sat Mar 10, 2012 12:28 am
([msg=64918]see Re: C# Login[/msg])

prizzy wrote:Hey ezah, did you get this to work? I am learning programming c# via the challenges, and would like to make it turn key.

-- Fri Mar 09, 2012 10:46 pm --

Hey ezah, did you get this to work? I am learning programming c# via the challenges, and would like to make it turn key.


This thread is almost over 2 years old. The OP hasn't been on this site since March, 15th, 2010. Please don't necro threads, like these.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1410
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: C# Login

Post by prizzy on Sat Mar 10, 2012 6:00 pm
([msg=64933]see Re: C# Login[/msg])

Not meaning to necro any threads. I figured I would got with the one that's started, instead of starting a new one. Perhaps I am not the only one working on this.

anyway..

I have found a way to do it with c#, but I don't like it. I want to complete the programming missions (the ones possible) with one click. Have the program, log into site, download info (picture, xml file, etc.), do the work, then submit it back. I don't like opening a webbrowser control to do it though.

Code: Select all
webBrowser1.Navigate("www.hackthissite.org/user/login");

            while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
            {
                MessageBox.Show("Loaded");

                HtmlDocument document = webBrowser1.Document;
                HtmlElement inputValue = document.GetElementById("login_username");
                inputValue.SetAttribute("value", "username");
                HtmlElement inputValuep = document.GetElementById("login_password");
                inputValuep.SetAttribute("value", "password");
              //  HtmlElement submitButton = document.GetElementById("submit");
               // HtmlElement submitButton =
                HtmlElementCollection tags = document.GetElementsByTagName("input");
                HtmlElement submitButton;

                foreach (HtmlElement tag in tags)
                {
                    richTextBox1.AppendText(tag.GetAttribute("type").ToString());
                    if (tag.GetAttribute("type").ToString() == "submit")
                    {
                        submitButton = tag;
                        submitButton.InvokeMember("click");
                    }
                }
prizzy
New User
New User
 
Posts: 5
Joined: Tue Feb 09, 2010 10:19 pm
Blog: View Blog (0)


Re: C# Login

Post by Mars7411 on Sat Mar 10, 2012 7:46 pm
([msg=64936]see Re: C# Login[/msg])

prizzy wrote:Not meaning to necro any threads. I figured I would got with the one that's started, instead of starting a new one. Perhaps I am not the only one working on this.

anyway..

I have found a way to do it with c#, but I don't like it. I want to complete the programming missions (the ones possible) with one click. Have the program, log into site, download info (picture, xml file, etc.), do the work, then submit it back. I don't like opening a webbrowser control to do it though.

Moron, can you read? This thread is nearly two years old. Don't post in this thread. Start a new thread.
Mars7411
Experienced User
Experienced User
 
Posts: 99
Joined: Thu Oct 27, 2011 7:09 pm
Blog: View Blog (0)


Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests