【问题标题】:Post Json Data in the API在 API 中发布 Json 数据
【发布时间】:2013-12-06 23:06:36
【问题描述】:

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

我的班级

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..
            }
        }

我遇到的错误

【问题讨论】:

  • “远程服务器返回错误:NotFound”它说。
  • 如何处理该错误..?远程服务器处于活动状态..我使用 GET 方法从 url 获取数据..
  • 这意味着,远程服务器无法为您的请求找到正确的控制器和操作。或者控制器操作可能由于某种原因返回 NotFound 错误消息。
  • 我该如何解决这个问题..?

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


【解决方案1】:

您应该改用 HttpClient。大致如下:

var client = new HttpClient();
client.SendAsync(<url>, data);

编辑

您的 API 是否存在于“myurl”中?

【讨论】:

  • 如果您可以修改我的代码 r 可以建议任何示例方法来完成它..我无法找到 post 方法的解决方案..
【解决方案2】:
  1. 您应该使用 HttpClient 或 RestSharp(http://www.nuget.org/packages/RestSharp/)。
  2. 如果基于 .net 的 api 使用 jsonp 或使用带有 EnableCors 的 web api 2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    • 1970-01-01
    • 2019-07-06
    • 2016-09-12
    • 2021-09-10
    相关资源
    最近更新 更多