示例#1
0
        public async Task <StoreData.Response> Save(string sessCode,
                                                    string dataBody, StoreData.Type dataType,
                                                    string dataName, int dataIndex = 1)
        {
            var resp = new StoreData.Response()
            {
                status = StoreError.E_CHAOS.ToString()
            };

            Log(m_apiUrl);
            Log(m_apiKey);

            using (var client = new HttpClient()){
                try {
                    var request = new HttpRequestMessage(HttpMethod.Post, m_apiUrl);
                    request.Content = new StringContent
                                      (
                        JsonSerializer.ToJsonString(new StoreData.Request()
                    {
                        role      = "server",
                        mode      = "save",
                        sessCode  = sessCode,
                        dataName  = dataName,
                        dataIndex = dataIndex,
                        data      = dataBody,
                        dataType  = (int)dataType
                    })
                        , new UTF8Encoding()
                        , dataType.ToContentType()
                                      );

                    request.Headers.Add(@"x-api-key", m_apiKey);

                    var response = await client.SendAsync(request);

                    var responseContent = await response.Content.ReadAsStringAsync();

                    if (!String.IsNullOrEmpty(responseContent))
                    {
                        Log("Got Resp >> " + responseContent);
                        resp = JsonSerializer.Deserialize <StoreData.Response>(responseContent);
                    }
                    else
                    {
                        resp.status = StoreError.E_CRITICAL.ToString();
                    }
                }
                catch (Exception e) {
                    Log("Exception : " + e.ToString());
                    resp.status = StoreError.E_CRITICAL.ToString();
                }
            }
            return(resp);
        }