这是怎么回事?
我正在尝试向 OWA 网站发出请求并登录用户帐户。以前有人试过吗?我的应用程序将是一个控制台应用程序,主要思想是检查 OWA 在比较 HTML 响应时是否工作正常。
我收到了 POST 请求,但在那之后我有一个重定向(代码 302)并且我没有得到 HTML rigth 的响应。
我的代码:
//GET
CookieCollection cookies = new CookieCollection();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://domain/owa");
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
//Get the response from the server and save the cookies from the first request..
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cookies = response.Cookies;
// POST
string getUrl = "https://domain/owa/auth.owa";
string postData = "destination=https%3A%2F%2Fdomain%2Fowa%2Fauth.owa&flags=0&forcedownlevel=0&trusted=0&username=myuser&password=mypassword&isUtf8=1";
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
getRequest.CookieContainer = new CookieContainer();
getRequest.CookieContainer.Add(cookies); //recover cookies First request
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
Stream newStream = getRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
response = (HttpWebResponse)getRequest.GetResponse();
之后,我的回复有一个 html 页面,说我需要允许 javascript 运行它。但我了解 HTTPWerRequest 对象无法运行 js。
有谁知道如何解决这个问题?