简体   繁体   English

如何使用 POST 请求的响应标头发出新的 GET 请求? 颤振/飞镖/HTTP

[英]How do I use the response header from a POST request to make a new GET request? Flutter/Dart/HTTP

I have made a POST request and received a response body and header.我发出了 POST 请求并收到了响应正文和标头。 Now how do I use the header to make a GET request?现在如何使用标头发出 GET 请求?

This is the code for the POST Login Request.这是 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()));
    }
  }

This is the response body :这是响应正文:

{
   "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"
      }
   }
}

This is the response header :这是响应标头:

{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}

and finally this is the code to make the GET request :最后这是发出 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);
  }

From here on in another page, how do I use the header or body to make a GET request?从这里开始,在另一个页面中,如何使用标头或正文发出 GET 请求? Thanks.谢谢。

To append some of your custom headers to your GET request you can that call method somehow like this.要将您的一些自定义标头附加到您的 GET 请求中,您可以像这样调用该方法。

var response = await http.get(url, headers: headers);

Where headers will be your custom parsed map eg:标题将是您的自定义解析地图,例如:

 Map<String, String> headers = {
       'Content-type': 'application/json',
       'Accept': 'application/json',
       'Authorization': '$token',
       ...
     };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM