コード例 #1
0
        private void signIn_Click(object sender, EventArgs e)
        {
            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri(UserPage.connectionString);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            Guid id = Guid.Empty;

            try
            {
                id = Guid.Parse(signIntext.Text);
            }
            catch (ArgumentNullException)
            {
                id = Guid.Empty;
            }
            catch (FormatException)
            {
                id = Guid.Empty;
            }


            var url = "api/users/" + id;

            Program.userId = id;

            try
            {
                HttpResponseMessage response = client.GetAsync(url).Result;
                if (response.IsSuccessStatusCode)
                {
                    Users user = response.Content.ReadAsAsync <Users>().Result;
                    signIntext.Text = user.Nickname;
                    UserPage frm = new UserPage();
                    this.Hide();
                    frm.Show();
                }
                else
                {
                    signIntext.Text = "Неверные данные";
                }
            }
            catch
            {
                signIntext.Text = "Нет подключения";
            }
        }
コード例 #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
            photopath             = openFileDialog1.FileName;
            PreviewPhoto.SizeMode = PictureBoxSizeMode.StretchImage;
            PreviewPhoto.Load(photopath);

            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri(connectionString);

            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            Image newPhoto = PreviewPhoto.Image;

            Posts        post = new Posts();
            MemoryStream ms   = new MemoryStream();

            newPhoto.Save(ms, ImageFormat.Jpeg);
            byte[] bytes = ms.ToArray();
            post.Photo = Convert.ToBase64String(bytes);

            post.UserId = Program.userId;
            var response = client.PostAsJsonAsync("api/posts", post).Result;

            if (response.IsSuccessStatusCode)
            {
                MessageBox.Show("Пост добавлен");
                PreviewPhoto.Load(photopath);
            }
            else
            {
                MessageBox.Show("Ошибка " + response.StatusCode + " : подробнее - " + response.ReasonPhrase);
            }

            UserPage frm = new UserPage();

            this.Hide();
            frm.Show();
        }