public static async Task <byte[]> requestImageAsync(long pid) { Debug.WriteLine("DHB:ImageScrollingPage:requestImageAsync start pid:" + pid); byte[] result = null;// "fail"; try { HttpClient client = new HttpClient(); string previewURL = GlobalStatusSingleton.activeURL + PREVIEW + "/" + pid; HttpRequestMessage previewRequest = new HttpRequestMessage(HttpMethod.Get, previewURL); previewRequest.Headers.Add("Authorization", GlobalSingletonHelpers.getAuthToken()); HttpResponseMessage previewResult = await client.SendAsync(previewRequest); if (previewResult.StatusCode == System.Net.HttpStatusCode.OK) { //result = await previewResult.Content.ReadAsStringAsync(); result = await previewResult.Content.ReadAsByteArrayAsync(); } else { // no ok back from the server! gahh. Debug.WriteLine("DHB:ImageScrollingPage:requestSubmissionsAsync invalid result code: " + previewResult.StatusCode.ToString()); } } catch (System.Net.WebException err) { Debug.WriteLine("DHB:ImageScrollingPage:requestSubmissionsAsync:WebException"); Debug.WriteLine(err.ToString()); } catch (Exception e) { Debug.WriteLine("DHB:ImageScrollingPage:Exception"); Debug.WriteLine(e.ToString()); } Debug.WriteLine("DHB:ImageScrollingPage:requestImageAsync end pid:" + pid); return(result); }
public static async Task <bool> SendLogData(string logInfo) { Debug.WriteLine("DHB:GlobalSingletonHelpers:SendLogData start"); string result = "fail"; try { // only send log data if we are authenticated. if (GlobalStatusSingleton.authToken != null) { HttpClient client = new HttpClient(); client.BaseAddress = new Uri(GlobalStatusSingleton.activeURL); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "log"); LoggingJSON log = new LoggingJSON(); log.msg = logInfo; string jsonQuery = JsonConvert.SerializeObject(log); request.Content = new StringContent(jsonQuery, Encoding.UTF8, "application/json"); request.Headers.Add("Authorization", GlobalSingletonHelpers.getAuthToken()); HttpResponseMessage sendResult = await client.SendAsync(request); if (sendResult.StatusCode == System.Net.HttpStatusCode.OK) { // do I need these? result = await sendResult.Content.ReadAsStringAsync(); } } } catch (Exception e) { // we're doing nothing in the fail case. Debug.WriteLine(e.ToString()); } return(true); }
static async Task <string> requestLeaderboardAsync(long category_id) { Debug.WriteLine("DHB:LeaderboardPage:requestLeaderboardAsync start for id:" + category_id); string result = LOAD_FAILURE; try { HttpClient client = new HttpClient(); string categoryURL = GlobalStatusSingleton.activeURL + LEADERBOARD + CATEGORY + System.Convert.ToString(category_id); HttpRequestMessage leaderboardRequest = new HttpRequestMessage(HttpMethod.Get, categoryURL); leaderboardRequest.Headers.Add("Authorization", GlobalSingletonHelpers.getAuthToken()); HttpResponseMessage catResult = await client.SendAsync(leaderboardRequest); if (catResult.StatusCode == System.Net.HttpStatusCode.OK) { result = await catResult.Content.ReadAsStringAsync(); } else { // no ok back from the server! gahh. Debug.WriteLine("DHB:LeaderboardPage:requestLeaderboardAsync invalid result code: " + catResult.StatusCode.ToString()); } } catch (System.Net.WebException err) { Debug.WriteLine("DHB:LeaderboardPage:requestLeaderboardAsync:WebException"); Debug.WriteLine(err.ToString()); } catch (Exception e) { Debug.WriteLine("DHB:LeaderboardPage:Exception"); Debug.WriteLine(e.ToString()); } Debug.WriteLine("DHB:LeaderboardPage:requestLeaderboardAsync end"); return(result); }
/// <summary> /// Currently only requests for categories in the category call. /// </summary> /// <param name="category_id"></param> /// <returns></returns> protected static async Task <string> requestApiCallAsync(long category_id, string activeApiCall) { Debug.WriteLine("DHB:ImageScrollingPage:requestApiCallAsync start"); string result = "fail"; try { HttpClient client = new HttpClient(); string submissionURL = GlobalStatusSingleton.activeURL + activeApiCall + "/prev" + "/" + category_id + "?num_categories=5"; //string submissionURL = GlobalStatusSingleton.activeURL + SUBMISSIONS + "/next" + "/" + category_id + "?num_categories=5"; //string submissionURL = GlobalStatusSingleton.activeURL + SUBMISSIONS + "/next" + "/" + System.Convert.ToString(category_id) + "?num_categories=5"; //string submissionURL = GlobalStatusSingleton.activeURL + SUBMISSIONS + "/next/"+category_id; HttpRequestMessage submissionRequest = new HttpRequestMessage(HttpMethod.Get, submissionURL); submissionRequest.Headers.Add("Authorization", GlobalSingletonHelpers.getAuthToken()); HttpResponseMessage subResult = await client.SendAsync(submissionRequest); if (subResult.StatusCode == System.Net.HttpStatusCode.OK) { result = await subResult.Content.ReadAsStringAsync(); } else if ((subResult.StatusCode == System.Net.HttpStatusCode.NoContent) || (subResult.StatusCode == System.Net.HttpStatusCode.NotFound)) { result = EMPTY; } else { // no ok back from the server! gahh. Debug.WriteLine("DHB:ImageScrollingPage:requestApiCallAsync invalid result code: " + subResult.StatusCode.ToString()); } } catch (System.Net.WebException err) { Debug.WriteLine("DHB:ImageScrollingPage:requestApiCallAsync:WebException"); Debug.WriteLine(err.ToString()); } catch (Exception e) { Debug.WriteLine("DHB:ImageScrollingPage:requestApiCallAsync:Exception"); Debug.WriteLine(e.ToString()); } Debug.WriteLine("DHB:ImageScrollingPage:requestApiCallAsync end"); return(result); }
/// <summary> /// connects to the server and sends the user's current submission. /// </summary> /// <param name="imgBytes">bytes of the image we are sending.</param> /// <returns>The JSON success string on success, an err msg on failure. </returns> protected static async Task <string> sendSubmitAsync(byte[] imgBytes) { Debug.WriteLine("DHB:CameraEnterPhotoView:sendSubmitAsync start"); //string result = "fail"; PhotoSubmitResponseJSON resJson = new PhotoSubmitResponseJSON(); resJson.message = "fail"; string result = JsonConvert.SerializeObject(resJson); try { PhotoSubmitJSON submission = new PhotoSubmitJSON(); submission.imgStr = imgBytes; submission.extension = "JPEG"; //Debug.Assert(GlobalStatusSingleton.uploadingCategories.Count > 0, "DHB:ASSERT!:CameraContentPage:sendSubmitAsync Invalid uploading categories!"); //Debug.WriteLine(GlobalStatusSingleton.uploadingCategories.Count > 0, "DHB:ASSERT!:CameraContentPage:sendSubmitAsync Invalid uploading categories!"); //submission.categoryId = GlobalStatusSingleton.uploadingCategories[0].categoryId; if (CameraContentPage.activeCameraCategory == null) { result = "No category selected"; } else { //submission.categoryId = GlobalStatusSingleton.uploadingCategories[0].categoryId; submission.categoryId = CameraContentPage.activeCameraCategory.categoryId; Debug.WriteLine("DHB:CameraEnterPhotoView:sendSubmitAsync catid: " + CameraContentPage.activeCameraCategory.categoryId); //submission.userId = GlobalStatusSingleton.loginCredentials.userId; // moved this up in case serialization was causing the client to be disposed prematurely... string jsonQuery = JsonConvert.SerializeObject(submission); Debug.WriteLine("DHB:CameraEnterPhotoView:sendSubmitAsync query serialized"); Debug.WriteLine("DHB:CameraEnterPhotoView:sendSubmitAsync query[0,99]: " + jsonQuery.Substring(0, 100)); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, CameraContentPage.PHOTO); //request.Headers.Add("Authorization", "JWT " + GlobalStatusSingleton.authToken.accessToken); request.Headers.Add("Authorization", GlobalSingletonHelpers.getAuthToken()); request.Content = new StringContent(jsonQuery, Encoding.UTF8, "application/json"); HttpContent baseContent = new StringContent(jsonQuery, Encoding.UTF8, "application/json"); MemoryStream dummyStream = new MemoryStream(); ProgressableStreamContent trackableSend = new ProgressableStreamContent(baseContent, 4096, (sent, total) => { Debug.WriteLine("Uploading {0}/{1}", sent, total); }, imgBytes); //request.Content = trackableSend; Debug.WriteLine("DHB:CameraEnterPhotoView:sendSubmitAsync request obj built"); // string test = request.ToString(); HttpClient client = new HttpClient(); client.BaseAddress = new Uri(GlobalStatusSingleton.activeURL); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); Debug.WriteLine("DHB:CameraContentPage:sendSubmitAsync pre send"); HttpResponseMessage submitResult = await client.SendAsync(request); Debug.WriteLine("DHB:CameraEnterPhotoView:sendSubmitAsync post send"); if ((submitResult.StatusCode == System.Net.HttpStatusCode.Created) || (submitResult.StatusCode == System.Net.HttpStatusCode.OK)) { // tada Debug.WriteLine("DHB:CameraEnterPhotoView:sendSubmitAsync returned with code:" + submitResult.StatusCode.ToString()); result = await submitResult.Content.ReadAsStringAsync(); Debug.WriteLine("DHB:CameraEnterPhotoView:sendSubmitAsync result read in"); } else { Debug.WriteLine("DHB:CameraEnterPhotoView:sendSubmitAsync send statusCode=" + submitResult.StatusCode.ToString()); result = "unknown failure"; } } } catch (System.Net.WebException err) { // The server was down last time this happened. Is that the case now, when you are rereading this? // Or, is it a connection fail? Debug.WriteLine(err.ToString()); Debug.WriteLine("DHB:CameraEnterPhotoView:sendSubmitAsync webexception"); result = "Network error. Please check your connection and try again."; } catch (HttpRequestException err) { Debug.WriteLine(err.ToString()); // do something!! result = "login failure"; } catch (Exception err) { Debug.WriteLine(err.ToString()); // do something!! result = "unknown failure"; } Debug.WriteLine("DHB:CameraEnterPhotoView:sendSubmitAsync end"); return(result); }
public static async Task <string> requestFromServerAsync(HttpMethod method, string apiCall, string jsonQuery) { Debug.WriteLine("DHB:GlobalSingletonHelpers:requestFromServerAsync start"); string result = "fail"; try { HttpClient client = new HttpClient(); string requestCall = null; HttpRequestMessage request = null; if (method == HttpMethod.Get) { requestCall = GlobalStatusSingleton.activeURL + apiCall + jsonQuery; Uri req = new Uri(requestCall); request = new HttpRequestMessage(method, req); } else { client.BaseAddress = new Uri(GlobalStatusSingleton.activeURL); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); requestCall = apiCall; request = new HttpRequestMessage(method, requestCall); request.Content = new StringContent(jsonQuery, Encoding.UTF8, "application/json"); } request.Headers.Add("Authorization", GlobalSingletonHelpers.getAuthToken()); HttpResponseMessage response = await client.SendAsync(request); if ((response.StatusCode == System.Net.HttpStatusCode.OK) || (response.StatusCode == System.Net.HttpStatusCode.Created)) { // do I need these? result = await response.Content.ReadAsStringAsync(); } else if ((response.StatusCode == System.Net.HttpStatusCode.NoContent) || (response.StatusCode == System.Net.HttpStatusCode.NotFound)) { result = EMPTY; } else { // pooh. what do i do here? //result = "internal fail; why?"; // server failure. keep the msg as a fail for correct onVote processing // do we get back json? result = await response.Content.ReadAsStringAsync(); Debug.WriteLine("DHB:GlobalSingletonHelpers:requestFromServerAsync " + apiCall + " result recvd:" + result); result = "fail"; } response.Dispose(); request.Dispose(); client.Dispose(); } catch (System.Net.WebException err) { //result = "exception"; // web failure. keep the msg as a simple fail for correct onVote processing Debug.WriteLine(err.ToString()); } catch (Exception e) { Debug.WriteLine("DHB:GlobalSingletonHelpers:requestFromServerAsync:Exception apiCall:" + apiCall); Debug.WriteLine(e.ToString()); } Debug.WriteLine("DHB:GlobalSingletonHelpers:requestFromServerAsync " + apiCall + " end"); return(result); }