Saya mencoba melakukan hal berikut:
- kirim permintaan GET untuk mengambil halaman login (yang meminta nama pengguna, kata sandi, dan menyetel cookie)
- buat permintaan POST yang mengirimkan cookie dari #1 dan isi nama pengguna/kata sandi (ini mengembalikan Set-Cookie dan mengalihkan ke halaman arahan situs web untuk pengguna yang masuk)
Masalah saya adalah dengan pengalihan 302. Server web mengembalikan 302 dengan Set-Cookie
, tetapi ketika HttpWebRequests dialihkan secara otomatis, ia tidak meneruskan cookie yang sekarang diperbarui. Untuk menyiasatinya, saya mencoba menyetel .AllowAutoRedirect = false
, menyimpan cookie di CookieCollection, lalu membuat permintaan HTTP ke-3: GET ke lokasi 302 terakhir. Sayangnya, saya tidak dapat menyetel cookie pada permintaan ini. Saya tidak yakin mengapa dan itu membuat saya gila.
Permintaan HTTP, secara berurutan, diberi nama request, postRequest, redirectRequest.
string loginGetUrl = "https://<..>/signin.htm";
string loginPostUrl = "https://<..>/j_acegi_security_check";
string loginRedirectUrl = "https://<..>/centraladmin/poslinks.htm";
string postData = String.Format("j_username={0}&j_password={1}", username, password);
CookieCollection cookies = new CookieCollection();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginGetUrl);
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
//Get the response from the server and save the cookies from the first request..
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cookies = response.Cookies;
HttpWebRequest postRequest = (HttpWebRequest)WebRequest.Create(loginPostUrl);
postRequest.CookieContainer = new CookieContainer();
// Add the received Cookies from the HTTP Get
postRequest.CookieContainer.Add(cookies);
postRequest.Method = WebRequestMethods.Http.Post;
postRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
postRequest.AllowWriteStreamBuffering = false;
postRequest.ProtocolVersion = HttpVersion.Version11;
postRequest.AllowAutoRedirect = false;
postRequest.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
postRequest.ContentLength = byteArray.Length;
Stream newStream = postRequest.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
newStream.Close();
HttpWebResponse postResponse = (HttpWebResponse)postRequest.GetResponse();
// Save the cookies from the POST login request, then send them on to the redirected URL
cookies = postResponse.Cookies;
HttpWebRequest redirectRequest = (HttpWebRequest)WebRequest.Create(loginRedirectUrl);
redirectRequest.CookieContainer = new CookieContainer();
// add cookies from POST
redirectRequest.CookieContainer.Add(cookies);
HttpWebResponse redirectResponse = (HttpWebResponse)redirectRequest.GetResponse();
Di redirectRequest.CookieContainer.Add(cookies);
, objek cookies berisi cookie yang benar. Namun ketika saya melihat dengan Fiddler, saya hanya melihat info ini:
GET https://<...>/centraladmin/poslinks.htm HTTP/1.1
Host: host:port
Aku seperti membenturkan kepalaku ke dinding saat ini. Ada saran? Apakah saya merujuk sesuatu yang salah? Hati-hati, saya biasanya tidak menulis kode C#