简体   繁体   中英

Keep login data alive after HTTP-request login

with a HTTP request I'm login into the page: https://secure.bodytel.com/de/mybodytel.html . After that I want to read the settings (https://secure.bodytel.com/de/mybodytel/settings.html). So here's my problem: I send a second request and the page tells me I'm not logged in anymore. When I debug things with fiddler I saw that I have two different cookie IDs. So, how can I keep the cookie alive? or send it with my request.

I already read this post: "http://stackoverflow.com/questions/11596378/getting-a-page-source-after-post-variables-have-been-sent" but it didn't helped me very much.

Here's my code: using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Forms; using System.Net; using System.IO; using System.Web;

namespace BodytelConnection
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{


    public MainWindow()
    {
        InitializeComponent();
    }


    private void loginBtn_Click(object sender, RoutedEventArgs e)
    {

        #region username stuff
        string benutzername = textBox_benutzername.ToString();
        string passwort = textBox_passwort.ToString();
        passwort = changeString(passwort);
        benutzername = changeString(benutzername);
        #endregion username stuff


        CookieCollection cookies = new CookieCollection();
        CookieContainer container = new CookieContainer();


        #region login
        HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create("https://secure.bodytel.com/de/mybodytel.html");
        getRequest.Method = WebRequestMethods.Http.Post;
        getRequest.AllowWriteStreamBuffering = true;
        getRequest.AllowAutoRedirect = true;
        getRequest.ContentType = "application/x-www-form-urlencoded";
        byte[] byteArray = Encoding.ASCII.GetBytes("login=" + benutzername + "&password=" + passwort + "&step=login");
        getRequest.ContentLength = byteArray.Length;
        Stream newStream = getRequest.GetRequestStream();
        newStream.Write(byteArray, 0, byteArray.Length);
        newStream.Close();
        HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
        StreamReader sr = new StreamReader(getResponse.GetResponseStream());
        // here im trying to save the cookie
        container.Add(getResponse.Cookies);
        getRequest.CookieContainer = container;
        string source = sr.ReadToEnd();
        // save the html
        StreamWriter myWriter = File.CreateText(@"C:\Users\nicholas\Documents\Visual Studio 2010\Projects\BodytelConnection\BodytelConnection\bin\\Debug\test.txt");
        myWriter.Write(source);
        myWriter.Close();
        #endregion login



        //read the settings 
        #region readSettings
        getRequest.Method = WebRequestMethods.Http.Get;
        getRequest = (HttpWebRequest)WebRequest.Create("https://secure.bodytel.com/de/mybodytel/settings.html");         
        getRequest.AllowWriteStreamBuffering = true;
        getResponse = (HttpWebResponse)getRequest.GetResponse();
        sr = new StreamReader(getResponse.GetResponseStream());
        source = sr.ReadToEnd();
        StreamWriter myWriter2 = File.CreateText(@"C:\Users\nicholas\Documents\Visual Studio 2010\Projects\BodytelConnection\BodytelConnection\bin\\Debug\test2.txt");
        myWriter2.Write(source);
        myWriter2.Close();
        getResponse.Close();
        #endregion readSettings
    }

    private string changeString(string myString)
    {

        myString = myString.Replace("System.Windows.Controls.TextBox: ", "");
        return myString;
    }


}

}

您应该在每个请求中发送cookie,否则服务器无法确定谁是用户

I got it now. Thanks for your help Gaurav.

Here's the code:

 private void loginBtn_Click(object sender, RoutedEventArgs e)
    {

        #region username stuff
        string benutzername = textBox_benutzername.ToString();
        string passwort = textBox_passwort.ToString();
        passwort = changeString(passwort);
        benutzername = changeString(benutzername);
        #endregion username stuff

        // get the first cookie

        CookieContainer cookies = new CookieContainer();
        CookieContainer cookieContainer = new CookieContainer();
        HttpWebRequest sessionRequest = (HttpWebRequest)WebRequest.Create("http://www.bodytel.com/");
        sessionRequest.CookieContainer = new CookieContainer();
        cookies = sessionRequest.CookieContainer;
        HttpWebResponse sessionResponse = (HttpWebResponse)sessionRequest.GetResponse();
        sessionResponse.Close();

        // login 

        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://secure.bodytel.com/de/mybodytel.html");
        req.CookieContainer = cookieContainer;
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        byte[] byteArray = Encoding.ASCII.GetBytes("login=" + benutzername + "&password=" + passwort + "&step=login");
        req.ContentLength = byteArray.Length;
        Stream newStream = req.GetRequestStream();
        newStream.Write(byteArray, 0, byteArray.Length);
        newStream.Close();
        HttpWebResponse res = (HttpWebResponse)req.GetResponse();

        // now connect to the other link

        req = (HttpWebRequest)HttpWebRequest.Create("https://secure.bodytel.com/de/mybodytel/settings.html");
        req.CookieContainer = cookieContainer;
        req.Method = "GET";
        res = (HttpWebResponse)req.GetResponse();
        StreamReader sr = new StreamReader(res.GetResponseStream());
        string source = sr.ReadToEnd();
        StreamWriter myWriter = File.CreateText(@"C:\Users\nicholas\Documents\Visual Studio 2010\Projects\BodytelConnection\BodytelConnection\bin\\Debug\test.txt");
        myWriter.Write(source);
        myWriter.Close();
        res.Close();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM