C#HttpWebRequest到Scorm Cloud总是返回错误400错误请求

时间:2014-08-26 17:42:39

标签: c# json httpwebrequest tin-can-api

我正尝试通过JSON格式的c#HttpWebRequest将Tin Can Statements发布到Scorm Cloud LRS。但是我总是得到错误400.验证是正确的,所以JSON有什么问题?我已尝试编码为UTF8,但仍然没有骰子

以下是所需的代码和凭据:

    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://cloud.scorm.com/ScormEngineInterface/TCAPI/RCFZ5D8GXU/sandbox/");
    httpWebRequest.ContentType = "application/json; charset=UTF-8";
    httpWebRequest.Method = "POST";
    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
    String autorization= "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("RCFZ5D8GXU222" + ":" + "YQA3VfX1NiuYkKXEEzkKu723NwejpwNkB6x0Vhg3"));

    httpWebRequest.Headers.Add("Authorization", autorization);
    httpWebRequest.Headers.Add("X-Experience-API-Version", "1.0.1");
    string jsonText = "{"+
                        "   \"actor\": {"+
                            "   \"mbox\": \"mailto:Steeno@gmail.com\","+
                            "   \"name\": \"Austin Glatt\","+
                            "   \"objectType\": \"Agent\""+
                            "},"+
                            "\"verb\": {"+
                            "   \"id\": \"http://adlnet.gov/expapi/verbs/attempted\","+
                            "   \"display\": {"+
                            "       \"en-US\": \"attempted\""+
                            "   }"+
                            "},"+
                            "\"object\": {"+
                            "   \"id\": \"http://www.example.com/tincan/activities/cMjKwAGI\","+
                            "   \"objectType\": \"Activity\","+
                            "   \"definition\": {"+
                            "       \"name\": {"+
                            "           \"en-US\": \"Part Removal\""+
                            "       },"+
                            "       \"description\": {"+
                            "           \"en-US\": \"On Engine 155\""+
                            "       }"+
                            "   }"+
                            "}"+
                        "}";
    byte [] jsonData = System.Text.Encoding.UTF8.GetBytes(jsonText);
    Debug.Log(System.Text.Encoding.UTF8.GetString(jsonData));
    httpWebRequest.ContentLength = jsonData.Length;
    using (var streamWriter = httpWebRequest.GetRequestStream())
    {
        streamWriter.Write(jsonData,0,jsonData.Length);
        streamWriter.Flush();
        streamWriter.Close();
    }


     try
        {
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                Debug.Log("POST result"+result);
            }
    }
    catch(WebException ex)
    {

        if (ex.Response != null)
        {
            Debug.Log(ex.Message);
            foreach(DictionaryEntry d in ex.Data)
                Debug.Log(d.ToString());

                 string errorDetail = string.Empty;
                using (StreamReader streamReader = new StreamReader(ex.Response.GetResponseStream(), true))
                {
                    errorDetail = streamReader.ReadToEnd();
                    Debug.Log(errorDetail);
                }
        }
    }

1 个答案:

答案 0 :(得分:1)

我认为您错过了网址上的“语句”,您的请求应该是:

https://cloud.scorm.com/ScormEngineInterface/TCAPI/RCFZ5D8GXU/sandbox/statements

如有人关心的原始答案:

您似乎发布了0.95或1.0.x语句,但您似乎没有设置X-Experience-API-Version标头,因此LRS可能会将其解释为0.9语句,该语句无效

我强烈建议使用库来构建请求以及请求的内容。我们在这里有一个:

http://rusticisoftware.github.io/TinCan.NET/