0
  1. Login and make new (cookie)session (systempage.local/login.php)
  2. if login was successful navigate to page and do something (navigate to systempage.local/index.php)

I'm trying to code an applikation for a site in the localnetwork.
login.php redirects to index.php usally. (in the browser)
i have my webrequest ready to log in to it. (it works, so far)

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;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    string sThepage;
    CookieContainer logincookie;

    private void button1_Click(object sender, EventArgs e)
    {
        string strPostData = "user_name=" + txtUser.Text + "&password=" + txtPass.Text + "&language=en&action%3Asubmit=Submit";
        CookieContainer tempCookies = new CookieContainer();

        ASCIIEncoding encoding = new ASCIIEncoding();
        byte[] data = encoding.GetBytes(strPostData);

        HttpWebRequest postReq = (HttpWebRequest)WebRequest.Create("system.local/Login.php");
        postReq.Method = "POST";
        postReq.KeepAlive = true;
        postReq.AllowAutoRedirect = false;
        postReq.Accept = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

        postReq.ContentType = "application/x-www-form-urlencoded";
        postReq.Referer = "system.local/interface/Login.php";
        postReq.UserAgent = "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1";
        postReq.ContentLength = data.Length;

        Stream postreqStream = postReq.GetRequestStream();
        postreqStream.Write(data, 0, data.Length);
        postreqStream.Close();

        HttpWebResponse postResponse;
        postResponse = (HttpWebResponse)postReq.GetResponse();
        tempCookies.Add(postResponse.Cookies);
        logincookie = tempCookies;

        StreamReader postRegReader = new StreamReader(postResponse.GetResponseStream());
        sThepage = postRegReader.ReadToEnd();

        // Blanksite if login success.
        if (!sThepage.Contains("!doctype"))
        {
            MessageBox.Show("you logged in");
            Navigate(); //Navigate to the index.php
        }
        else
        {
            MessageBox.Show("Client was unable to connect!");    

        }

        richTextBox1.Text = sThepage;
    }
}   
}

..

Now I need a way to navigate to the indexpage (and some other pages in the futher)

 private void Navigate()
 {
    // What to do here?
    // and how to keep my cookie(sesion) for the next page?
 }
MrMAG
  • 1,194
  • 1
  • 11
  • 34

1 Answers1

0

What you'd have to do is after the login request has completed, log the cookie(s) you're sent to a variable/file, and send those same cookies back again with each subsequent request. You can get the cookies out of the header object.

PhonicUK
  • 13,486
  • 4
  • 43
  • 62
  • variable `WebHeaderCollection myHeader;` ... `//first request (logiN) myHeader = postResponse.Headers; ...` Where should i add the header to? (newReg.cookiecontainer or headers?) `HttpWebRequest newReq = (HttpWebRequest)WebRequest.Create("system.local/interface/index.php"); newReq.Headers = myHeader;` ....Sorry, formatting doesn't work, hope you can understand it. :/ – MrMAG Aug 01 '12 at 10:13
  • The mainquestion is just where should i add the header/cookie to? `newReg.??? = myHeader;`... hope it's more understandable now :) – MrMAG Aug 01 '12 at 10:23