Log in or register to post comments

Error 400 Bad Request

February 1, 2018 - 8:28pm #1

Hi folks,

I'm trying to use de Vuforia API but the code of error of the response is "Error 400 bad request",  this is my code if some one can help me, pls!!!

 

 

 string WEBSERVICE_URL = Configuracion.url;
            string strDate = string.Format("{0:r}", DateTime.Now.AddHours(0));
            try
            {
                var webRequest = System.Net.WebRequest.Create(WEBSERVICE_URL);
                if (webRequest != null)
                {
                    webRequest.Method = "GET";
                    webRequest.Timeout = 12000;
                    webRequest.ContentType = "application/json";
                   
                    string strSign = CrearStringToSign(webRequest);
                    string sendWsVuforia = "VWS {" + Configuracion.accessKey + "}:{" + strSign + "}";
                    webRequest.Headers.Add("Authorization", sendWsVuforia);
                    using (System.IO.Stream s = webRequest.GetResponse().GetResponseStream())
                    {
                        using (System.IO.StreamReader sr = new System.IO.StreamReader(s))
                        {
                            var jsonResponse = sr.ReadToEnd();
                            Console.WriteLine(String.Format("Response: {0}", jsonResponse));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            return "";
        }

        public string CrearStringToSign(System.Net.WebRequest webReq) {
            string HTTPVerb = webReq.Method;
            string ContentMD5 = "d41d8cd98f00b204e9800998ecf8427e";
            string ContentType = webReq.ContentType;
            string Date = string.Format("{0:r}", DateTime.Now.AddHours(-6));
            string RequestPath = webReq.RequestUri.ToString();
            string StringToSign = HTTPVerb + "\n" + ContentMD5 + "\n" + ContentType + "\n" + Date + "\n" +RequestPath;
            byte[] key = Encoding.ASCII.GetBytes(Configuracion.secretKey);
            string strEncode = Encode(StringToSign, key);
            strEncode = Base64Encode(strEncode);
            return strEncode;
        }

        public string Encode(string input, byte[] key)
        {
            HMACSHA1 myhmacsha1 = new HMACSHA1(key);
            byte[] byteArray = Encoding.ASCII.GetBytes(input);
            MemoryStream stream = new MemoryStream(byteArray);
            return myhmacsha1.ComputeHash(stream).Aggregate("", (s, e) => s + String.Format("{0:x2}", e), s => s);
        }

        public static string Base64Encode(string plainText)
        {
            var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
            return System.Convert.ToBase64String(plainTextBytes);
        }
        public static string Base64Decode(string base64EncodedData)
        {
            var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
            return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
        } 

 

thank you in advance!!

Log in or register to post comments