【问题标题】:Always Getting an HTML in Response from RTC总是从 RTC 获得 HTML 响应
【发布时间】:2020-07-22 07:58:23
【问题描述】:

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

更新:我还尝试用 %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));
        }

【问题讨论】:

    标签: c# httpwebrequest rational-team-concert ibm-jazz oslc


    【解决方案1】:

    这就是我的理解。

    评论“// * Still says AuthRequired here for some reason *”表明授权确实没有发生。 “X-com-ibm-team-repository-web-auth-msg”的标头值在正式不再需要时确实为空。

    失败是因为:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 2013-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多