資源簡介
1、用C#的exe程序,搭建HTTPS監聽,可用于靜態網站、接口、模擬WebApi等等
2、由于是客戶端EXE,可以正常訪問客戶端所有硬件資源
3、如:web界面上調用http://localhost:xx/+參數,調用本地硬件、本地緩存(內存或文件,可
代替cookies)、打開本地程序、調用打印機、掃碼墩....

代碼片段和文件信息
using?System;
public?class?test
{
????public?test()
????{
????????System.Net.HttpListener?httpListener?=?new?System.Net.HttpListener();
????????httpListener.AuthenticationSchemes?=?System.Net.AuthenticationSchemes.Anonymous;
????????httpListener.Prefixes.Add(“https://*:443/“);
????????httpListener.Start();
????????new?System.Threading.Thread(new?System.Threading.ThreadStart(delegate
????????{
????????????while?(true)
????????????{
????????????????try
????????????????{
????????????????????System.Net.HttpListenerContext?httpListenerContext?=?httpListener.GetContext();
????????????????????new?System.Threading.Thread(new?System.Threading.ParameterizedThreadStart((input)?=>
????????????????????{
????????????????????????System.Net.HttpListenerContext?ctx?=?(System.Net.HttpListenerContext)input;
????????????????????????System.Net.HttpListenerRequest?request?=?ctx.Request;
????????????????????????string?pram?=?request.QueryString[“Data“];//Get入參?
????????????????????????string?responseMessage?=?string.Empty;//返回值
????????????????????????if?(!string.IsNullOrEmpty(pram))
????????????????????????{
????????????????????????????//Get入參,url解密
????????????????????????????pramOrg?=?pram;
????????????????????????????pram?=?System.Web.HttpUtility.UrlDecode(pram);
????????????????????????}
????????????????????????//POST入參
????????????????????????if?(request.HttpMethod?==?“POST“)
????????????????????????{
????????????????????????????isPost?=?true;
????????????????????????????//處理業務請求
????????????????????????????StreamReader?reader?=?new?StreamReader(request.InputStream?Encoding.UTF8);
????????????????????????????pram?=?reader.ReadToEnd();
????????????????????????????reader.Close();
????????????????????????????reader.Dispose();
????????????????????????}
????????????????????????#region?業務處理
????????????????????????try
????????????????????????{
????????????????????????????//業務處理
????????????????????????????responseMessage?=?“業務結果“;
????????????????????????}
????????????????????????catch?(Exception?ex)
????????????????????????{
????????????????????????????//異常處理
????????????????????????????responseMessage?=?ex.Message;
????????????????????????}
????????????????????????#endregion?業務處理
????????????????????????#region?返回給調用者
????????????????????????//輸出類型
????????????????????????httpListenerContext.Response.ContentType?=?“text/html;?charset=UTF-8“;
????????????????????????//返回狀態
????????????????????????httpListenerContext.Response.StatusCode?=?200;
????????????????????????//設置授權,嘗試解決Jquery跨域問題
????????????????????????//httpListenerContext.Response.Headers[“Access-Control-Allow-Origin“]?=?“*“;
????????????????????????//httpListenerContext.Response.Headers[“Access-Control-Allow-Methods“]?=?“GETPOST“;
????????????????????????//httpListenerContext.Response.Headers[“Access-Control-Max-Age“]?=?“1000“;
????????????????????????try
????????????????????????{
????????????????????????????//輸出界面內容
????????????????????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????296071??2019-09-05?17:57??HTTPS證書創建+綁定端口+C#程序監聽.pdf
?????文件???????3970??2019-09-05?17:59??test.cs
-----------?---------??----------?-----??----
???????????????300041????????????????????2
- 上一篇:實現UDP可靠文件傳輸
- 下一篇:基于asp.net c#在線答題頁面
評論
共有 條評論