AnsweredAssumed Answered

What is a dotNet (C#) equivalent of the cURL command, to upload SIS feed files?

Question asked by guy.heatley on Nov 2, 2016
Latest reply on Nov 7, 2016 by guy.heatley

I am using C# to create a GUI app. It features the ability to upload an snapshot XML feed file to a BB server via SIS functionality. I have successfully used cURL to do this via automated shell scripts but would like to know how do achieve the same results via C#.

I have produced the code below, but it seems to work intermittently. BB doesn't provide useful error responses which makes it hard to debug. I haven't provided all the peripheral class definitions (BBServerResponse,BBCxnDetails) but they are simple containers and you can probably guess what they hold. Note the comment (in red). Only HTTP v1.0 seems to work, at all.

Any hints would be appreciated!

 

        public BBServerResponse UploadFeed(BBCxnDetails bcd)

        {

            BBServerResponse resp = new BBServerResponse("Error", HttpStatusCode.InternalServerError);

 

            if (!String.IsNullOrEmpty(this.FeedContainer)

                && !String.IsNullOrEmpty(bcd.userName))

            {

                try

                {

                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(bcd.locator);

 

                    req.Method = "POST";

                    req.ContentType = "text/xml";

                    // Using HTTP v1.0 seems to be important for Blackboard.

                    req.ProtocolVersion = HttpVersion.Version10;

                    req.KeepAlive = true;

                    req.Credentials = new NetworkCredential(bcd.userName, bcd.password);

 

                    using (var sw = new StreamWriter(req.GetRequestStream()))

                    {

                        sw.Write(FeedContainer);

                    }

 

                    using (var response = req.GetResponse())

                    {

                        Console.WriteLine(((HttpWebResponse)response).StatusDescription);

 

                        using (var sr = new StreamReader(response.GetResponseStream()))

                        {

                            resp = new BBServerResponse(sr.ReadToEnd(), ((HttpWebResponse)response).StatusCode);

                        }

 

                        if (((HttpWebResponse)response).StatusCode == HttpStatusCode.OK)

                            oa.addTimeStamp(FeedRec.Filename, FeedRec.SeqNo);

                    }

                    //Tidy up

                    req.Abort();

                }

                catch (Exception ex)

                {

                    resp = new BBServerResponse(ex.Message.ToString(), HttpStatusCode.InternalServerError);

                }

            }

            return resp;

        }

Outcomes