ฉันกำลังพยายามทำสิ่งต่อไปนี้:
- ส่งคำขอ GET เพื่อดึงข้อมูลหน้าเข้าสู่ระบบ (ซึ่งจะแจ้งชื่อผู้ใช้ รหัสผ่าน และตั้งค่าคุกกี้)
- สร้างคำขอ POST ที่ส่งคุกกี้จาก #1 และเนื้อหาของชื่อผู้ใช้/รหัสผ่าน (ซึ่งจะส่งคืน Set-Cookie และเปลี่ยนเส้นทางไปยังหน้า Landing Page ของเว็บไซต์สำหรับผู้ใช้ที่เข้าสู่ระบบ)
ปัญหาของฉันอยู่ที่การเปลี่ยนเส้นทาง 302 เว็บเซิร์ฟเวอร์ส่งคืน 302 ด้วย Set-Cookie
แต่เมื่อ HttpWebRequests เปลี่ยนเส้นทางอัตโนมัติ จะไม่ส่งผ่านคุกกี้ที่อัปเดตในขณะนี้ เพื่อหลีกเลี่ยงปัญหาดังกล่าว ฉันกำลังพยายามตั้งค่า .AllowAutoRedirect = false
บันทึกคุกกี้ใน CookieCollection จากนั้นสร้างคำขอ HTTP ลำดับที่ 3: GET ไปยังตำแหน่ง 302 สุดท้าย ขออภัย ฉันไม่สามารถตั้งค่าคุกกี้ตามคำขอนี้ได้ ฉันไม่แน่ใจว่าทำไม และมันทำให้ฉันเป็นบ้า
คำขอ HTTP ตามลำดับ คำขอที่มีชื่อ postRequest, เปลี่ยนเส้นทางคำขอ
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();
ที่ redirectRequest.CookieContainer.Add(cookies);
วัตถุคุกกี้ประกอบด้วยคุกกี้ที่ถูกต้อง แต่เมื่อฉันดูด้วย Fiddler ฉันเห็นเพียงข้อมูลนี้:
GET https://<...>/centraladmin/poslinks.htm HTTP/1.1
Host: host:port
ตอนนี้ฉันกำลังเอาหัวโขกกำแพงอยู่ มีข้อเสนอแนะอะไรบ้าง? ฉันกำลังอ้างอิงถึงสิ่งผิดหรือเปล่า? ระวัง ปกติฉันจะไม่เขียนโค้ด C#