首页 > 其他分享 >HttpUitls:Winform操作

HttpUitls:Winform操作

时间:2023-07-02 12:55:43浏览次数:34  
标签:request HttpUitls json newStream result reader 操作 data Winform

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
}

标签:request,HttpUitls,json,newStream,result,reader,操作,data,Winform
From: https://www.cnblogs.com/wen-chen/p/17520656.html

相关文章

  • WebApiApplication:WebApi操作
    [Route("/[controller]/[action]")][ApiController]publicclassCabinetController:ControllerBase{privatestaticIFreeSqlfreeSqlInstance=newFreeSqlBuilder().UseConnectionString(DataType.SqlServer,"DataSource=.;Initia......
  • 光脚丫学ASP.NET MVC(0008):非操作方法
    视频演示:http://u.115.com/file/e6r505ey控制器中的方法并不一定都是操作方法。要使控制器中的方法称为操作方法,则必须使其为公共方法,并且没有为其添加NonActionAttribute特性。私有方法不是操作方法,静态方法也不是操作方法。如下面的两个方法,均不是控制器的操作方法。privateA......
  • Markdown操作方式
    Markdown操作方式标题​ 一共分为六级书写方式:​ #(个数不同级数不同)+空格+编写内容引用​ 书写方式:>(个数不同效果不同)+空格字体​ 加粗:****在中间写文字​ 斜体:**在中间写文字​ 删除线:~~~~在中间写文字​ 高亮:====在中间写文字​ 上标:^^在中间写文字......
  • Linux文件系统、常用文件操作命令及用户权限
    1、熟悉Linux的文件系统结构Linux的文件系统结构其实是一个树形的分层组织结构,如下图:Linux系统目录结构及目录路径:1.1、文件系统层次结构标准Linux是开源的操作系统,各个Linux发行机构都可以按照自己的需求对Linux系统的文件系统进行相应的裁剪,所以各个Linux发行版本的目录结构......
  • Debian是一种流行的自由操作系统,它基于Linux内核,采用自由软件的方式进行开发和发布 De
    Debian是一种流行的自由操作系统,它基于Linux内核,采用自由软件的方式进行开发和发布。以下是关于Debian起源和其开发者的信息:Debian的起源:Debian的起源可以追溯到1993年,最初由IanMurdock(IanAshleyMurdock,1970-2015)创建。他希望构建一个自由、开放和稳定的操作系统发行版,以及一......
  • Xv6 操作系统组织架构
    进程概述64位的RISC-V的VAS是39位的,即VA只有39位,而Xv6则只有38位,最大虚拟地址为#defineMAXVA0x3fffffffff。VAS的顶端,即最高位存放了两个page,一个是用于trampoline,一个用于mappingtheprocess'strapframe。Xv6使用这两个page来切换到内核以及返回。......
  • Linux常用操作
    Linux常用文件操作Linux常用文件操作目录简介cdusr切换到该目录下usr目录cd../切换到上一层目录cd/切换到系统根目录cd~切换到用户主目录cd-切换到上一个所在目录su或suroot进入root上下文su<用户名>......
  • 3、apache-kylin-3.1.3-bin-hadoop3基本操作(创建model和cube、数据查詢)
    apache-kylin-3.1.3-bin-hadoop3系列文章1、apache-kylin-3.1.3-bin-hadoop3介绍及部署、验证详解2、apache-kylin-3.1.3-bin-hadoop3集群部署3、apache-kylin-3.1.3-bin-hadoop3基本操作(创建model和cube、数据查詢)4、apache-kylin-3.1.3-bin-hadoop3增量构建、全量构建详细示例......
  • 5、apache-kylin-3.1.3-bin-hadoop3 segment管理及JDBC操作
    apache-kylin-3.1.3-bin-hadoop3系列文章1、apache-kylin-3.1.3-bin-hadoop3介绍及部署、验证详解2、apache-kylin-3.1.3-bin-hadoop3集群部署3、apache-kylin-3.1.3-bin-hadoop3基本操作(创建model和cube、数据查詢)4、apache-kylin-3.1.3-bin-hadoop3增量构建、全量构建详细示例......
  • 3、Apache Kudu集成impala(shell和java操作)的详细操作
    ApacheKudu系列文章1、ApacheKudu介绍及架构、工作原理、两种部署方式、使用限制详解2、ApacheKudu-javaapi操作kudu详细示例以及kudu的三种实现示例3、ApacheKudu集成impala(shell和java操作)的详细操作文章目录ApacheKudu系列文章一、kudu集成impala1、impala配置修改二......