Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 762
  • Last Modified:

Using C# to Log into a PHP Page

Hi All,

I'm trying to use C# to log into a PHP webpage.  I've got the following code which gets me as far as requesting the HTML and removing problem characters but I don't know how to get username/password into the text boxes (auth_user & auth_pw) and click the submit login button (b_login):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;

namespace WebRippp
{
    public partial class Form1 : Form
    {

        public string source;
        public string url = "http://99.99.99.99/admin/";

        public Form1()
        {
            InitializeComponent();
        }

        private void btnGetSource_Click(object sender, EventArgs e)
        {
            //Create Cookie Container.
            CookieContainer cookies = new CookieContainer();
            
            //Set Proxy Server Info.
            setProxy();
            
            //Request Login Page.
            loginPage();


            string url = txtSource.Text;
        }

        private void loginPage()
        {
            //Request login page source.
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader stream = new StreamReader(response.GetResponseStream());
            source = stream.ReadToEnd();
            stream.Close();
            source = source.Replace("\n", "").Replace("\r", "").Replace("\t", "").Replace(" ", "");

            //Post
            [b]?????[/b]
        }

        private string ExtractViewState(string s)
        {
            string viewStateNameDelimiter = "__VIEWSTATE";
            string valueDelimiter = "value=\"";

            int viewStateNamePosition = s.IndexOf(viewStateNameDelimiter);
            int viewStateValuePosition = s.IndexOf(
                  valueDelimiter, viewStateNamePosition
               );

            int viewStateStartPosition = viewStateValuePosition +
                                         valueDelimiter.Length;
            int viewStateEndPosition = s.IndexOf("\"", viewStateStartPosition);
            string viewState = HttpUtility.UrlEncodeUnicode(
                     s.Substring(
                        viewStateStartPosition,
                        viewStateEndPosition - viewStateStartPosition
                     )
                  );
            return viewState;
        }

        private void setProxy()
        {
            System.Net.WebProxy pry = new System.Net.WebProxy("99.99.99.99", 8080);
            //The DefaultCredentials automically get username and password.
            pry.Credentials = CredentialCache.DefaultCredentials;
            GlobalProxySelection.Select = pry;
        }
    }
}

Open in new window



Help & Guidance much appreciated!

Thanks!
0
ImaCircularSaw
Asked:
ImaCircularSaw
2 Solutions
 
SubsonicDesignOfficialCommented:
You should use WebBrowser control automation to do that. It seems that you already know the IDs of TextBox controls and the Button so you only have to write a little code to automatically add the text into textboxes and perform a button click. Please take a look at the following article:

http://www.codeproject.com/KB/cs/mshtml_automation.aspx

I've created something very similar in the past and the article above helped me A LOT!
0
 
Russell_VenableCommented:
Using a Webbrowser control is the easiest way, but it does not give you full control over what your doing.

I would suggest that you buy the book: HTTP Programming Recipes for C# Bots. Its the best reference for this kind of work. Gets straight to the point as it becomes really difficult to give small solutions for this.

Besides if you get the book all you have to do is reference it :) Its a plus plus.

It will teach you  how to use get/post requests and parse the information and then display or use however you need it. Including posting to Forms, Multi-posts, etc.

If you dont want to go that route your going to need a few tools to do the job.

Tools:

1. Fiddler: [url="http://www.fiddler2.com/fiddler2/

Fiddler[/url]"]

2. Wireshark: [url="http://www.wireshark.org/

Wireshark[/url]"]

3. or A custom tool to read the Http get/post response



When you submit a form it sends a request like this.
POST /someForm.php?Value=someValue&auth_user=someUser&auth_pw=SomePassword&b_login

Open in new window

You are basically recreating the same method that those tools show you. After that you should be logged in and you can add CookieContainer to store your cookies to stay logged in, etc.
0
 
ImaCircularSawAuthor Commented:
I've not been able to complete this yet, however, both of your options seem good enough to me so I'll split the points fairly.

Thanks!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now