无法通过RTC中的webrequest创建工作项

时间:2017-06-14 17:44:30

标签: c# json httpwebrequest rtc rational-team-concert

我尝试使用RTC创建.NET Web应用程序集成,我将使用RTC变更管理服务插入新的工作项,如this article中所定义(具体在"创建变更请求&#34)。我能够在services.xml文件中找到要使用的URL( / oslc / contexts / _0_iN4G09EeGGMqpyZT5XdQ / workitems / ),我的目标是插入数据使用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结尾的巨大JSON文件:next& #34;:" HTTPS:/// OSLC /上下文/ _0_iN4G09EeGGMqpyZT5XdQ /工作项/%7B0%7D oslc_cm.pageSize = 50&安培; _resultToken = _AAY50FEkEee1V4u7RUQSjA&安培; _startIndex = 50 即可。它是我直接从浏览器访问 / oslc / contexts / _0_iN4G09EeGGMqpyZT5XdQ / workitems / 时收到的XML的JSON表示,就像我试图做一个简单的在工作项集合中查询(即使我使用的是POST,而不是GET)。

我也试过使用PUT方法,但后来收到405状态代码。

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

提前致谢。

0 个答案:

没有答案