始终从 RTC 获得 HTML 响应

Always Getting an HTML in Response from RTC

RTC 服务器似乎不喜欢我的 url 请求。我基本上可以在 url 的 "host" 部分之后添加我想要的任何内容,并获得相同的结果。所以我猜我得到的东西是错误的。按照我之前 的回答,我很确定我在 <oslc_cm:simpleQuery><dc:title>Change request queries</dc:title> 标签中的 "services" 文件中有正确的 url。所以我不确定是否还有其他不喜欢的东西?它不再使身份验证失败,而且我现在使用的是基于表单的而不是基本的,所以我认为它与身份验证无关。它似乎忽略了任何事情,但仍然知道我的凭据没有错。有什么想法吗?

更新:我也试过用 %3A 交换所有冒号,因为 Jazz documentation 在他们的示例中似乎并不特别一致,无论是否有必要。虽然结果相同。

        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));
        }

我是这么理解的。

评论“// * 由于某种原因这里仍然说 AuthRequired *”说明授权确实没有发生。当正式不再需要时,"X-com-ibm-team-repository-web-auth-msg" 的 header 值确实将为 null。

失败是因为:

  1. _formPost 本身需要对 post 表单值进行基本身份验证
  2. CookieContainer 为空。创建一个新的 CookieContainer 允许进行身份验证。
  3. "authenticated/j_security_check" 不正确。它应该只是 "j_security_check"。
  4. 验证后第二次请求数据时,必须创建一个新请求并使用原始请求中的 CookieContainer。