DartVM服务器开发(二)-处理请求

上篇文章中,我们完成了第一个dart服务器,输出了Hello World!今天,我们来学习处理请求,获取请求方法,获取请求参数,获取请求头,那开始学习吧!

1. 处理请求

在上一节中,我们对所有请求都回复它一个Hello World!这个信息,我们现在改变一下,添加一个方法,传入request,把Hello World!这一条注释掉

1
2
3
4
5
6
7
8
9
10
11
12
13
  //....
//监听请求
await for(HttpRequest request in requestServer){

//监听到请求后response回复它一个Hello World!然后关闭这个请求
// request.response..write('Hello World!')
// ..close();
handleMessage(request);
}
}
void handleMessage(HttpRequest request){
//在这个方法里面处理请求
}

一般情况下,我们是不知道别人要对我们服务器做什么的,如果服务器出现异常而崩溃掉,就会影响其它用户的访问,所以,我们需要try..catch里面处理请求,以防止客户端恶意请求

1
2
3
4
5
6
7
8
9
//....
void handleMessage(HttpRequest request){
try{
//在这里处理请求
}catch(e){
print('出现了一个异常,异常为:$e');
}
print('请求被处理了');
}

2. 请求方法

request这个对象里面,有一个method变量,这个变量就是请求的方法了,我们是可以通过request.method获取,请求方法有下面几种!
| 请求类型|含义|
| - | - |
| request.method==’GET’ | 当前为GET请求(一般为访问资源) |
| request.method==’POST’ | 当前为POST请求 (一般为提交数据)|
| request.method==’PUT’ | 当前为PUT请求(一般为特定位置上传资源) |
| request.method==’DELETE’ | 当前为DELETE请求(一般为删除资源) |
| request.method==’TRACE’ | 当前为TRACE请求(一般用于测试或诊断) |
| request.method==’CONNECT’ | 当前为CONNECT请求 (一般为代理服务)|
| request.method==’HEAD’ | 当前为HEAD请求 (一般获取响应消息头)|
| request.method==’OPTIONS’ | 当前为OPTIONS请求 (一般获取服务器支持的请求方法)|
好了,我们知道了可以使用request.method来获取请求方法,那么对于一般服务器来说,只用到GET或者POST,所以,我们对不是GET或者POST的请求回应不支持该请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void handleMessage(HttpRequest request){
try{
if(request.method=='GET'){
//获取到GET请求
handleGET(request);
}else if(request.method=='POST'){
//获取到POST请求
handlePOST(request);
}else{
//其它的请求方法暂时不支持,回复它一个状态
request.response..statusCode=HttpStatus.methodNotAllowed
..write('对不起,不支持${request.method}方法的请求!')
..close();
}
}catch(e){
print('出现了一个异常,异常为:$e');
}
print('请求被处理了');
}

void handleGET(HttpRequest request){
//处理GET请求
}
void handlePOST(HttpRequest request){
//处理POST请求
}

上面我们看到了一个新的东西,就是request.response..statusCode ,这个变量呢,是对客户端返回一个状态码,我们熟悉的状态码有200(ok),404(链接不存在)等等,下面,我们学习一下dart内置的状态码有哪些(有同学可能会疑问,为什么使用“ .. ”而不是使用“ . ”呢,这个就是dart的一个语法,通过” .. “ 我们可以不断的调用第一次“ .. ”的那个对象的方法,简直爱死这个操作有没有,有点语法糖的味道)

3. 状态码

HttpStatus这个类是一个抽象类,里面定义的一连串的状态码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
abstract class HttpStatus {
//继续
static const int continue_ = 100;
//交换协议
static const int switchingProtocols = 101;
//可以
static const int ok = 200;
//已创建
static const int created = 201;
//认可的
static const int accepted = 202;
//非授权信息
static const int nonAuthoritativeInformation = 203;
//没有内容
static const int noContent = 204;
//重置内容
static const int resetContent = 205;
//部分内容
static const int partialContent = 206;
//多项选择
static const int multipleChoices = 300;
//永久迁移
static const int movedPermanently = 301;
//已发现
static const int found = 302;
//临时迁移
static const int movedTemporarily = 302; // Common alias for found.
//查看其它
static const int seeOther = 303;
//未修改的
static const int notModified = 304;
//使用代理
static const int useProxy = 305;
//暂时重定向
static const int temporaryRedirect = 307;
//请求失败
static const int badRequest = 400;
//没有授权
static const int unauthorized = 401;
//要求付款
static const int paymentRequired = 402;
//被禁止
static const int forbidden = 403;
//未找到
static const int notFound = 404;
//请求方法不允许
static const int methodNotAllowed = 405;
//不接受
static const int notAcceptable = 406;
//需要代理身份认证
static const int proxyAuthenticationRequired = 407;
//请求超时
static const int requestTimeout = 408;
//冲突
static const int conflict = 409;
//过去了
static const int gone = 410;
//长度要求
static const int lengthRequired = 411;
//先决条件失败
static const int preconditionFailed = 412;
//请求实体过大
static const int requestEntityTooLarge = 413;
//请求地址过长
static const int requestUriTooLong = 414;
//非支持的媒体类型
static const int unsupportedMediaType = 415;
//请求范围不可满足
static const int requestedRangeNotSatisfiable = 416;
//期望失败
static const int expectationFailed = 417;
//升级要求
static const int upgradeRequired = 426;
//内部服务器错误
static const int internalServerError = 500;
//未实现
static const int notImplemented = 501;
//网关坏了
static const int badGateway = 502;
//服务不可用
static const int serviceUnavailable = 503;
//网关超时
static const int gatewayTimeout = 504;
//http版本不支持
static const int httpVersionNotSupported = 505;
// 连接超时
static const int networkConnectTimeoutError = 599;
}

4.获取请求的参数

当接收到请求时,客户端传递一个参数给我,我们应该怎样去获取呢?
这里我们需要用到 request.uri 这个变量,这个变量主要包含了请求的资源,例如:主机,地址,端口,查询字符串等等,那么现在,我们定义一个变量为id,当接收到这个id时,返回一个字符串为’当前查询的id为:$id’

1
2
3
4
5
6
7
8
void handleGET(HttpRequest request){
//获取一个参数
var id=request.uri.queryParameters['id'];//查询id的值
request.response
..statusCode=HttpStatus.ok//回复它一个ok状态,说明我收到请求啦
..write('当前查询的id为$id')//显示到浏览器的内容
..close();//我已经回复你了,所以关闭这个请求
}

好了,我们按照之前的方法,启动服务器吧!然后到浏览器输入 http://localhost:8080?id=123
(这里因为是GET请求,所以,可以直接在链接上面传入查询参数,一般以 链接?key=value 的形式传入,key 对应为 id ,value对应为id的值,我们有不同的参数,就以 链接?key1=value1&key2=value2 这种形式传入)
传入id.png
可以看到,我们很成功的获取到id的值。

5.请求头

获取请求头,我们也是很容易就可以获取到的,可以通过
request.headers获取到

1
2
3
4
5
6
request.headers.forEach((key,values){
print('key:$key');
for(String value in values){
print('value:$value');
}
});

上面代码,可以打印出客户端请求的详细请求头
请求头返回的信息.png

今天我们学习了如何处理请求,获取请求方法,获取请求参数,获取请求头,好了,明天见!

评论系统未开启,无法评论!