我是初学者并创建winform应用程序.其中我必须使用API进行简单的CRUD操作.我的客户与我共享了API,并要求以JSON的形式发送数据.
API:http://blabla.com/blabla/api/login-valida
KEY:"HelloWorld"
价值:{"email":"user@gmail.com","密码":"123456","时间":"2015-09-22 10:15:20"}
响应:Login_id
如何将数据转换为JSON,使用POST方法调用API并获得响应?
编辑 stackoverflow上的某个地方我找到了这个解决方案
public static void POST(string url, string jsonContent)
{
url="blabla.com/api/blala" + url;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseURL);
request.Method = "POST";
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
Byte[] byteArray = encoding.GetBytes(jsonContent);
request.ContentLength = byteArray.Length;
request.ContentType = @"application/json";
using (Stream dataStream = request.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
}
long length = 0;
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
length = response.ContentLength;
}
}
catch
{
throw;
}
}
//on my login …Run Code Online (Sandbox Code Playgroud) 我在数据库中有菜单表,它有自引用外键,即 ParentID。下面是我的菜单类(数据库优先方法)
public partial class Menu
{
public Menu()
{
this.Menu1 = new HashSet<Menu>();
this.Products = new HashSet<Product>();
}
public int MenuID { get; set; }
public string Name { get; set; }
public Nullable<int> ParentID { get; set; }
public virtual ICollection<Menu> Menu1 { get; set; }
public virtual Menu Menu2 { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想实施以下事情,