public MultipartMimeFormContent(HttpMultipartMimeForm form) { this.Form = form; string raw = GetNonceAsHexDigitString(12); this.ContentType = "multipart/form-data; boundary=" + raw; boundary = "--" + raw; }
/// <summary> /// Validates the given user’s application key. /// </summary> /// <returns>OK, nick, name. Error message on failure.</returns> public UserValidateResponse Validate() { using (var client = GetDefaultClient()) { // build form data post HttpMultipartMimeForm form = new HttpMultipartMimeForm(); form.Add("app_key", this.AppKey); form.Add("user_key", this.UserKey); // call method using (HttpResponseMessage response = client.Post("user.validate", form.CreateHttpContent())) { response.EnsureStatusIsSuccessful(); return response.Content.ReadAsXmlSerializable<UserValidateResponse>(); } } }
/// <summary> /// Posts a message to the user’s Hellotxt services. /// </summary> /// <param name="request">The request.</param> /// <returns>OK. Error message on failure.</returns> public ServiceResponse Post(UserPostRequest request) { using (var client = GetDefaultClient()) { // build form data post HttpMultipartMimeForm form = new HttpMultipartMimeForm(); form.Add("app_key", this.AppKey); form.Add("user_key", this.UserKey); form.Add("body", request.body); if (!String.IsNullOrWhiteSpace(request.title)) form.Add("title", request.title); if (!String.IsNullOrWhiteSpace(request.networks)) form.Add("networks", request.networks); if (!String.IsNullOrWhiteSpace(request.poi_lat)) form.Add("poi_lat", request.poi_lat); if (!String.IsNullOrWhiteSpace(request.poi_long)) form.Add("poi_long", request.poi_long); if (!String.IsNullOrWhiteSpace(request.poi_name)) form.Add("poi_name", request.poi_name); if (!String.IsNullOrWhiteSpace(request.scheduled_time)) form.Add("scheduled_time", request.scheduled_time); if (!String.IsNullOrWhiteSpace(request.debug)) form.Add("debug", request.debug); if (request.image != null) { HttpFormFile file = new HttpFormFile(); file.Content = HttpContent.Create(request.image, "application/octet-stream"); file.FileName = request.image.Name; file.Name = "image"; form.Files.Add(file); } // call method using (HttpResponseMessage response = client.Post("user.post", form.CreateHttpContent())) { response.EnsureStatusIsSuccessful(); return response.Content.ReadAsXmlSerializable<ServiceResponse>(); } } }
/// <summary> /// Remove a service from the given user /// </summary> /// <param name="request">The request.</param> /// <returns>OK. Error message on failure.</returns> public ServiceResponse RemoveService(UserRemoveServiceRequest request) { using (var client = GetDefaultClient()) { // build form data post HttpMultipartMimeForm form = new HttpMultipartMimeForm(); form.Add("app_key", this.AppKey); form.Add("user_key", this.UserKey); form.Add("service", request.service); form.Add("code", request.code); // call method using (HttpResponseMessage response = client.Post("user.removeservice", form.CreateHttpContent())) { response.EnsureStatusIsSuccessful(); return response.Content.ReadAsXmlSerializable<ServiceResponse>(); } } }
/// <summary> /// Add/update a service to the given user /// </summary> /// <param name="request">The request.</param> /// <returns>OK. Error message on failure.</returns> public ServiceResponse AddService(UserAddServiceRequest request) { using (var client = GetDefaultClient()) { // build form data post HttpMultipartMimeForm form = new HttpMultipartMimeForm(); form.Add("app_key", this.AppKey); form.Add("user_key", this.UserKey); form.Add("service", request.service); if (!String.IsNullOrWhiteSpace(request.code)) form.Add("code", request.code); form.Add("nick", request.nick); form.Add("pwd", request.pwd); // call method using (HttpResponseMessage response = client.Post("user.addservice", form.CreateHttpContent())) { response.EnsureStatusIsSuccessful(); return response.Content.ReadAsXmlSerializable<ServiceResponse>(); } } }
/// <summary> /// Register a new user in Hellotxt. /// </summary> /// <param name="request">The request.</param> /// <returns>OK, user_key. Error message on failure.</returns> public UserRegisterResponse Register(UserRegisterRequest request) { using (var client = GetDefaultClient()) { // build form data post HttpMultipartMimeForm form = new HttpMultipartMimeForm(); form.Add("app_key", this.AppKey); form.Add("email", request.email); if (!String.IsNullOrWhiteSpace(request.pass)) form.Add("pass", request.pass); // call method using (HttpResponseMessage response = client.Post("user.register", form.CreateHttpContent())) { response.EnsureStatusIsSuccessful(); return response.Content.ReadAsXmlSerializable<UserRegisterResponse>(); } } }
/// <summary> /// Asynchronicznie dodaje status do blipa /// </summary> /// <param name="content">treść statusu</param> /// <param name="imagePath">ścieżka do pliku z obrazem</param> public void AddUpdateAsync(string content, string imagePath) { string query = "/updates"; string fileName = Path.GetFileName(imagePath); HttpMultipartMimeForm form = new HttpMultipartMimeForm(); form.Add("update[body]", content); using (MemoryStream ms = new MemoryStream()) { Image img = Image.FromFile(imagePath); img.Save(ms, System.Drawing.Imaging.ImageFormat.Png); byte[] imageBytes = ms.ToArray(); HttpContent pic = HttpContent.Create(imageBytes, "application/octet-stream"); form.Add("update[picture]", fileName, pic); lock (httpAsyncClientLock) { //jako state przekazujemy cały obiekt,aby można było pobrać później z niego ResponseMessage blipHttpClientAsync.BeginSend( new HttpRequestMessage("POST", new Uri(query, UriKind.Relative), form.CreateHttpContent()), new AsyncCallback(AfterAddStatusAsync), blipHttpClientAsync); } } }