2

我正在尝试创建与 RTC 的 .NET Web 应用程序集成,我将使用本文中定义的 RTC 变更管理服务插入新的工作项(特别是在“创建变更请求”中)。我能够在 services.xml 文件(/oslc/contexts/_0_iN4G09EeGGMqpyZT5XdQ/workitems/)中获取要使用的 URL,我的目标是使用 JSON 插入数据。

我的代码基本上如下:

CookieContainer cookies = new CookieContainer();
HttpWebRequest documentPost = (HttpWebRequest)WebRequest.Create(rtcServerUrl + "/oslc/contexts/_0_iN4G09EeGGMqpyZT5XdQ/workitems/order");//"Order" is the workitem name
documentPost.Method = "POST";
documentPost.CookieContainer = cookies;
documentPost.Accept = "application/json";
documentPost.ContentType = "application/x-oslc-cm-change-request+json";
documentPost.Timeout = TIMEOUT_SERVICO;
string json = "{ \"dc:title\":\"" + title + "\", \"rtc_cm:filedAgainst\": [     { \"rdf:resource\" : \"" + rtcServerUrl + "/resource/itemOid/com.ibm.team.workitem.Category/" + idCategory + "\"} ] }"; //dc:title and rtc_cm:filedAgainst are the only two mandatory data from the workitem I'm trying to create
using (var writer = new StreamWriter(documentPost.GetRequestStream()))
{
    writer.Write(json);
    writer.Flush();
    writer.Close();
}
Encoding encode = System.Text.Encoding.UTF8;
string retorno = null;
//Login
HttpWebRequest formPost = (HttpWebRequest)WebRequest.Create(rtcServerUrl +     "/j_security_check");
formPost.Method = "POST";
formPost.Timeout = TIMEOUT_REQUEST;
formPost.CookieContainer = request.CookieContainer;
formPost.Accept = "text/xml";
formPost.ContentType = "application/x-www-form-urlencoded";
String authString = "j_username=" + userName + "&j_password=" + password;     //create authentication string
Byte[] outBuffer = System.Text.Encoding.UTF8.GetBytes(authString); //store in byte buffer
formPost.ContentLength = outBuffer.Length;
System.IO.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();
var rtcAuthHeader = formResponse.Headers["X-com-ibm-team-repository-web-    auth-msg"];
//check if authentication has failed
if ((rtcAuthHeader != null) && rtcAuthHeader.Equals("authfailed"))
{
    //authentication failed. You can write code to handle the authentication     failure.
    //if (DEBUG) Console.WriteLine("Authentication Failure");
}
else
{
    //login successful
    HttpWebResponse responseRetorno =     (HttpWebResponse)request.GetResponse();
    if (responseRetorno.StatusCode != HttpStatusCode.OK)
        retorno = responseRetorno.StatusDescription;
    else
    {
        StreamReader reader = new     StreamReader(responseRetorno.GetResponseStream());
        retorno = "[Response] "  + reader.ReadToEnd();
     }
    responseRetorno.Close();
    formResponse.GetResponseStream().Flush();
    formResponse.Close();
}

正如我设法在其他论坛中搜索的那样,这应该足以创建工作项(我有一个非常相似的代码可以使用“” URL和 PUT 方法来更新工作项)。但是,请求的响应不是在 RTC 中创建工作项并给我一些带有项目标识符的响应,而是返回一个以"oslc_cm:next":"https:///oslc/contexts/_0_iN4G09EeGGMqpyZT5XdQ/workitems/%结尾的巨大 JSON 文件7B0%7D?oslc_cm.pageSize=50&_resultToken=_AAY50FEkEee1V4u7RUQSjA&_startIndex=50 。这是我直接从浏览器访问/oslc/contexts/_0_iN4G09EeGGMqpyZT5XdQ/workitems/时收到的 XML 的 JSON 表示,就像我试图在workitem 的集合(即使我使用的是 POST,而不是 GET)。

我也尝试使用 PUT 方法,但随后收到 405 状态码。

有人知道我在这里想念什么吗?我的方法是错误的,即使使用相同的方法我能够更新 RTC 中现有的工作项数据?

提前致谢。

4

0 回答 0