后台发送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 });
}
}

最新文章

  1. asp.net获取客户端浏览器及主机信息
  2. Ubuntu 用vsftpd 配置FTP服务器
  3. IOS Socket 05-XMPP开始&amp;安装服务器openfire&amp;安装配置客户端
  4. SecureCRT自动登录
  5. Mac下cocos2dx-3.0打包Android时,提示&amp;quot;SimpleAudioEngine.h&amp;quot;not found的解决方法
  6. Lost connection to MySQL server at ‘reading initial communication packet&#39;, system error: 0 mysql远程连接问题
  7. Struts1的实现原理
  8. Spring任务调度定时器
  9. MPAndroidChart——饼图
  10. 4.JAVA-数组、String详解
  11. android - TextView单行显示...或者文字左右滚动(走马灯效果)
  12. How does the compilation and linking process work?
  13. zepto 源码 $.contains 学习笔记
  14. commons工具类 FilenameUtils FileUtils
  15. 异常来自 HRESULT:0x80070057 (E_INVALIDARG)(转)
  16. LeetCode 12 - 整数转罗马数字 - [简单模拟]
  17. spring-boot-2.0.3应用篇 - shiro集成
  18. 《剑指offer》第五十五题(平衡二叉树)
  19. Linux系统运维笔记(四),CentOS 6.4安装Nginx
  20. 实战mysql分区

热门文章

  1. oracle异常处理——ORA-01000:超出打开游标最大数
  2. 【C#】【MySQL】【GridView】删除出现Parameter index is out of range
  3. 使用wesocket从 rabbitMQ获取实时数据
  4. 【简】题解 P5283 [十二省联考2019]异或粽子
  5. numpy基础教程--将二维数组转换为一维数组
  6. Redis操作命令合集
  7. 如何推翻JAVA的统治地位
  8. canvas绘制圣诞树
  9. APS高级计划排程系统和生产排产系统
  10. Frequency函数(Excel函数集团)