Always Getting an HTML in Response from RTC

211 views Asked by At

It seems like the RTC server doesn't like my url request. I can essentially add whatever I want after the "host" part of the url and get the same result. So I'm guessing something about what I've got is wrong. Following the answer from my previous post, I'm pretty sure I have the right url from the "services" file in the <oslc_cm:simpleQuery><dc:title>Change request queries</dc:title> tag. So I'm not sure if there's something else it doesn't like? It no longer fails authentication and I'm now using form-based rather than basic, so I don't think it's authentication-related. It just seems to ignore anything and everything but still knows my credentials aren't wrong. Any ideas?

Update: I've also tried swapping all the colons with %3A as the Jazz documentation didn't seem particularly consistent in their examples if that was necessary or not. Same results though.

        string host = "https://my.host.com:9443/ccm/";
        string item = host + "oslc/contexts/_MySp3ci4lK3Y/workitems?" +
                                  "oslc.where=dcterms:identifier=%222494443%22&" +
                                  "oslc.properties=dcterms:title,dcterms:identifier&" +
                                  "oslc.prefix=dcterms=%3Chttp://purl.org/dc/terms/%3E";
        Debug.Log("Request");
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(item);
        request.Accept = "application/json";
        request.Headers.Add("OSLC-Core-Version", "2.0");
        WebResponse response = request.GetResponse();
        string AuthHeader = response.Headers["X-com-ibm-team-repository-web-auth-msg"];
        //check if authentication has failed
        if ((AuthHeader != null) && AuthHeader.Equals("authrequired"))
        {
            Debug.Log("Authentication Required");
            HttpWebRequest _formPost = (HttpWebRequest)WebRequest.Create(host + "authenticated/j_security_check"); // Same response without the "authenticated/j_security_check"
            _formPost.Method = "POST";
            _formPost.Timeout = 30000;
            _formPost.Headers.Add("OSLC-Core-Version", "2.0");
            _formPost.CookieContainer = request.CookieContainer;
            _formPost.Accept = "text/xml";
            _formPost.ContentType = "application/x-www-form-urlencoded";

            Byte[] _outBuffer = Encoding.UTF8.GetBytes(credentials); //store in byte buffer
            _formPost.ContentLength = _outBuffer.Length;
            Stream _str = _formPost.GetRequestStream();
            _str.Write(_outBuffer, 0, _outBuffer.Length); //update form
            _str.Close();

            //FormBasedAuth Step2:submit the login form and get the response from the server
            HttpWebResponse _formResponse = (HttpWebResponse)_formPost.GetResponse();

            string _rtcAuthHeader = _formResponse.Headers["X-com-ibm-team-repository-web-auth-msg"];
            //check if authentication has failed
            if ((_rtcAuthHeader != null) && _rtcAuthHeader.Equals("authfailed"))
            {
                Debug.Log("Authentication Failed");
                return;
            }
            else
            {
                //login successful
              // *** Still says AuthRequired here for some reason ***
                Debug.Log("Auth Header = " + _rtcAuthHeader); 
                _formResponse.GetResponseStream().Flush();
                _formResponse.Close();
                //FormBasedAuth Step3: Resend the request for the protected resource.
                response = (HttpWebResponse)request.GetResponse();
            }
        }
        else if (AuthHeader == null)
        {
            Debug.Log("AuthHeader Null");
        }
        else
        {
            Debug.Log("AuthHeader = " + AuthHeader);
        }

        Debug.Log("Response Stream");
        Stream responseStream = response.GetResponseStream();
        byte[] buffer = new byte[BufferSize];
        int read;
        while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0)
        {
            // Prints out an HTML Doc rather than a JSON string.
            Debug.Log(Encoding.UTF8.GetString(buffer));
        }
1

There are 1 answers

0
CodeMonkey On BEST ANSWER

This is what I've come to understand.

The comment "// * Still says AuthRequired here for some reason *" is telling of the fact that authorization is indeed not occurring. The header value for "X-com-ibm-team-repository-web-auth-msg" will indeed be null when it is officially no longer required.

It is failing because:

  1. The _formPost itself needs basic authentication to post the form values
  2. The CookieContainer is null. Creating a new CookieContainer allows for the authentication to proceed.
  3. The "authenticated/j_security_check" is not correct. It should simply be "j_security_check".
  4. When requesting the data a second time after authenticating, a new request must be created and using the CookieContainer from the original.