System.Net.WebException。远程服务返回错误。 (400)错误的要求

时间:2019-10-03 15:34:09

标签: firebase-realtime-database

我想首先从Firebase获取数据, 然后将包含获得的数据的数据写入Firebase。

我使用“ GET”方法首先获取数据。 然后使用“ PATCH”方法写入数据。

我得到一个错误,上面写着'System.Net.WebException。远程服务返回错误。 (400)错误的请求。在代码上'var response2 = request2.GetResponse();'

任何建议将不胜感激。

 public void GetDataFromFirebase()
    {
        var request = WebRequest.CreateHttp("https://datafrom.firebaseio.com/current_userID.json");

        request.Method = "GET";
        request.ContentType = "json";
        var response = request.GetResponse();
        var json = (new StreamReader(response.GetResponseStream())).ReadToEnd();
       JObject data = JObject.Parse(json);

        string userid = data["userID"].Value<string>();

        System.Console.WriteLine(userid);
        CurrentUserID = userid;
    }
    public string generateID()
    {
        return Guid.NewGuid().ToString("N");
    }

    public void SendDataToFirebase()
    {

        Shimmer.UICallback += this.HandleEvent;
        System.Net.ServicePointManager.Expect100Continue = false;
        while (true)
        {
            DateTime date = DateTime.Now;
            String Date = date.ToString("");

            String Acceleration_X = AccelX.ToString("");
            String Acceleration_Y = AccelY.ToString("");
            String Acceleration_Z = AccelZ.ToString("");
            String Value_GSR = GSR.ToString("");
            String Value_PPG = PPG.ToString("");
             // Current User = CurrentUserID.ToString("");

            string jsondata2 = @"{" + generateID() + ":{'Accel_X': " + Acceleration_X + ",'Accel_Y': " + Acceleration_Y + ", 'Accel_Z':  " + Acceleration_Z + ", 'ValueOfHR': " + HeartRate + ",'ValueOfGSR': " + Value_GSR + ", 'ValueOfPPG': " + Value_PPG + ", 'Time': " + time + ", 'UserID': " + CurrentUserID  + ", 'Date': '" + DateTime.Today.ToString("MM-dd-yyyy")+"',}}";
            JObject data2 = JObject.Parse(jsondata2);
            //JObject jsonObject = JObject.FromObject(jsondata1);

            string json2 = JsonConvert.SerializeObject(jsondata2);
            var request2 = WebRequest.CreateHttp("https://datafrom.firebaseio.com/Shimmer-data/" + CurrentUserID + ".json"); 

            request2.Method = "PATCH";
            request2.ContentType = "json";
            var buffer = Encoding.UTF8.GetBytes(json2);
            request2.ContentLength = buffer.Length;
            request2.GetRequestStream().Write(buffer, 0, buffer.Length);
            var response2 = request2.GetResponse();

            json2 = (new StreamReader(response2.GetResponseStream())).ReadToEnd();

            time++;
           }
    }

0 个答案:

没有答案