使用HTTPWebRequest访问OWA

时间:2013-01-24 12:30:12

标签: c# httpwebrequest httpwebresponse outlook-web-app

怎么了?

我正在尝试向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 objet无法运行js。

有人知道如何解决这个问题吗?

0 个答案:

没有答案