内容简介:此项目作者IKende ,github开源地址:此人博客园地址:我基于他的github项目的 samples学习的。反正我崇拜他
此项目作者IKende ,github开源地址: https://github.com/IKende
此人博客园地址: https://www.cnblogs.com/smark/
我基于他的github项目的 samples学习的。反正我崇拜他
设置cookie,以前都是在Response上加
public bool setCookie(string name, string value, IHttpContext context)
{
context.Response.SetCookie(name, value);
return true;
}
public string getCookie(string name, HttpRequest request, IHttpContext context)
{
string value = context.Request.Cookies[name];
return value;
}
同理设置header
public void SetHeader(string token,IHttpContext context)
{
context.Response.Header["Token"]=token;
}
public string GetHeader(string name, IHttpContext context)
{
return context.Request.Header[name];
}
拓展,context里面肯定还有好东西,可以跟以前的HttpContext 差不多的设置。
下面是他sample中一个js
var HomesetCookieUrl='/setcookie';
/**
* 'var result= await HomesetCookie(params);'
**/
function HomesetCookie(name,value,useHttp)
{
return api(HomesetCookieUrl,{name:name,value:value},useHttp).sync();
}
/**
* 'HomesetCookieAsync(params).execute(function(result){},useHttp);'
**/
function HomesetCookieAsync(name,value,useHttp)
{
return api(HomesetCookieUrl,{name:name,value:value},useHttp);
}
var HomegetCookieUrl='/getcookie';
/**
* 'var result= await HomegetCookie(params);'
**/
function HomegetCookie(name,useHttp)
{
return api(HomegetCookieUrl,{name:name},useHttp).sync();
}
/**
* 'HomegetCookieAsync(params).execute(function(result){},useHttp);'
**/
function HomegetCookieAsync(name,useHttp)
{
return api(HomegetCookieUrl,{name:name},useHttp);
}
是封装了一个api的函数为了吗?还是es6语法? 我以前没见过。
忽略吧
他的view下面提供了
2个js,他自己封装的
var __id = 0;
var __receive;
var __connect;
var __disconnect;
function FastHttpApiWebSocket() {
this.wsUri = "ws://" + window.location.host;
this.websocket;
this.status = false;
this.messagHandlers = new Object();
}
FastHttpApiWebSocket.prototype.send = function (url, params, callback) {
if (this.status == false) {
if (callback != null) {
callback({ Url: url, Code: 505, Error: 'disconnect' })
}
}
this.messagHandlers[params._requestid] = callback;
var data = { url: url, params: params };
this.websocket.send(JSON.stringify(data));
}
FastHttpApiWebSocket.prototype.onOpen = function (evt) {
this.status = true;
if (__connect)
__connect(this);
}
FastHttpApiWebSocket.prototype.onClose = function (evt) {
this.status = false;
var _this = this;
if (__disconnect)
__disconnect(this);
if (evt.code == 1006) {
setTimeout(function () {
_this.Connect();
}, 2000);
}
}
FastHttpApiWebSocket.prototype.onMessage = function (evt) {
var msg = JSON.parse(evt.data);
var callback = this.messagHandlers[msg.ID];
if (callback)
callback(msg);
else
if (__receive)
__receive(msg);
}
FastHttpApiWebSocket.prototype.onError = function (evt) {
}
FastHttpApiWebSocket.prototype.Connect = function () {
this.websocket = new WebSocket(this.wsUri);
_this = this;
this.websocket.onopen = function (evt) { _this.onOpen(evt) };
this.websocket.onclose = function (evt) { _this.onClose(evt) };
this.websocket.onmessage = function (evt) { _this.onMessage(evt) };
this.websocket.onerror = function (evt) { _this.onError(evt) };
}
function FastHttpApi(url, params, http, post) {
if (http == true)
this.http = true;
else
this.http = false;
this.url = url;
this.post = false;
if (post == true)
this.post = true;
this.params = params;
if (!this.params)
this.params = new Object();
}
FastHttpApi.prototype.sync = function () {
var _this = this;
return new Promise(resolve => {
_this.execute(function (result) {
resolve(result);
});
});
}
FastHttpApi.prototype.httpRequest = function () {
this.http = true;
return this.sync();
}
FastHttpApi.prototype.execute = function (callback, http) {
if (http == true)
this.http = true;
var id = ++__id;
if (__id > 1024)
__id = 0;
var httpurl;
var keys;
var index;
this.params['_requestid'] = id;
if (this.http || __websocket.status == false) {
if (this.post) {
httpurl = this.url;
$.post(httpurl, JSON.stringify(this.params), function (result) {
if (callback)
callback(result);
});
}
else {
//get
httpurl = this.url;
keys = Object.keys(this.params);
index = 0;
for (i = 0; i < keys.length; i++) {
if (this.params[keys[i]]) {
if (index == 0) {
httpurl += "?";
}
else {
httpurl += "&";
}
httpurl += keys[i] + '=' + encodeURIComponent(this.params[keys[i]]);
index++;
}
}
$.get(httpurl, function (result) {
if (callback)
callback(result);
});
}
}
else {
__websocket.send(this.url, this.params, callback);
}
}
function api_connect(callback) {
__connect = callback;
}
function api_disconnect(callback) {
__disconnect = callback;
}
function api(url, params, http, post) {
return new FastHttpApi(url, params, http, post);
}
function api_receive(callback) {
__receive = callback;
}
var __websocket = new FastHttpApiWebSocket();
__websocket.Connect();
/************************************************************************************
FastHttpApi javascript api Generator Copyright © henryfan 2018 email:henryfan@msn.com
https://github.com/IKende/FastHttpApi
**************************************************************************************/
var HomesetCookieUrl = '/setcookie';
/**
* 'var result= await HomesetCookie(params);'
**/
function HomesetCookie(name, value, useHttp) {
return api(HomesetCookieUrl, { name: name, value: value }, useHttp).sync();
}
/**
* 'HomesetCookieAsync(params).execute(function(result){},useHttp);'
**/
function HomesetCookieAsync(name, value, useHttp) {
return api(HomesetCookieUrl, { name: name, value: value }, useHttp);
}
var HomegetCookieUrl = '/getcookie';
/**
* 'var result= await HomegetCookie(params);'
**/
function HomegetCookie(name, useHttp) {
return api(HomegetCookieUrl, { name: name }, useHttp).sync();
}
/**
* 'HomegetCookieAsync(params).execute(function(result){},useHttp);'
**/
function HomegetCookieAsync(name, useHttp) {
return api(HomegetCookieUrl, { name: name }, useHttp);
}
他的测试页面html代码
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<link href="css/bootstrap.css" rel="stylesheet" />
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.1.min.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/FastHttpApi.js"></script>
<script src="js/api.js"></script>
<title>FastHttpApi Cookie Sample</title>
</head>
<body>
<nav aria-label="breadcrumb">
<ol>
<li class="breadcrumb-item active" aria-current="page">FastHttpApi Cookie Sample</li>
</ol>
</nav>
<div class="container bs-docs-container">
<div>
<div role="main">
<div class="panel panel-default">
<div> <h3>set cookie</h3></div>
<div>
<form>
<div>
<label for="exampleInputEmail1">Cookie Name</label>
<input type="text" id="cookieName" aria-describedby="emailHelp" placeholder="cookie name">
</div>
<div>
<label for="exampleInputPassword1">Value</label>
<input type="text" id="cookieValue">
</div>
<button type="button" onclick="setCookie()" class="btn btn-primary">submit</button>
</form>
</div>
</div>
<div class="panel panel-default">
<div>
<h3>get cookie</h3>
</div>
<div>
<form>
<div>
<label for="exampleInputEmail1">Cookie Name</label>
<input type="text" id="getcookieName" aria-describedby="emailHelp" placeholder="cookie name">
</div>
<div>
<label for="exampleInputPassword1">Value</label>
<input type="text" id="getcookieValue">
</div>
<button type="button" onclick="getCookie()" class="btn btn-primary">submit</button>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
<script>
async function setCookie() {
var result = await HomesetCookie($('#cookieName').val(), $('#cookieValue').val(), true);
if (result.Data == true)
alert('success!')
}
async function getCookie() {
var result = await HomegetCookie($('#getcookieName').val(), true);
$('#getcookieValue').val(result.Data);
}
</script>
</html>
推荐您阅读更多有关于“FastHttpApi,”的文章
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Science of Programming
David Gries / Springer / 1989-4-21 / USD 99.00
Describes basic programming principles and their step-by- step applications.Numerous examples are included.一起来看看 《The Science of Programming》 这本书的介绍吧!