我正在使用 http 執行如下 Head 呼叫:
Future<void> fetchModifiedDate(String fileUrl,BuildContext context) async{
if (await checkNetworkConnection(context)) {
var responseValue = [];
var response = http.head(Uri.parse(fileUrl));
response.then((value) {
responseValue = value.headers.values.toString().split(",");
modifiedDate = responseValue[1].trim();
print(value.headers.toString());
print(value.headers.values.toString());
});
}
我從 Header 得到的值如下:
- 對于 print(value.hedaers.toString()),
{x-served-by:psm100.akshar-dev.ml,連接:keep-alive,最后修改時間:2022 年 10 月 13 日星期四 00:09:35 GMT,接受范圍:位元組,日期:2022 年 11 月 2 日星期三格林威治標準時間 10:24:35,內容長度:69910,etag:“6347573f-11116”,內容型別:application/json,服務器:openresty}
- 對于 print(value.headers.values.toString()),
(psm100.akshar-dev.ml,keep-alive,星期四,2022 年 10 月 13 日 00:09:35 GMT,...,application/json,openresty)
我想要特定的標頭值,即最后修改鍵的值。我怎么才能得到它?
uj5u.com熱心網友回復:
試試下面的代碼
value.hedaers['last-modified']
uj5u.com熱心網友回復:
得到respose后你決議json了嗎?
你可以這樣做。
if (response.statusCode == 200) {
return Album.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to load album');
}
和 ALbum 是類,您可以根據回應定義它
class Album {
final int userId;
final int id;
final String title;
const Album({
required this.userId,
required this.id,
required this.title,
});
factory Album.fromJson(Map<String, dynamic> json) {
return Album(
userId: json['userId'],
id: json['id'],
title: json['title'],
);
}
}
要自動制作此類 yupe,您可以使用擴展“JSON to DART”或用戶在線工具
class Response {
Response({
this.xServedBy,
this.connection,
this.lastModified,
this.acceptRanges,
this.date,
this.contentLength,
this.etag,
this.contentType,
this.server,
});
String xServedBy;
String connection;
String lastModified;
String acceptRanges;
String date;
int contentLength;
String etag;
String contentType;
String server;
factory Response.fromJson(Map<String, dynamic> json) => Response(
xServedBy: json["x-served-by"],
connection: json["connection"],
lastModified: json["last-modified"],
acceptRanges: json["accept-ranges"],
date: json["date"],
contentLength: json["content-length"],
etag: json["etag"],
contentType: json["content-type"],
server: json["server"],
);
Map<String, dynamic> toJson() => {
"x-served-by": xServedBy,
"connection": connection,
"last-modified": lastModified,
"accept-ranges": acceptRanges,
"date": date,
"content-length": contentLength,
"etag": etag,
"content-type": contentType,
"server": server,
};
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/525834.html
上一篇:C#System.IndexOutOfRangeException:索引超出了陣列的范圍。但是當我手動檢查它時,它在陣列的范圍內
下一篇:尚不可用的資源的HTTP狀態代碼
