在API中发布Json数据
我正在开发WP8应用程序,我想知道我的post方法是否正确,因为我无法在url中发布数据,因为它会产生异常 我的班级在API中发布Json数据,json,post,windows-phone-8,windows-phone,webclient,Json,Post,Windows Phone 8,Windows Phone,Webclient,我正在开发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
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..
}
}
我收到的错误
您应该改用HttpClient。大致如下:
var client = new HttpClient();
client.SendAsync(<url>, data);
var-client=new-HttpClient();
client.sendsync(,数据);
编辑
“myurl”中是否存在API
“远程服务器返回了一个错误:NotFound”,它说。如何处理该错误。。?远程服务器处于活动状态。我已使用GET方法从url获取数据。这意味着,远程服务器无法为您的请求找到正确的控制器和操作。或者可能控制器操作出于某种原因返回NotFound错误消息。我如何解决此问题..?如果您可以修改我的代码r,您可以建议任何示例方法来完成它..我无法找到post方法的解决方案。。