库类:

Owin.dll
Owin.IAppBuilder Microsoft.Owin.dll
Microsoft.Owin.OwinContext Microsoft.Owin.Hosting.dll
Microsoft.Owin.Hosting.WebApp Microsoft.Owin.Host.HttpListener.dll \Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Net.Http.dll
System.Net.Http.HttpResponseMessage System.Web.Http.dll
System.Web.Http.ApiController、System.Web.Http.HttpConfiguration System.Web.Http.Owin.dll
System.Net.Http.Formatting.dll Newtonsoft.Json.dll

1. 代码

1.1 启动

using System;
using System.Web.Http;
using Microsoft.Owin.Hosting;
using Owin; class HttpServer
{
static void Main(string[] args)
{
string baseAddress = "http://127.0.0.1:8090"; WebApp.Start<StartUp>(url: baseAddress); Console.WriteLine("开始监听", baseAddress);
Console.ReadKey();
}
} internal class StartUp
{
public void Configuration(IAppBuilder appBuilder)
{
var config = new HttpConfiguration(); // 允许特性路由
config.MapHttpAttributeRoutes(); //按控制器controller名称的路由
var controlroute = config.Routes.MapHttpRoute(
name: "ControllerApi",
routeTemplate: "{controller}"
); // web api 接口
appBuilder.UseWebApi(config);
}
}

1.2 控制器基类

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json; public class BaseApiController : ApiController
{
/// <summary>
/// 创建一个json格式的返回信息,一般用于返回值类型是string(直接返回string如果123,会变成"123"),如果是其它对象类型,直接返回即可,程序会自动转成json
/// </summary>
/// <param name="obj">返回信息,如果是字符串,则不进行转换</param>
/// <param name="encode">返回是的编码</param>
/// <param name="setting">转换为json时所用的设置</param>
/// <returns></returns>
[NonAction()]
public HttpResponseMessage CreateJsonResponse(object obj, Encoding encode = null, JsonSerializerSettings setting = null)
{
// 返回的HttpResponseMessage
string str;
HttpStatusCode statusCode;
HttpResponseMessage result;
try
{
if (obj == null)
{
str = null;
statusCode = HttpStatusCode.NoContent;
}
else if (obj is string || obj is char)
{
str = obj.ToString();
statusCode = HttpStatusCode.OK;
}
else
{
if (setting == null) setting = new JsonSerializerSettings { NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore, DateFormatString = "yyyy-MM-dd HH:mm:ss" };
str = JsonConvert.SerializeObject(obj, setting);
statusCode = HttpStatusCode.OK;
}
result = new HttpResponseMessage { StatusCode = statusCode, Content = new StringContent(str, encode == null ? Encoding.UTF8 : encode, "application/json") };
}
catch (Exception ex)
{
result = new HttpResponseMessage { StatusCode = System.Net.HttpStatusCode.InternalServerError, Content = new StringContent(ex.ToString(), encode == null ? Encoding.UTF8 : encode) };
}
return result;
}
}

1.3 业务控制器

/// <summary>
/// Json数据转发控制器
/// </summary>
public class JsonDataForwardController : BaseApiController
{
#region 接收数据,1.通过put或get并把数据作为参数附在url推送过来,2.通过post并将数据附在body上传送过来。推荐用第2种方法 /// <summary>
/// 通过url参数获取接收数据
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
[HttpGet]
[HttpPut]
public HttpResponseMessage ReveiveDataFromUrl(string data)
{
return CreateJsonResponse(ReveiveData(data));
} /// <summary>
/// 在body中获取接收数据
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
[HttpPost]
public HttpResponseMessage ReveiveDataFromBody([FromBody] object data)
{
return CreateJsonResponse(ReveiveData(data.ToString()));
}
#endregion #region 普通请求
[HttpPost]
[Route("JsonDataForward/Extend")]
public HttpResponseMessage ExtendRequest([FromBody] object body)
{
return new HttpResponseMessage();
}
#endregion #region 非http请求方法
/// <summary>
/// 处理推送过来的数据
/// </summary>
/// <param name="data">推送过来的数据</param>
/// <returns></returns>
[NonAction()]
public string ReveiveData(string data)
{
return string.Empty;
}
}

Post 请求:

  1. http://127.0.0.1:8090/JsonDataForward 触发 ReveiveDataFromBody
  2. http://127.0.0.1:8090/JsonDataForward/Extend 触发 ExtendRequest

3. Http 系列

3.1 发起请求

使用 HttpWebRequest 发起 Http 请求:https://www.cnblogs.com/MichaelLoveSna/p/14501036.html

使用 WebClient 发起 Http 请求 :https://www.cnblogs.com/MichaelLoveSna/p/14501582.html

使用 HttpClient 发起 Http 请求:https://www.cnblogs.com/MichaelLoveSna/p/14501592.html

