private static void json_Callback(IAsyncResult result) { object[] vars = (object[])result.AsyncState; RequestType type = (RequestType)vars[1]; HttpWebRequest myHttpWebRequest = (HttpWebRequest)vars[0]; HttpWebResponse response = null; string data; JObject obj = null; ConvMessage convMessage; try { response = (HttpWebResponse)myHttpWebRequest.EndGetResponse(result); Stream responseStream = response.GetResponseStream(); if (string.Equals(response.Headers[HttpRequestHeader.ContentEncoding], "gzip", StringComparison.OrdinalIgnoreCase)) { data = decompressResponse(responseStream); } else { using (var reader = new StreamReader(responseStream)) { data = reader.ReadToEnd(); } } obj = JObject.Parse(data); } catch (IOException ioe) { obj = null; } catch (WebException we) { obj = null; } catch (JsonException je) { obj = null; } catch (Exception e) { obj = null; } finally { if (vars[2] is postResponseFunction) { postResponseFunction finalCallbackFunction = vars[2] as postResponseFunction; finalCallbackFunction(obj); } else if (vars[2] is postUploadPhotoFunction) { postUploadPhotoFunction finalCallbackFunctionForUpload = vars[2] as postUploadPhotoFunction; convMessage = vars[3] as ConvMessage; SentChatBubble chatBubble = vars[4] as SentChatBubble; finalCallbackFunctionForUpload(obj, convMessage, chatBubble); } } }
public static void uploadFile(byte[] dataBytes, postUploadPhotoFunction finalCallbackFunction, ConvMessage convMessage, SentChatBubble chatbubble) { HttpWebRequest req = HttpWebRequest.Create(new Uri(HikeConstants.FILE_TRANSFER_BASE_URL)) as HttpWebRequest; addToken(req); req.Method = "PUT"; req.ContentType = convMessage.FileAttachment.ContentType.Contains(HikeConstants.IMAGE) || convMessage.FileAttachment.ContentType.Contains(HikeConstants.VIDEO) ? "" : convMessage.FileAttachment.ContentType; req.Headers["Connection"] = "Keep-Alive"; req.Headers["Content-Name"] = convMessage.FileAttachment.FileName; req.Headers["X-Thumbnail-Required"] = "0"; req.BeginGetRequestStream(setParams_Callback, new object[] { req, RequestType.UPLOAD_FILE, dataBytes, finalCallbackFunction, convMessage, chatbubble }); }
private static void setParams_Callback(IAsyncResult result) { object[] vars = (object[])result.AsyncState; JObject data = new JObject(); HttpWebRequest req = vars[0] as HttpWebRequest; Stream postStream = req.EndGetRequestStream(result); postResponseFunction finalCallbackFunction = null; RequestType type = (RequestType)vars[1]; switch (type) { #region REGISTER ACCOUNT case RequestType.REGISTER_ACCOUNT: string pin = vars[2] as string; string unAuthMSISDN = vars[3] as string; finalCallbackFunction = vars[4] as postResponseFunction; data.Add("set_cookie", "0"); data.Add("devicetype", "windows"); data[HikeConstants.DEVICE_ID] = Utils.getHashedDeviceId(); //data[HikeConstants.DEVICE_TOKEN] = Utils.getDeviceId();//for push notifications data[HikeConstants.DEVICE_VERSION] = Utils.getDeviceModel(); data[HikeConstants.APP_VERSION] = Utils.getAppVersion(); string inviteToken = ""; if (!string.IsNullOrEmpty(inviteToken)) { data[HikeConstants.INVITE_TOKEN_KEY] = inviteToken; } if (pin != null) { data.Add("msisdn", unAuthMSISDN); data.Add("pin", pin); } Compress4(data.ToString(Formatting.None), postStream); postStream.Close(); req.BeginGetResponse(json_Callback, new object[] { req, type, finalCallbackFunction }); return; #endregion #region INVITE case RequestType.INVITE: string phoneNo = vars[2] as string; data.Add("to", phoneNo); break; #endregion #region VALIDATE NUMBER case RequestType.VALIDATE_NUMBER: string numberToValidate = vars[2] as string; finalCallbackFunction = vars[3] as postResponseFunction; data.Add("phone_no", numberToValidate); break; #endregion #region CALL ME case RequestType.CALL_ME: string msisdn = vars[2] as string; finalCallbackFunction = vars[3] as postResponseFunction; data.Add("msisdn", msisdn); break; #endregion #region SET NAME case RequestType.SET_NAME: string nameToSet = vars[2] as string; finalCallbackFunction = vars[3] as postResponseFunction; data.Add("name", nameToSet); break; #endregion #region SET PROFILE case RequestType.SET_PROFILE: JObject jo = vars[2] as JObject; data = jo; finalCallbackFunction = vars[3] as postResponseFunction; break; #endregion #region POST ADDRESSBOOK case RequestType.POST_ADDRESSBOOK: Dictionary <string, List <ContactInfo> > contactListMap = vars[2] as Dictionary <string, List <ContactInfo> >; finalCallbackFunction = vars[3] as postResponseFunction; data = getJsonContactList(contactListMap); string x = data.ToString(Newtonsoft.Json.Formatting.None); Compress4(x, postStream); postStream.Close(); req.BeginGetResponse(json_Callback, new object[] { req, type, finalCallbackFunction }); ContactUtils.ContactState = ContactUtils.ContactScanState.ADDBOOK_POSTED; return; #endregion #region SOCIAL POST case RequestType.SOCIAL_POST: data = vars[2] as JObject; finalCallbackFunction = vars[3] as postResponseFunction; break; #endregion #region SOCIAL DELETE case RequestType.SOCIAL_DELETE: finalCallbackFunction = vars[2] as postResponseFunction; break; #endregion #region UPDATE ADDRESSBOOK case RequestType.UPDATE_ADDRESSBOOK: Dictionary <string, List <ContactInfo> > contacts_to_update = vars[2] as Dictionary <string, List <ContactInfo> >; JArray ids_json = vars[3] as JArray; finalCallbackFunction = vars[4] as postResponseFunction; if (ids_json != null) { data.Add("remove", ids_json); } JObject ids_to_update = getJsonContactList(contacts_to_update); if (ids_to_update != null) { data.Add("update", ids_to_update); } break; #endregion #region DELETE ACCOUNT case RequestType.DELETE_ACCOUNT: finalCallbackFunction = vars[2] as postResponseFunction; break; #endregion #region POST PROFILE ICON case RequestType.POST_PROFILE_ICON: byte[] imageBytes = (byte[])vars[2]; finalCallbackFunction = vars[3] as postResponseFunction; postStream.Write(imageBytes, 0, imageBytes.Length); postStream.Close(); req.BeginGetResponse(json_Callback, new object[] { req, type, finalCallbackFunction }); return; #endregion #region POST PUSH NOTIFICATION DATA case RequestType.POST_PUSHNOTIFICATION_DATA: string uri = (string)vars[2]; finalCallbackFunction = vars[3] as postResponseFunction; data.Add("dev_token", uri); data.Add("dev_type", "windows"); break; #endregion case RequestType.POST_INFO_ON_APP_UPDATE: finalCallbackFunction = vars[2] as postResponseFunction; if (Utils.IsWP8) { data["_os"] = "win8"; } else { data["_os"] = "win7"; } data["_os_version"] = Utils.getOSVersion(); data["deviceversion"] = Utils.getDeviceModel(); data["app_version"] = Utils.getAppVersion(); data["dev_type"] = "windows"; break; #region UPLOAD FILE case RequestType.UPLOAD_FILE: byte[] dataBytes = (byte[])vars[2]; postUploadPhotoFunction finalCallbackForUploadFile = vars[3] as postUploadPhotoFunction; ConvMessage convMessage = vars[4] as ConvMessage; SentChatBubble chatBubble = vars[5] as SentChatBubble; int bufferSize = 2048; int startIndex = 0; int noOfBytesToWrite = 0; double progressValue = 0; while (startIndex < dataBytes.Length) { Thread.Sleep(5); noOfBytesToWrite = dataBytes.Length - startIndex; noOfBytesToWrite = noOfBytesToWrite < bufferSize ? noOfBytesToWrite : bufferSize; postStream.Write(dataBytes, startIndex, noOfBytesToWrite); progressValue = ((double)(startIndex + noOfBytesToWrite) / dataBytes.Length) * 100; bool updated = chatBubble.updateProgress(progressValue); if (!updated) { chatBubble.setAttachmentState(Attachment.AttachmentState.CANCELED); break; } startIndex += noOfBytesToWrite; } postStream.Close(); req.BeginGetResponse(json_Callback, new object[] { req, type, finalCallbackForUploadFile, convMessage, chatBubble }); return; #endregion #region DEFAULT default: break; #endregion } using (StreamWriter sw = new StreamWriter(postStream)) { string json = data.ToString(Newtonsoft.Json.Formatting.None); sw.Write(json); } postStream.Close(); req.BeginGetResponse(json_Callback, new object[] { req, type, finalCallbackFunction }); }