发送批量请求
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
应用建立的每个 HTTP 连接都会产生一定的开销。
此库支持批处理
以允许您的应用将多个 API 调用组合为一个 HTTP 请求。
在以下示例情况下,您可能需要使用批处理:
-
您要发出许多小请求,并且希望最大程度地减少 HTTP 请求开销。
-
用户在您的应用处于离线状态时对数据进行了更改,
因此您的应用需要将其本地数据与服务器同步
发送大量更新和删除信息。
注意:单个批量请求最多只能调用 1,000 次调用。
如果您需要发出更多调用,请使用多个批量请求。
注意:您不能使用
媒体上传
对象。
详细信息
您可以通过将
BatchRequest
对象,然后为要执行的每个请求调用 Queue
方法。
对于每个请求,请传入一个回调,该回调将在应用收到
对该请求的响应
回调函数的实参如下:
- 内容
- 内容响应,如果请求失败,则为
null
。
- 错误
- 错误,如果请求成功,则为
null
。
- 索引
- 单个请求的索引。
- 消息
- 包含其所有标头和内容的完整 HTTP 消息。
。
添加请求后,调用
ExecuteAsync
方法发出请求。
在以下代码段中,
系统会将两个 API 请求批量处理为一个 HTTP 请求
并且每个 API 请求都会获得一个回调:
UserCredential credential;
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { CalendarService.Scope.Calendar },
"user", CancellationToken.None, new FileDataStore("Calendar.Sample.Store"));
}
// Create the service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Google Calendar API Sample",
});
// Create a batch request.
var request = new BatchRequest(service);
request.Queue<CalendarList>(service.CalendarList.List(),
(content, error, i, message) =>
{
// Put your callback code here.
});
request.Queue<Event>(service.Events.Insert(
new Event
{
Summary = "Learn how to execute a batch request",
Start = new EventDateTime() { DateTime = new DateTime(2014, 1, 1, 10, 0, 0) },
End = new EventDateTime() { DateTime = new DateTime(2014, 1, 1, 12, 0, 0) }
}, "YOUR_CALENDAR_ID_HERE"),
(content, error, i, message) =>
{
// Put your callback code here.
});
// You can add more Queue calls here.
// Execute the batch request, which includes the 2 requests above.
await request.ExecuteAsync();
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eBatching allows you to combine multiple API calls into a single HTTP request to reduce overhead.\u003c/p\u003e\n"],["\u003cp\u003eThis is especially useful when dealing with many small requests or synchronizing data with numerous updates and deletes.\u003c/p\u003e\n"],["\u003cp\u003eBatch requests are limited to 1,000 calls; for larger volumes, use multiple batch requests.\u003c/p\u003e\n"],["\u003cp\u003eMedia uploads are not supported within batch requests.\u003c/p\u003e\n"],["\u003cp\u003eUtilize the \u003ccode\u003eBatchRequest\u003c/code\u003e object and its \u003ccode\u003eQueue\u003c/code\u003e method to structure and execute your batched API calls.\u003c/p\u003e\n"]]],[],null,["# Send Batch Requests\n\nEach HTTP connection that your application makes results in a certain amount of overhead.\nThis library supports batching,\nto allow your application to put several API calls into a single HTTP request.\nExamples of situations when you might want to use batching:\n\n- You have many small requests to make and would like to minimize HTTP request overhead.\n- A user made changes to data while your application was offline, so your application needs to synchronize its local data with the server by sending a lot of updates and deletes.\n\n\n**Note**: You're limited to 1,000 calls in a single batch request.\nIf you need to make more calls than that, use multiple batch requests.\n\n\n**Note** : You cannot use a\n[media upload](/api-client-library/dotnet/guide/media_upload)\nobject in a batch request.\n\nDetails\n-------\n\n\nYou create batch requests by instantiating a\n[`BatchRequest`](https://googleapis.dev/dotnet/Google.Apis/latest/api/Google.Apis.Requests.BatchRequest.html)\nobject and then calling the `Queue` method for each request you want to execute.\nWith each request, pass in a callback to be called when your application receives\nthe response to that request.\nThe callback function's arguments are:\n\ncontent\n: The content response, or `null` if the request failed.\n\nerror\n: The error, or `null` if the request succeeded.\n\nindex\n: The index of the individual request.\n\nmessage\n: The full HTTP message that includes all its headers and content.\nAfter you've added the requests, you call the `ExecuteAsync` method to make the requests.\n\n\u003cbr /\u003e\n\n\nIn the following code snippet,\ntwo API requests are batched into a single HTTP request,\nand each API request is supplied a callback: \n\n```gdscript\nUserCredential credential;\nusing (var stream = new FileStream(\"client_secrets.json\", FileMode.Open, FileAccess.Read))\n{\n credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(\n GoogleClientSecrets.Load(stream).Secrets,\n new[] { CalendarService.Scope.Calendar },\n \"user\", CancellationToken.None, new FileDataStore(\"Calendar.Sample.Store\"));\n}\n\n// Create the service.\nvar service = new CalendarService(new BaseClientService.Initializer()\n {\n HttpClientInitializer = credential,\n ApplicationName = \"Google Calendar API Sample\",\n });\n\n// Create a batch request.\nvar request = new BatchRequest(service);\nrequest.Queue\u003cCalendarList\u003e(service.CalendarList.List(),\n (content, error, i, message) =\u003e\n {\n // Put your callback code here.\n });\nrequest.Queue\u003cEvent\u003e(service.Events.Insert(\n new Event\n {\n Summary = \"Learn how to execute a batch request\",\n Start = new EventDateTime() { DateTime = new DateTime(2014, 1, 1, 10, 0, 0) },\n End = new EventDateTime() { DateTime = new DateTime(2014, 1, 1, 12, 0, 0) }\n }, \"YOUR_CALENDAR_ID_HERE\"),\n (content, error, i, message) =\u003e\n {\n // Put your callback code here.\n });\n// You can add more Queue calls here.\n\n// Execute the batch request, which includes the 2 requests above.\nawait request.ExecuteAsync();\n```"]]