使用 HttpClient 发起上传文件、下载文件请求:https://www.cnblogs.com/MichaelLoveSna/p/14501603.html

3.2 接受请求

使用 HttpListener 接受 Http 请求:https://www.cnblogs.com/MichaelLoveSna/p/14501628.html

使用 WepApp 接受 Http 请求:https://www.cnblogs.com/MichaelLoveSna/p/14501612.html

使用 WepApp 处理文件上传、下载请求:https://www.cnblogs.com/MichaelLoveSna/p/14501616.html

C# 应用 - 使用 WepApp 接受 Http 请求的更多相关文章

  1. C# 应用 - 使用 HttpListener 接受 Http 请求

    1. 库类: \Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.dll System.Net.HttpListen ...

  2. Web APi之捕获请求原始内容的实现方法以及接受POST请求多个参数多种解决方案(十四)

    前言 我们知道在Web APi中捕获原始请求的内容是肯定是很容易的,但是这句话并不是完全正确,前面我们是不是讨论过,在Web APi中,如果对于字符串发出非Get请求我们则会出错,为何?因为Web A ...

  3. 限制action所接受的请求方式或请求参数

    原文:http://www.cnblogs.com/liukemng/p/3726897.html 2.限制action所接受的请求方式(get或post): 之前我们在HelloWorldContr ...

  4. Controller 的 Action 只接受 Ajax 请求

    ASP.NET MVC 使 Controller 的 Action 只接受 Ajax 请求. 2014-08-27 14:19 by h82258652, 555 阅读, 2 评论, 收藏, 编辑 首 ...

  5. C# 发送和接受Get请求

    1.发送Get请求 public static string HttpGet(string Url, string postDataStr) { HttpWebRequest request = (H ...

  6. C#发送和接受POST请求

    1.发送Post请求代码 /// <summary> /// 发起Http请求 /// </summary> /// <param name="flightDa ...

  7. Spring MVC 接受的请求参数

    目录 1. 概述 2. 详解 2.1 处理查询参数 2.2 处理路径参数接受输入 2.3 处理表单 3. 补充内容 3.1 Ajax/JSON 输入 3.2 multipart参数 3.3 接收 he ...

  8. 让webapi只接受ajax请求

    为了测试先做一个简单的webapi,直接用新建项目时默认的就可以了.   在浏览器中测试request get,得到结果   然后再项目中新建一个AjaxOnly的类   AjaxOnly继承Acti ...

  9. ASP.NET MVC 使 Controller 的 Action 只接受 Ajax 请求。

    首先,ajax 请求跟一般的 web 请求本质是相同的,都是 http 请求.理论上服务器端是无法区分该次请求是不是 ajax 请求的,但是,既然标题都已经说了,那么肯定是有办法做的. 在 ajax ...

随机推荐

  1. Go语言中时间轮的实现

    最近在工作中有一个需求,简单来说就是在短时间内会创建上百万个定时任务,创建的时候会将对应的金额相加,防止超售,需要过半个小时再去核对数据,如果数据对不上就需要将加上的金额再减回去. 这个需求如果用Go ...

  2. 爬虫入门六 总结 资料 与Scrapy实例-bibibili番剧信息

    title: 爬虫入门六 总结 资料 与Scrapy实例-bibibili番剧信息 date: 2020-03-16 20:00:00 categories: python tags: crawler ...

  3. WMI在渗透测试中的重要性

    0x01 什么是wmi WMI可以描述为一组管理Windows系统的方法和功能.我们可以把它当作API来与Windows系统进行相互交流.WMI在渗透测试中的价值在于它不需要下载和安装, 因为WMI是 ...

  4. how to config custom process.env in node.js

    how to config custom process.env in node.js process.env APP_ENV NODE_ENV https://nodejs.org/api/proc ...

  5. 算法的时间复杂度 & 性能对比

    算法的时间复杂度 & 性能对比 累加算法性能对比 // js 累加算法性能对比测试 const n = 10**6; (() => { console.time(`for`); let ...

  6. how to install zoom meeting app in macOS

    how to install zoom meeting app in macOS https://support.zoom.us/hc/zh-cn/articles/203020795-如何在Mac上 ...

  7. project generators & project scaffold

    project generators & project scaffold how to write a node cli & Project Scaffold https://www ...

  8. Node.js Debugger

    Node.js Debugger VS Code & Chrome DevTools https://nodejs.org/api/debugger.html https://nodejs.o ...

  9. windwos创建和删除服务

    创建 >sc create <service name> type=kernel binpath="C:\hsys.sys" 删除 win+r 输出 regedi ...

  10. 算法型稳定币USDN有哪些使用功能

    众所周知,稳定币是基于区块链的支付工具,旨在实现最终用户要求的价格稳定性.有些稳定币利用法定货币作为抵押资产.其他则使用一系列其他非法定类型的抵押资产.还有一些尝试使用算法来实现价格稳定性而根本没有抵 ...