1

HTTPリクエストを使用して、次のページにログインします:https ://secure.bodytel.com/de/mybodytel.html 。その後、設定を読みたいと思います(https://secure.bodytel.com/de/mybodytel/settings.html)。これが私の問題です。2番目のリクエストを送信すると、ページにログインしなくなったと表示されます。fiddlerでデバッグすると、2つの異なるCookieIDがあることがわかりました。では、どうすればCookieを存続させることができますか?または私のリクエストでそれを送信します。

私はすでにこの投稿を読んだ:「http://stackoverflow.com/questions/11596378/getting-a-page-source-after-post-variables-have-been-sent」が、それは私をあまり助けませんでした。

これが私のコードです:using System.Text; System.Windowsを使用します。System.Windows.Controlsを使用する; System.Windows.Formsを使用します。System.Netを使用します。System.IOを使用します。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;
    }


}

}
4

2 に答える 2

2

リクエストごとにCookieを送信する必要があります。そうしないと、サーバーはユーザーが誰であるかを把握できません。

于 2012-09-06T12:32:31.473 に答える
1

分かりました。Gauravにご協力いただきありがとうございます。

コードは次のとおりです。

 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();
    }
于 2012-09-07T07:41:30.137 に答える