0

私はそれを何時間も機能させようとしてきましたが、ほとんど成功していません。最初のリクエスト後のソースから、ログインが成功したことを確認できますが、Cookie を使用して他のアクセスにログイン情報を要求し続けます。

アクセスしようとしているサイトは次のとおりです。http://gpro.net/

私が使用しているコードは次のとおりです。// 注: これは、フォームの URL ではなく、フォームの POST 先の URL です (これは、HTML のフォーム タグ文字列の "action" 属性で見つけることができます formParams = "textLogin=user&textPassword=pass&Logon=Login&LogonFake=Sign+in ";

        string strResponse;
        HttpWebRequest requestLogin = (HttpWebRequest)WebRequest.Create(formUrl);
        requestLogin.Method = "POST";
        requestLogin.CookieContainer = cookieJar;
        requestLogin.ContentType = "application/x-www-form-urlencoded";

        requestLogin.ContentLength = formParams.Length;
        StreamWriter stOut = new StreamWriter(requestLogin.GetRequestStream(), System.Text.Encoding.ASCII);
        stOut.Write(formParams);
        stOut.Close();

        HttpWebResponse responseLogin = (HttpWebResponse)requestLogin.GetResponse();
        StreamReader stIn = new StreamReader(responseLogin.GetResponseStream());
        strResponse = stIn.ReadToEnd();
        stIn.Close();
        sb.Append(strResponse);
        sb.AppendLine();
        sb.AppendLine();
        sb.AppendLine();
        //Add cookies to CookieJar (Cookie Container)
        foreach (Cookie cookie in responseLogin.Cookies)
        {
            cookieJar.Add(new Cookie(cookie.Name.Trim(), cookie.Value.Trim(), cookie.Path, cookie.Domain));
            sb.AppendLine(cookie.Name.ToString() + Environment.NewLine + cookie.Value.ToString() + Environment.NewLine + cookie.Path.ToString() + Environment.NewLine + cookie.Domain.ToString());
        }

        //Grab the cookie we just got back for this specifc page

        string cookies = cookieJar.GetCookieHeader(requestLogin.RequestUri);


        //put it back in the cookie container for the whole server

        cookieJar.SetCookies(new Uri("http://www.gpro.net/"), cookies);

        string testResults = string.Empty;
        HttpWebRequest runTest = (HttpWebRequest)WebRequest.Create("http://www.gpro.net/pl/RaceAnalysis.asp");
        runTest.Referer = "http://gpro.net/pl/gpro.asp";
        runTest.CookieContainer = cookieJar;
        //runTest.Method = "POST";
        //runTest.ContentType = "application/x-www-form-urlencoded";
        //StreamWriter stOut2 = new StreamWriter(runTest.GetRequestStream(), System.Text.Encoding.ASCII);
        //stOut2.Write(formParams);
        //stOut2.Close();
        StreamReader stIn2 = new StreamReader(runTest.GetResponse().GetResponseStream());
        testResults = stIn2.ReadToEnd();
        stIn2.Close();
        sb.Append(testResults);
        e.Result = sb.ToString();
4

1 に答える 1

0

私は上記のコードとスタックオーバーフローに投稿されたすべての同様のコードの何が問題であるかを発見しました(私はすべてが機能しているとマークされてみました)。ブラウザリダイレクトでログを記録していたときは正常に機能しましたが、上記の方法を使用すると、リクエストは同じアドレスにリダイレクトされましたが、wwwプレフィックスはありませんでした。その結果、2つの異なるCookieコレクションが作成されました。ログインの問題を解決したすべてのリクエストからwwwプレフィックスを削除しました。

于 2012-09-10T13:03:40.197 に答える