public class HttpUitls
{
public HttpUitls() { }
#region 查询所有柜子数据
public List<Cabinet> AllCabinetData()
{
Task<string> task = Task.Run(async () => await "https://localhost:44358/Cabinet/GetAllCabinetsByJson".GetStringAsync());
string text = task.Result;
List<Cabinet> list = JsonConvert.DeserializeObject<List<Cabinet>>(text);
return list;
}
#endregion
#region 添加柜子数据
public int AddCabinet(Cabinet cabinet)
{
Cabinet cabinetAdd = cabinet;
// 请求路径
string url = "https://localhost:44358/Cabinet/AddCabinet";
// 定义request并设置request的路径
WebRequest request = WebRequest.Create(url);
request.Method = "post";
// 初始化request参数
int result;
string json = JsonConvert.SerializeObject(cabinetAdd);
byte[] data = Encoding.UTF8.GetBytes(json);
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.Timeout = 60000;
// 发送数据
try
{
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
WebResponse response = request.GetResponse();
newStream = response.GetResponseStream();
StreamReader reader = new StreamReader(newStream, Encoding.UTF8);
result = Convert.ToInt32(reader.ReadToEnd());
reader.Close();
newStream.Close();
return result;
}
catch (Exception)
{
}
return -1;
}
#endregion
#region 删除柜子数据
public int DeleteCabinet(Cabinet cabinetDelete)
{
Cabinet cabinet = cabinetDelete;
// 请求路径
string url = "https://localhost:44358/Cabinet/DelCabinet";
// 定义request并设置request的路径
WebRequest request = WebRequest.Create(url);
request.Method = "post";
// 初始化request参数
int result;
string json = JsonConvert.SerializeObject(cabinet);
byte[] data = Encoding.UTF8.GetBytes(json);
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.Timeout = 60000;
// 发送数据
try
{
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
WebResponse response = request.GetResponse();
newStream = response.GetResponseStream();
StreamReader reader = new StreamReader(newStream, Encoding.UTF8);
result = Convert.ToInt32(reader.ReadToEnd());
reader.Close();
newStream.Close();
return result;
}
catch (Exception)
{
}
return -1;
}
#endregion
#region 批量删除柜子数据
public int DeleteCabinets(List<Cabinet> deleteCabinetLsit)
{
List<Cabinet> cabinetsList = deleteCabinetLsit;
// 请求路径
string url = "https://localhost:44358/Cabinet/DeleteCabinets";
// 定义request并设置request的路径
WebRequest request = WebRequest.Create(url);
request.Method = "post";
// 初始化request参数
int result;
string json = JsonConvert.SerializeObject(cabinetsList);
byte[] data = Encoding.UTF8.GetBytes(json);
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.Timeout = 60000;
// 发送数据
try
{
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
WebResponse response = request.GetResponse();
newStream = response.GetResponseStream();
StreamReader reader = new StreamReader(newStream, Encoding.UTF8);
result = Convert.ToInt32(reader.ReadToEnd());
reader.Close();
newStream.Close();
return result;
}
catch (Exception)
{
}
return -1;
}
#endregion
#region 修改柜子数据
public int UpdateCabinet(List<Cabinet> cabinetList)
{
List<Cabinet> updateCabinet = cabinetList;
// 请求路径
string url = "https://localhost:44358/Cabinet/UpDateCabinet";
// 定义request并设置request的路径
WebRequest request = WebRequest.Create(url);
request.Method = "post";
// 初始化request参数
int result;
string json = JsonConvert.SerializeObject(updateCabinet);
byte[] data = Encoding.UTF8.GetBytes(json);
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.Timeout = 60000;
// 发送数据
try
{
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
WebResponse response = request.GetResponse();
newStream = response.GetResponseStream();
StreamReader reader = new StreamReader(newStream, Encoding.UTF8);
result = Convert.ToInt32(reader.ReadToEnd());
reader.Close();
newStream.Close();
return result;
}
catch (Exception)
{
}
return -1;
}
#endregion
#region 修改柜子数据时判断是否冲突
public int UpdateTheSame(Cabinet selectCabinet)
{
Cabinet cabinetAdd = selectCabinet;
// 请求路径
string url = "https://localhost:44358/Cabinet/SelectCabinet";
// 定义request并设置request的路径
WebRequest request = WebRequest.Create(url);
request.Method = "post";
// 初始化request参数
int result;
string json = JsonConvert.SerializeObject(cabinetAdd);
byte[] data = Encoding.UTF8.GetBytes(json);
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.Timeout = 60000;
// 发送数据
try
{
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
WebResponse response = request.GetResponse();
newStream = response.GetResponseStream();
StreamReader reader = new StreamReader(newStream, Encoding.UTF8);
result = Convert.ToInt32(reader.ReadToEnd());
reader.Close();
newStream.Close();
return result;
}
catch (Exception)
{
}
return -1;
}
#endregion
#region 用户登录验证
public int CheckUser(User checkUser)
{
User user = checkUser;
// 请求路径
string url = "https://localhost:44358/User/CheckUser";
// 定义request并设置request的路径
WebRequest request = WebRequest.Create(url);
request.Method = "post";
// 初始化request参数
int result;
string json = JsonConvert.SerializeObject(user);
byte[] data = Encoding.UTF8.GetBytes(json);
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.Timeout = 60000;
// 发送数据
try
{
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
WebResponse response = request.GetResponse();
newStream = response.GetResponseStream();
StreamReader reader = new StreamReader(newStream, Encoding.UTF8);
result = Convert.ToInt32(reader.ReadToEnd());
reader.Close();
newStream.Close();
// 返回1则验证成功,返回0则验证失败
return result;
}
catch (Exception)
{
}
return -1;
}
#endregion
#region 添加用户数据
public int AddUser(User user)
{
User userAdd = user;
// 请求路径
string url = "https://localhost:44358/User/AddUser";
// 定义request并设置request的路径
WebRequest request = WebRequest.Create(url);
request.Method = "post";
// 初始化request参数
int result;
string json = JsonConvert.SerializeObject(userAdd);
byte[] data = Encoding.UTF8.GetBytes(json);
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.Timeout = 60000;
// 发送数据
try
{
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
WebResponse response = request.GetResponse();
newStream = response.GetResponseStream();
StreamReader reader = new StreamReader(newStream, Encoding.UTF8);
result = Convert.ToInt32(reader.ReadToEnd());
reader.Close();
newStream.Close();
return result;
}
catch (Exception)
{
}
return -1;
}
#endregion
#region 查询所有用户数据
public List<User> AllUserData()
{
Task<string> task = Task.Run(async () => await "https://localhost:44358/User/GetAllUsersByJson".GetStringAsync());
string text = task.Result;
List<User> list = JsonConvert.DeserializeObject<List<User>>(text);
return list;
}
#endregion
#region 修改用户数据
public int UpdateUser(List<User> userList)
{
List<User> updateUser = userList;
// 请求路径
string url = "https://localhost:44358/User/UpDateUser";
// 定义request并设置request的路径
WebRequest request = WebRequest.Create(url);
request.Method = "post";
// 初始化request参数
int result;
string json = JsonConvert.SerializeObject(updateUser);
byte[] data = Encoding.UTF8.GetBytes(json);
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.Timeout = 60000;
// 发送数据
try
{
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
WebResponse response = request.GetResponse();
newStream = response.GetResponseStream();
StreamReader reader = new StreamReader(newStream, Encoding.UTF8);
result = Convert.ToInt32(reader.ReadToEnd());
reader.Close();
newStream.Close();
return result;
}
catch (Exception)
{
}
return -1;
}
#endregion
#region 删除用户数据
public int DeleteUser(User userDelete)
{
User user = userDelete;
// 请求路径
string url = "https://localhost:44358/User/DeleteUser";
// 定义request并设置request的路径
WebRequest request = WebRequest.Create(url);
request.Method = "post";
// 初始化request参数
int result;
string json = JsonConvert.SerializeObject(user);
byte[] data = Encoding.UTF8.GetBytes(json);
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.Timeout = 60000;
// 发送数据
try
{
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
WebResponse response = request.GetResponse();
newStream = response.GetResponseStream();
StreamReader reader = new StreamReader(newStream, Encoding.UTF8);
result = Convert.ToInt32(reader.ReadToEnd());
reader.Close();
newStream.Close();
return result;
}
catch (Exception)
{
}
return -1;
}
#endregion
#region 查询当前用户信息
public User SeleteCurrentUser(User userSelect)
{
User user = userSelect;
//string name = user.Name;
// 请求路径
string url = "https://localhost:44358/User/SelectUser";
// 定义request并设置request的路径
WebRequest request = WebRequest.Create(url);
request.Method = "post";
// 初始化request参数
User result = new User();
string json = JsonConvert.SerializeObject(user);
byte[] data = Encoding.UTF8.GetBytes(json);
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.Timeout = 60000;
// 发送数据
try
{
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
WebResponse response = request.GetResponse();
newStream = response.GetResponseStream();
StreamReader reader = new StreamReader(newStream, Encoding.UTF8);
result = JsonConvert.DeserializeObject<User>(reader.ReadToEnd());
reader.Close();
newStream.Close();
return result;
}
catch (Exception)
{
}
return result;
}
#endregion
#region 更新用户时是否重复
public bool UpdateOnlyUser(User newUser)
{
User user = newUser;
// 请求路径
string url = "https://localhost:44358/User/CheckUser";
// 定义request并设置request的路径
WebRequest request = WebRequest.Create(url);
request.Method = "post";
// 初始化request参数
int result;
string json = JsonConvert.SerializeObject(user);
byte[] data = Encoding.UTF8.GetBytes(json);
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.Timeout = 60000;
// 发送数据
try
{
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
WebResponse response = request.GetResponse();
newStream = response.GetResponseStream();
StreamReader reader = new StreamReader(newStream, Encoding.UTF8);
result = Convert.ToInt32(reader.ReadToEnd());
reader.Close();
newStream.Close();
if (result == 0)
{
return true;
}
}
catch (Exception)
{
}
return false;
}
#endregion
#region 获取当前操作者的ID
public User SeleteCurrentUserByID(int userID)
{
User user = new User();
user.ID = userID;
// 请求路径
string url = "https://localhost:44358/User/SelectUserByID";
// 定义request并设置request的路径
WebRequest request = WebRequest.Create(url);
request.Method = "post";
// 初始化request参数
User result = new User();
string json = JsonConvert.SerializeObject(user);
byte[] data = Encoding.UTF8.GetBytes(json);
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.Timeout = 60000;
// 发送数据
try
{
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
WebResponse response = request.GetResponse();
newStream = response.GetResponseStream();
StreamReader reader = new StreamReader(newStream, Encoding.UTF8);
result = JsonConvert.DeserializeObject<User>(reader.ReadToEnd());
reader.Close();
newStream.Close();
return result;
}
catch (Exception)
{
}
return result;
}
#endregion
}