我已经发出了一个POST请求,并收到了一个响应体和标头。现在我如何使用标头来发出GET请求呢?
这是POST登录请求的代码。
void sendLogin() async {
var map = <String, dynamic>{
"UserName": _usernameController.text,
"Password": _passwordController.text,
};
var res = await http.post(
Uri.parse("http://192.168.1.8:8080/HongLeong/LOGIN_REQUEST.do"),
body: map,
);
final data = jsonDecode(res.body);
print(res.statusCode);
print(res.body);
print(res.headers);
if ((data as Map)['RESPONSE']['login_request_result'] == null) {
showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: const Center(child: Text('Invalid Username/Password')),
actions: <Widget>[
Center(
child: TextButton(
onPressed: () {
_usernameController.clear();
_passwordController.clear();
Navigator.of(ctx).pop();
},
child: const Text('Ok'),
),
),
],
),
);
} else {
Navigator.push(context,
MaterialPageRoute(builder: (context) => const DashboardPage()));
}
}
这是响应体:
{
"RESPONSE":{
"login_request_result":{
"user_must_change_password":"0"
},
"BASEL_RESPONSE":{
"UserDate":"0",
"UserTime":"0",
"UserName":"Administrator",
"module_config_1":"0",
"module_config_2":"0",
"ErrEntity":{
"MessageID":"0",
"last_req_id":"50029",
"table_id":"0",
"key_id_list":"536871",
"operation_id":"0"
},
"is_csv":"0",
"VersionName":"DYMA @ 6.1.24.0, ORG @ 2017.3.22.15.0.41, GRC @ 2017.3.22.15.0.55, LDC @ 2017.3.22.15.1.8, DYMA_XML @ 2017.3.22.15.0.30, NAS @ 2017.3.22.15.1.22 - Config: 0 - Node: OPRISK_DATACOLLECTOR",
"ExpiryDate":"31/01/2030",
"count_key":"0",
"id_Us":"1",
"is_popup":"0",
"tot_messages":"0",
"my_messages":"0",
"product":"0"
},
"RESPONSE_HEADER":{
"SessionID":"wVBeuYoPs1531497370SOhUMF0001",
"NomeRichiesta":"LOGIN_REQUEST",
"ltimeStart":"15314968",
"ltimeStop":"15314986",
"ldate_null":"19900101",
"product":"1",
"server_name":"OPRISK_DATACOLLECTOR",
"cell_context_id":"537554",
"operation_key":"1000000",
"operation_sub_num":"-1"
}
}
}
这是响应头:
{set-cookie: JSESSIONID=7E9295D8288C42221B1AF7935EF60EB4; Path=/HongLeong; HttpOnly,OpRiskSessionID=wVBeuYoPs1531497370SOhUMF0001, transfer-encoding: chunked, date: Wed, 13 Jul 2022 07:31:49 GMT, vary: Accept-Encoding,Accept-Encoding, content-encoding: gzip, content-type: text/html;charset=UTF-8, server: Apache-Coyote/1.1}
最后是GET请求的代码:
void getMessageBoard() async {
final response = await http.get(
Uri.parse("http://192.168.1.8:8080/HongLeong/MENU_REQUEST.do?_dc=1657668533900&table_id=25018&id_MenuAction=3&reset_context=1&ViewType=MENU_REQUEST&gui_open_popup=1&id_Window=2&activeWindowId=mw_2&noOrigUserDate=true&LocalDate=20220713&LocalTime=07285300&TimeZone=Asia/Shanghai&UserDate=0&UserTime=0&server_name=OPRISK_DATACOLLECTOR&key_id_list=&cell_context_id=0&id_Desktop=100231&operation_key=1000013&operation_sub_num=-1&is_json=1&is_popup=0&is_search_window=0&ccsfw_conf_by_user=0&is_batch=0&previousToken=1657668514164&historyToken=1657668533898&historyUrl=1"),
);
print(response.body);
}
从这里开始,在另一个页面中,我如何使用标题或正文来发出GET请求?谢谢。
要在GET请求中添加一些自定义头,您可以像这样调用方法。
var response = await http.get(url, headers: headers);
其中头将是您自定义的已解析映射,例如:
Map<String, String> headers = {
'Content-type': 'application/json',
'Accept': 'application/json',
'Authorization': '$token',
...
};