C# 后台发送get,post请求及WebApi接收
2024-09-06 08:29:16
后台发送get请求
1.发送带参数的get请求
/// <summary>
/// 发送get请求 参数拼接在url后面
/// </summary>
/// <param name="url">请求接口地址</param>
/// <returns></returns>
public static string HttpGet(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
//设置发送请求的类型
request.ContentType = "text/html;charset=UTF-8";// "application/json";
//设置请求超时时间
request.Timeout = 60 * 1000; HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
string strResult = reader.ReadToEnd();
reader.Close();
response.Close();
return strResult;
}
}
2.后台接收请求API
/// <summary>
/// 获取站点信息
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("GetStationInfo")]
public ActionResult GetStationInfo(string stationName)
{
List<StationInfo> station= _stationServices.GetStationList().Where(p => p.StationName == stationName).ToList();
return new JsonResult(new { code = 1000, data = station });
}
后台发送post请求
1.发送带参数的post请求
string url = "https://localhost:44319/api/Station/AddStationInfo";
StationInfo stationInfo = new StationInfo()
{
StationGuid = Guid.NewGuid().ToString(),
StationName = "站点10",
UpdateTime = DateTime.Now
};
//传递json格式字符串信息
var jsonStr = JsonConvert.SerializeObject(stationInfo);
var result = HttpPost(url, jsonStr);
var returnData = JsonConvert.DeserializeObject<ReturnData>(result);
2.后台请求的方法
/// <summary>
/// 发送post请求 参数在Body中
/// </summary>
/// <param name="url">请求接口地址</param>
/// <param name="json">json字符串</param>
/// <returns></returns>
public static string HttpPost(string url, string json)
{
//创建请求
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
//请求方式为POST
request.Method = "POST";
request.ContentType = "application/json";
//设置请求超时时间
request.Timeout = 60 * 1000;
try
{
//添加post请求参数
byte[] bs = Encoding.UTF8.GetBytes(json);
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
reqStream.Close();
}
//发送请求并获取相应回应数据
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
{
string content = reader.ReadToEnd();
return content;
}
}
}
catch (WebException ex)
{
return string.Format("错误信息:{0},传输json串:{1}", ex.Message, json);
}
}
3.后台接收请求API
/// <summary>
/// 添加站点信息
/// </summary>
/// <param name="station"></param>
/// <returns></returns>
[HttpPost]
[Route("AddStationInfo")]
public ActionResult AddStationInfo(StationInfo station)
{
try
{
StationInfo stationInfo = new StationInfo();
stationInfo.StationGuid = station.StationGuid;
stationInfo.StationName = station.StationName;
stationInfo.UpdateTime = station.UpdateTime;
string result = _stationServices.AddStation(stationInfo) > 0 ? "添加成功" : "添加失败";
return new JsonResult(new { code = 1000, data = result });
}
catch (Exception ex)
{
return new JsonResult(new { code = 1000, data = ex.Message });
}
}
最新文章
- asp.net获取客户端浏览器及主机信息
- Ubuntu 用vsftpd 配置FTP服务器
- IOS Socket 05-XMPP开始&;安装服务器openfire&;安装配置客户端
- SecureCRT自动登录
- Mac下cocos2dx-3.0打包Android时,提示&;quot;SimpleAudioEngine.h&;quot;not found的解决方法
- Lost connection to MySQL server at ‘reading initial communication packet&#39;, system error: 0 mysql远程连接问题
- Struts1的实现原理
- Spring任务调度定时器
- MPAndroidChart——饼图
- 4.JAVA-数组、String详解
- android - TextView单行显示...或者文字左右滚动(走马灯效果)
- How does the compilation and linking process work?
- zepto 源码 $.contains 学习笔记
- commons工具类 FilenameUtils FileUtils
- 异常来自 HRESULT:0x80070057 (E_INVALIDARG)(转)
- LeetCode 12 - 整数转罗马数字 - [简单模拟]
- spring-boot-2.0.3应用篇 - shiro集成
- 《剑指offer》第五十五题(平衡二叉树)
- Linux系统运维笔记(四),CentOS 6.4安装Nginx
- 实战mysql分区