繁体   English   中英

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

[英]C# HttpWebRequest to Scorm Cloud always returning error 400 bad request

我试图通过c#HttpWebRequest以JSON格式将Tin Can语句发布到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);
                }
        }
    }

我认为您缺少URL上的“声明”,您的请求应转到:

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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM