Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey fellow Cpians,

i have a problem with a connection to a Server with the REST.API. The code posted below is EXAMPLECODE.

As soon as i reach the point of client.DownloadData(...) it fails the Handshake producing the error "The connection was closed. Unexpected error while sending...".
The inner Exception is "Error on handshake, unexpected package format".

I don'T have any clue what the problem is since this is my first try with REST and C#.

BTW the URI String : "https://servername:8080/cb/rest/user/myuser/items/page/1?role=assigned to&status=Unresolved&pagesize=50&onlyDirect=False";

Additionally, when i put the URI into the firefox i recieve data, but the program crashes. So i guess this is a C# problem?

Thanks :)

EDIT:
I tried connecting without https:// and instead using only http://, this actually works but i don'T get any data back (as i expected). So is this some kind of SSL issue?

Finally:
Thanks Sacha for your info, i'll keep that in mind for the future, for now i just tried again with http:// in the app. If i replace downloadData with downloadString it works. Now i have to get the "DataDownload" correct and it'll be fine.

What I have tried:

C#
static void Main(string[] args)
        {
            ProgramArguments arguments = parseArguments(args);

            if (arguments.Debug)
            {
                printOutArguments(arguments);
            }

            int pageNum = 1;
            bool ready = false;

            // init web client
            WebClient client = getWebClient(arguments);

            // init input serializer
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ResultObject));

            List<WorkItem> workItems = new List<WorkItem>();

            // fetch all isses using paging while all issues is listed
            while (!ready)
            {
                // create data fetch URL onlyDirect
                StringBuilder urlBuilder = new StringBuilder(500);
                urlBuilder.Append(arguments.Endpoint).Append("/cb/rest/user/").Append(arguments.UserName).
                    Append("/items/page/").Append(pageNum).Append("?").Append("role=assigned to").
                    Append("&status=Unresolved").Append("&pagesize=").Append(arguments.PageSize).Append("&onlyDirect=").Append(arguments.OnlyDirect);

                try
                {
                    // fetch the data from the created URL
                    using (MemoryStream input = new MemoryStream(client.DownloadData(urlBuilder.ToString())))
                    {
                        ResultObject resultObject = (ResultObject)serializer.ReadObject(input);

                        // add fetched issues to result list
                        if (resultObject.items != null)
                        {
                            foreach (Item item in resultObject.items)
                            {
                                IItemConverter converter = getItemConverter(arguments);
                                workItems.Add(converter.Convert(item));
                            }
                        }

                        // check is all of the items is downloaded
                        ready = (pageNum * arguments.PageSize) > resultObject.total;
                        // go to next page
                        pageNum++;
                    }
                }
                catch (Exception e)
                {
                    if (arguments.Debug)
                    {
                        Console.WriteLine("Error: {0}", e.ToString());
                    }
                    ready = true;
                }
            }
            if (arguments.Debug)
            {
                Console.WriteLine(workItems.Count + " issue fetched from codeBeamer!");
            }
            StreamWriter writer = null;
            try
            {
                writer = getOutputWriter(arguments);
                // generate output via providers
                OutputProviderFactory.GetProvider(arguments).GenerateOutput(workItems, writer);
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }
        }
Posted
Updated 19-May-16 3:44am
v3

1 solution

have a look here I show loads of different ways to use .NET to do REST calls, there is another way shown in the comments:

JSON API[^]
 
Share this answer
 
Comments
HobbyProggy 19-May-16 9:52am    
Thank you +5 :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900