after further digging, I got the solution in .netcore, hop it could help others.
///
/// EMC unity Api use cookies and redirect, but httpwebrequest will drop cookies when redirects, /// we have to do the process manually. /// /// /// private string GetRequestResponseWithRedirect(string url) { var result = string.Empty; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = _cookies;
//we will recall the redirected url by ourself request.AllowAutoRedirect = false;
HttpWebResponse response = null; try { response = (HttpWebResponse)request.GetResponse(); } catch (WebException ex) { if (ex.Message.Contains("302")) { var webResponse = (HttpWebResponse)ex.Response; if (webResponse.Cookies.Count > 0) { //save any cookies we may need for the recall _cookies.Add(webResponse.Cookies); }
//get the redirected url result = GetRequestResponseWithRedirect(ex.Response.Headers["Location"]); }
}
if (response != null) { // read the result when no errors. using (var stream = response.GetResponseStream()) { using (var reader = new StreamReader(stream)) { result = reader.ReadToEnd();
mi hui
1 Message
7416
1
Posted July 1st, 2019 01:00
I got the same issue.
Here's my solution:
It's solved my problem.