1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | private QResult FetchAPIData(string urlRelativePath, HttpMethod httpMethod, HttpContent contentToSend) { QResult result = new QResult(); //add auth token if we have a user and we arent deleting if (User != null && httpMethod !=HttpMethod.Delete) { urlRelativePath = urlRelativePath + "&access_token=" + User.id; } string url = _baseUrl + urlRelativePath; Debug.WriteLine("Performing " + httpMethod.ToString() + " to " + url); if (contentToSend!=null) { Debug.WriteLine("Sending data " + contentToSend.ReadAsStringAsync().Result); } HttpRequestMessage request = new HttpRequestMessage(httpMethod, url); request.Content = contentToSend; if (User != null) request.Headers.Add("X-Access-Token", User.id); using (HttpResponseMessage response = _client.SendAsync(request).Result) if (response.IsSuccessStatusCode) { using (HttpContent content = response.Content) { // ... Read the string. result.Message = content.ReadAsStringAsync().Result; result.Success = response.IsSuccessStatusCode; } } else { result.Message = response.ReasonPhrase; result.Success = false; } return result; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | private QResult PerformLogin() { if (_token == string.Empty) throw new Exception("A token is required."); QResult result = new QResult(); HttpContent content= new StringContent("apiToken=" + _token, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded");//CONTENT-TYPE header result = FetchAPIData("/users/loginWithToken", HttpMethod.Post, content); if (result.Success) { User = JsonConvert.DeserializeObject<QUser>(result.Message); Debug.WriteLine("Logged in and have UserID: " + User.userid); } else { User = null; } return result; } |
1 2 3 | "{\"id\":\"XXXXXXXXXXXXXXXXXXXXXXXXXX\",\"ttl\":1209600,\"created\": \"2017-10-03T14:51:51.918Z\",\"userId\": \"XXXXXXXXXXXXXXXXXXXXXXXXXXX7999a6\"}" |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |