It's all about the answers!

Ask a question

Project area creation failed using C# and REST API for CCM 4.0.1


Ben Liu (3145) | asked Jun 27 '13, 9:36 p.m.
Hi there,

I using C# to create a project area in CCM using REST API. However, the program always fails without any project area creation.
The code snippet is pasted here.

static string login = "ben";

        static string password = "benxxx";

        const String AUTHREQUIRED = "X-com-ibm-team-repository-web-auth-msg";

 

        static CookieContainer GetCookiesContainer()

        {

            CookieContainer container = new CookieContainer();

            try

            {

                HttpWebRequest request = WebRequest.Create(new Uri("https://localhost:9443/ccm")) as HttpWebRequest;

                HttpWebResponse intialResponse = request.GetResponse() as HttpWebResponse;

               

                foreach (string cookietext in intialResponse.Headers["Set-Cookie"].Split(';'))

                {

                    try

                    {

                        string trimmedCookie = cookietext.Trim();

                        if (trimmedCookie.StartsWith("Path"))

                        {

                            continue;

                        }

 

                        if (trimmedCookie.Contains(","))

                        {

                            continue;

                        }

 

                        Cookie c = new Cookie(trimmedCookie.Split('=')[0], trimmedCookie.Split('=')[1]);

                        c.Domain = "localhost";

                        container.Add(c);

                    }

                    catch

                    {

                        //// Handles any other issues here. Log here if need.

                    }

                }

 

                intialResponse.Close();

               

            }

            catch (Exception ex)

            {

            }

            return container;

        }

 

        public static void Create_Proj_area()

        {

            try

            {

                string server = "https://localhost:9443/ccm";

 

                //first sample string from Jazz wiki

                string xml_content = @"<?xml version='1.0' encoding='UTF-8'?>" +

                @"<jp06:project-area xmlns:jp06='http://jazz.net/xmlns/prod/jazz/process/0.6/'" +

                @"jp06:name='testProjectAreaCreation4Ben' jp06:templateId='standard_setup_process' jp06:templateLocale='en_US'>" +

                @"<jp06:summary>The summary does not contain anything</jp06:summary>" +

                @"<jp06:description>Dummy project area</jp06:description>" +

                @"<jp06:visibility jp06:access='PUBLIC'/>" +

                @"<jp06:members xmlns:jp06='http://jazz.net/xmlns/prod/jazz/process/0.6/'>" +

                @"<jp06:member>" +

                @"<jp06:user-url>https://localhost:9443/jazz/users/ben</jp06:user-url>" +

                @"</jp06:member>" +

                @"<jp06:member>" +

                @"<jp06:user-url>https://localhost:9443/jazz/users/yonghui</jp06:user-url>" +

                @"</jp06:member>" +

                @"</jp06:members>" +

                @"</jp06:project-area>";

 

                // another sample for test

                string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><jp06:project-area jp06:name=\"ttttt333333\" jp06:templateId=\"scrum2.process.ibm.com\" jp06:templateLocale=\"en_US\" xmlns:jp06=\"http://jazz.net/xmlns/prod/jazz/process/0.6/\"><jp06:summary>ttttt333333</jp06:summary><jp06:description>dddd</jp06:description><jp06:access-public>false</jp06:access-public><jp06:project-admins /><jp06:members /></jp06:project-area>";

 

                string url = "https://localhost:9443/ccm/rootservices";

 

                string mediatype = "application/xml;Charset=UTF-8";

                CookieContainer cookies = GetCookiesContainer();

                HttpWebRequest formPost = (HttpWebRequest)WebRequest.Create(server + "/j_security_check");

                formPost.Method = "POST";

                formPost.Timeout = 30000;

                formPost.CookieContainer = cookies;

                formPost.ContentType = "application/x-www-form-urlencoded";

                String output = "j_username=" + login + "&j_password=" + password;

                Byte[] outputBuffer = Encoding.UTF8.GetBytes(output);

                formPost.ContentLength = outputBuffer.Length;

                Stream stream = formPost.GetRequestStream();

                stream.Write(outputBuffer, 0, outputBuffer.Length);

                stream.Close();

 

                HttpWebResponse formResponse = (HttpWebResponse)formPost.GetResponse();

 

                string header = formResponse.Headers[AUTHREQUIRED];

                if ((header != null) && header.Equals("authfailed"))

                {

                    // The login failed

                    throw new WebException("Authentication failed");

                }

                else

                {

                    formResponse.GetResponseStream().Flush();

                    formResponse.Close();

                }

                string catalog = "https://localhost:9443/ccm/process/project-areas";

                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(catalog);

               

                webRequest.Method = "POST";

                webRequest.KeepAlive = true;

                webRequest.Referer = server;

                webRequest.CookieContainer = GetCookiesContainer();

                webRequest.ContentType = "text/xml";

                webRequest.Accept = "application/xml";

 

                var parameterString = Encoding.UTF8.GetBytes(xml);

                webRequest.ContentLength = parameterString.Length;

                using (var buffer = webRequest.GetRequestStream())

                {

                    buffer.Write(parameterString, 0, parameterString.Length);

                    buffer.Close();

                }

                System.Net.WebResponse tempResponse = webRequest.GetResponse();

            }

            catch (Exception e)

            {

                Console.WriteLine(e.StackTrace);

            }

        }

The Jazz server and CCM versions are 4.0.1, they are installed in my local machine. The user in the program has the highest administrator permission (the user replaced "ADMIN").

The program follows the information in https://jazz.net/wiki/bin/view/Main/DraftTeamProcessRestApi#POST_project_areas_collection.

The error information after the highlighted part looks like this.
Error Info

I am wondering why "ResponseUri" contains "redirect" information, and the "ContentLength" is so long. I even dived in the "System.Net.HttpWebResponse" and found the method is STILL "GET".

Could someone help me to resolve this problem?

Best,
Ben

2 answers



permanent link
Martha (Ruby) Andrews (3.0k44251) | answered Jul 03 '13, 2:46 p.m.
JAZZ DEVELOPER
Hi Ben,

From the response url, it looks like you are not authenticated. The content probably contains the HTML for the login page
I do not know why the request is not showing up as authenticated, but that is the place to start investigating. (ie it is not a problem with the XML you are passing for project area creation). Possibly there is a header that is not getting copied, or cookies are missing.

This article describes accessing the Process REST API with Perl; I don't know of one specific to C3:
https://jazz.net/library/article/1086/

Hope that helps.
 
Ruby

Martha (Ruby) Andrews
Jazz L3 Developer

permanent link
Stephane Leroy (1.4k149) | answered Aug 06 '13, 12:58 p.m.
JAZZ DEVELOPER
Hi Ben,

you might want to check the C# samples provided in the following blog posts
"C# with Visual Studio and Rational Team Concert (OSLC revisited)":
http://blog.boriskuschel.com/2012/01/c-with-visual-studio-and-rational-team.html
http://blog.boriskuschel.com/2012/02/c-with-visual-studio-and-rational-team.html 

Hope this helps.

Regards,
Stéphane




Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.