コード例 #1
0
        public static async void HttpPost(string phone, string password)
        {
            try
            {
                HttpClient httpClient = new HttpClient();
                string     posturi    = Config.apiUserRegister;

                string                    date     = DateTime.Now.Date.Year.ToString() + "-" + DateTime.Now.Date.Month.ToString() + "-" + DateTime.Now.Date.Day.ToString();
                HttpRequestMessage        request  = new HttpRequestMessage(HttpMethod.Post, new Uri(posturi));
                HttpFormUrlEncodedContent postData = new HttpFormUrlEncodedContent(
                    new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>("phone", phone),       //手机号
                    new KeyValuePair <string, string>("password", password), //密码
                    new KeyValuePair <string, string>("date", date),         //注册日期
                }
                    );
                request.Content = postData;
                HttpResponseMessage response = await httpClient.SendRequestAsync(request);

                string responseString = await response.Content.ReadAsStringAsync();



                JsonObject register = JsonObject.Parse(responseString);
                try
                {
                    int code = (int)register.GetNamedNumber("code");
                    switch (code)
                    {
                    case 0:
                    {
                        JsonObject user = register.GetNamedObject("user");
                        Config.UserPhone = user.GetNamedString("phone");
                        NavigationHelp.NavigateTo(typeof(UserData));
                        break;
                    }

                    case 1:
                        HelpMethods.Msg("手机号已注册!");
                        break;

                    case 2:
                        HelpMethods.Msg("未知错误!");
                        break;

                    default:
                        break;
                    }
                }
                catch (Exception ex)
                {
                    HelpMethods.Msg(ex.Message.ToString());
                }
            }
            catch (Exception ex)
            {
                HelpMethods.Msg(ex.Message.ToString());
            }
        }
コード例 #2
0
        public static async void HttpUserPost(string phone, string name, string content, string picPath, string tag)
        {
            HttpClient httpClient = new HttpClient();

            try
            {
                string      posturi = Config.apiUserUpdate;
                StorageFile file1   = await StorageFile.GetFileFromPathAsync(picPath);

                IRandomAccessStreamWithContentType stream1 = await file1.OpenReadAsync();

                HttpStreamContent streamContent1 = new HttpStreamContent(stream1);
                HttpStringContent stringContent  = new HttpStringContent(content);
                HttpStringContent stringTitle    = new HttpStringContent(phone);
                HttpStringContent stringName     = new HttpStringContent(name);
                HttpStringContent stringTag      = new HttpStringContent(tag);

                HttpMultipartFormDataContent fileContent = new HttpMultipartFormDataContent();
                fileContent.Add(stringContent, "dream");
                fileContent.Add(stringTitle, "phone");
                fileContent.Add(stringTag, "tag");
                fileContent.Add(stringName, "name");
                fileContent.Add(streamContent1, "picture", "pic.jpg");
                HttpResponseMessage response = await httpClient.PostAsync(new Uri(posturi), fileContent);

                string responString = await response.Content.ReadAsStringAsync();



                if ((int)response.StatusCode == 200)
                {
                    JsonObject user = JsonObject.Parse(responString);


                    Config.UserName  = user.GetNamedString("name");
                    Config.UserImage = Config.apiFile + user.GetNamedString("image");
                    Config.UserPhone = user.GetNamedString("phone");
                    Config.UserDream = user.GetNamedString("dream");
                    Config.UserTag   = user.GetNamedString("tag");

                    NavigationHelp.NavigateTo(typeof(Main));
                }
            }
            catch (Exception ex)
            {
                HelpMethods.Msg(ex.Message.ToString());
            }
        }
コード例 #3
0
ファイル: HttpPostLogin.cs プロジェクト: wenxiayili/dreaming
        public static async void HttpPost(string phone, string password)
        {
            try
            {
                HttpClient                httpClient = new HttpClient();
                string                    posturi    = Config.apiUserLogin;
                HttpRequestMessage        request    = new HttpRequestMessage(HttpMethod.Post, new Uri(posturi));
                HttpFormUrlEncodedContent postData   = new HttpFormUrlEncodedContent(
                    new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>("phone", phone),       //手机
                    new KeyValuePair <string, string>("password", password), //密码
                }
                    );
                request.Content = postData;
                HttpResponseMessage response = await httpClient.SendRequestAsync(request);

                string responseString = await response.Content.ReadAsStringAsync();

                Debug.WriteLine(responseString);

                JsonObject login = JsonObject.Parse(responseString);
                try
                {
                    int code = (int)login.GetNamedNumber("code");
                    switch (code)
                    {
                    case 0:
                    {
                        JsonObject user = login.GetNamedObject("user");
                        Config.UserName  = user.GetNamedString("name");
                        Config.UserImage = Config.apiFile + user.GetNamedString("image");
                        Config.UserPhone = user.GetNamedString("phone");
                        Config.UserDream = user.GetNamedString("dream");
                        Config.UserTag   = user.GetNamedString("tag");
                        NavigationHelp.NavigateTo(typeof(Main));
                        break;
                    }

                    case 1:
                        HelpMethods.Msg("手机号未注册!");
                        break;

                    case 2:
                        HelpMethods.Msg("密码错误!");
                        break;

                    default:
                        break;
                    }
                }
                catch (Exception ex)
                {
                    HelpMethods.Msg("服务器出错!");
                    Debug.WriteLine(ex.Message.ToString());
                }
            }
            catch (Exception ex)
            {
                HelpMethods.Msg(ex.Message.ToString());
            }
        }