json - 在 API 中发布 Json 数据

标签 json post windows-phone-8 windows-phone webclient

我正在开发 WP8 应用程序,我想知道我的发布方法是否正确,因为我无法在产生异常的 url 中发布我的数据..

我的类(class)

public class Register     
    {

        public int id { get; set; }
        public string password_reset_hash { get; set; }
        public string temp_password { get; set; }
        public bool remember_me { get; set; }
        public string activation_hash { get; set; }
        public string ip_address { get; set; }
        public bool status { get; set; }
        public bool activated { get; set; }
        public string permissions { get; set; }
        public DateTime last_login { get; set; }
        public DateTime created_at { get; set; }
        public DateTime updated_at { get; set; }
        public string email { get; set; }
        public string password { get; set; }
        public string conformpassword { get; set; }
        public string username { get; set; }      
    }

我的代码

 public  void btn_register_click(object sender, RoutedEventArgs e)
        {
            string url="myurl";
            Register res=new Register();// my class
            res.email = txt_email.Text;
            res.password = txt_password.Text;
            res.conformpassword = txt_conf_psswrd.Text;
            res.username = txt_username.Text;
            res.created_at = DateTime.Now;
            res.last_login = DateTime.Now;
            res.updated_at = DateTime.Now;
            res.status = true;
            
            json = JsonConvert.SerializeObject(res);
            WebClient wc = new WebClient();
            var URI = new Uri(url);  
            wc.Headers["Content-Type"] = "application/json";                
            wc.Headers["ACCEPT"] = "application/json";
            wc.UploadStringCompleted += new UploadStringCompletedEventHandler(wc_UploadStringCompleted);
            wc.UploadStringAsync(URI, "POST", json);             

        }

        private void wc_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            try
            {
                MessageBox.Show(e.Result); 
                //e.result fetches you the response against your POST request.         

            }

            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString()); //i'm getting error here..
            }
        }

我收到错误

enter image description here

最佳答案

  1. 您应该使用 HttpClient 或 RestSharp( http://www.nuget.org/packages/RestSharp/ )。
  2. 如果基于 .net 的 api 使用 jsonp 或使用带有 EnableCors 的 Web api 2

关于json - 在 API 中发布 Json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20138586/

相关文章:

java - 使用 jackson 将自定义反序列化类添加到已经存在的@JsonDeserialize 注释

HTTP POST : content-length header required?

php - htaccess 和两个 post 参数

windows - 使用 Windows Phone 应用程序进行的 PayPal 交易

windows-phone-8 - Windows Phone 8 应用程序栏

jquery - 在 jQuery 中循环 JSON 响应

json - 以JSON格式返回QuerySet吗?

windows-phone-8 - Windows Phone 8模拟器上的控制台窗口功能

php - 我能够以 json 格式从 facebook 获取数据,但无法插入数据库。

php - 当 GET 和 POST 发送相同的值时 $_REQUEST 会返回什么?