1

我正在開發WP8應用程序,我是新來的..我想知道我的發佈方法是否正確,因爲我無法發佈我的數據在它產生的url例外..發佈Json數據到其餘的API

我上課內容...

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

錯誤是..
enter image description here

感謝

回答

0

它看起來對我來說,有一個與你正在使用的URI的一個問題。您發佈的錯誤消息顯示服務器返回「未找到」標題。也許它只是不完全正確?我沒有在您發佈的代碼中看到確切的URI。 string url="myurl";看起來不像這是你想使用的網址。

這也是爲什麼您無法訪問響應流而無此例外的情況:MessageBox.Show(e.Result);。沒有有效的迴應。這裏記錄在這裏:http://msdn.microsoft.com/de-de/library/system.net.uploadstringcompletedeventargs.result%28v=vs.110%29.aspx

您可以通過檢查UploadStringCompletedEventArgshttp://msdn.microsoft.com/de-de/library/system.net.uploadstringcompletedeventargs%28v=vs.110%29.aspx)的Error屬性來確定是否發生此類錯誤。