Public async Task Post(Uri uri, byte data) Request.ContentType = "application/x-www-form-urlencoded" #NEWSTREAM ENTERPRISE CAREER WINDOWS#Request.UserAgent = "Mozilla/5.0 (compatible MSIE 10.0 Windows NT 6.2 WOW64 Trident/6.0)" Request.Accept = "text/html, application/xhtml+xml, */*" Private void SetHeaders(HttpWebRequest request) Var response = (HttpWebResponse) await request.GetResponseAsync() Var buffer = new System.IO.MemoryStream() īlockLength = stream.Read(block, 0, block.Length) Var stream = response.GetResponseStream() If (response.StatusCode = HttpStatusCode.Redirect) _cookies.Add(new Uri((UriPartial.Authority)), cookie) Private async Task DecodeResponse(HttpWebResponse response)įoreach ( cookie in response.Cookies) || (defaultValidator != null & defaultValidator(request, certificate, chain, sslPolicyErrors)) || (certificate.Subject = "CN=DRAC5 default certificate, OU=Remote Access Group, O=Dell Inc., L=Round Rock, S=Texas, C=US") (sender, certificate, chain, sslPolicyErrors) =>Ĭ("O=DO_NOT_TRUST, OU=Created by ") Request.ServerCertificateValidationCallback = Var request = HttpWebRequest.CreateHttp(uri) Private HttpWebRequest CreateRequest(Uri uri) Private readonly CookieContainer _cookies = new CookieContainer() I removed the async components as it wasn't necessary in my code. #NEWSTREAM ENTERPRISE CAREER CODE#I wasn't able to resolve this on my own, but did find a useful code snippet from this blog post by The code is on Github and I've attached it here for retention. Any suggestions? Am I referencing something wrong? Beware, I don't usually write C# code I'm kind of banging my head on the wall at this point. But when I look with Fiddler, I only see this info: GET HTTP/1.1 HttpWebResponse redirectResponse = (HttpWebResponse)redirectRequest.GetResponse() Īt (cookies), the cookies object contains the correct cookie. RedirectRequest.CookieContainer = new CookieContainer() HttpWebRequest redirectRequest = (HttpWebRequest)WebRequest.Create(loginRedirectUrl) Save the cookies from the POST login request, then send them on to the redirected URL HttpWebResponse postResponse = (HttpWebResponse)postRequest.GetResponse() NewStream.Write(byteArray, 0, byteArray.Length) // Send the data. Stream newStream = postRequest.GetRequestStream() //open connection PostRequest.ContentLength = byteArray.Length PostRequest.ContentType = "application/x. PostRequest.ProtocolVersion = HttpVersion.Version11 PostRequest.AllowWriteStreamBuffering = false Add the received Cookies from the HTTP Get PostRequest.CookieContainer = new CookieContainer() HttpWebRequest postRequest = (HttpWebRequest)WebRequest.Create(loginPostUrl) HttpWebResponse response = (HttpWebResponse)request.GetResponse() Get the response from the server and save the cookies from the first request. Request.CookieContainer = new CookieContainer() HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginGetUrl) string loginGetUrl = " string loginPostUrl = " string loginRedirectUrl = " string postData = String.Format("j_username=", username, password) ĬookieCollection cookies = new CookieCollection() The HTTP requests are, in order, named request, postRequest, redirectRequest. I'm not sure why and it's driving me mad. Unfortunately, I can't set the cookies on this request. AllowAutoRedirect = false, saving the cookies in a CookieCollection, then building a 3rd HTTP request: a GET to the final 302 location. The webserver is returning a 302 with a Set-Cookie, but when the HttpWebRequests auto-redirects, it doesn't pass along the now updated cookie.
2 Comments
2/17/2023 04:22:40 pm
Excellent article! Thank you for your excellent post, and I look forward to the next one.
Reply
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |