如何用Delphi的TNetHttpClient/TRESTClient实现合规的CSV文件PUT上传?
Delphi实现HTTP PUT上传CSV文件到指定服务器
以下分别用TNetHttpClient和TRESTClient两种组件实现需求,完全匹配你提供的curl操作逻辑:
一、使用TNetHttpClient实现
需要引用单元:System.Net.HttpClient, System.Net.HttpClientComponent, System.IOUtils
procedure UploadCSVWithNetHttpClient; var HttpClient: TNetHttpClient; Request: TNetHttpRequest; Response: IHTTPResponse; LocalFilePath: string; ClientID, RemoteFileName: string; ApiKey: string; UploadURL: string; FileContent: TBytes; begin // 配置参数 ClientID := 'd32krpq'; RemoteFileName := '20181023_update.csv'; ApiKey := 'lXViWTzFic9sM8qe9Ew7JME8xTdBAOMJHdIjK7XkjQ00OWr'; LocalFilePath := '20181023_update.csv'; // 本地CSV文件路径 // 构造上传URL UploadURL := Format('https://example.com/upload/%s/%s', [ClientID, RemoteFileName]); // 读取本地文件内容 if not TFile.Exists(LocalFilePath) then raise Exception.Create('本地CSV文件不存在'); FileContent := TFile.ReadAllBytes(LocalFilePath); // 初始化HTTP客户端 HttpClient := TNetHttpClient.Create(nil); try Request := TNetHttpRequest.Create(nil); try // 设置请求方法为PUT Request.Method := THTTPMethod.mPUT; // 添加API Key请求头 Request.CustomHeaders['x-api-key'] := ApiKey; // 设置请求体为文件内容 Request.ContentStream := TBytesStream.Create(FileContent); // 发送请求并获取响应 Response := HttpClient.Execute(Request, UploadURL); try // 检查响应状态,200-299为成功 if Response.StatusCode >= 200 and Response.StatusCode < 300 then ShowMessage('文件上传成功') else ShowMessage(Format('上传失败,状态码:%d,响应内容:%s', [Response.StatusCode, Response.ContentAsString])); finally Response := nil; end; finally Request.Free; end; finally HttpClient.Free; end; end;
二、使用TRESTClient组件实现
需要引用单元:REST.Client, REST.Types, System.IOUtils
procedure UploadCSVWithRESTClient; var RESTClient: TRESTClient; RESTRequest: TRESTRequest; RESTResponse: TRESTResponse; LocalFilePath: string; ClientID, RemoteFileName: string; ApiKey: string; UploadURL: string; begin // 配置参数 ClientID := 'd32krpq'; RemoteFileName := '20181023_update.csv'; ApiKey := 'lXViWTzFic9sM8qe9Ew7JME8xTdBAOMJHdIjK7XkjQ00OWr'; LocalFilePath := '20181023_update.csv'; // 本地CSV文件路径 // 构造上传URL UploadURL := Format('https://example.com/upload/%s/%s', [ClientID, RemoteFileName]); // 初始化REST组件 RESTClient := TRESTClient.Create(nil); RESTRequest := TRESTRequest.Create(nil); RESTResponse := TRESTResponse.Create(nil); try RESTClient.BaseURL := UploadURL; RESTRequest.Client := RESTClient; RESTRequest.Response := RESTResponse; // 设置请求方法为PUT RESTRequest.Method := rmPUT; // 添加API Key请求头 RESTRequest.Params.AddHeader('x-api-key', ApiKey); // 设置请求体为本地文件内容 RESTRequest.AddBody(TFile.ReadAllBytes(LocalFilePath), ctTEXT_CSV); // 执行请求 try RESTRequest.Execute; // 检查响应状态 if RESTResponse.StatusCode >= 200 and RESTResponse.StatusCode < 300 then ShowMessage('文件上传成功') else ShowMessage(Format('上传失败,状态码:%d,响应内容:%s', [RESTResponse.StatusCode, RESTResponse.Content])); except on E: Exception do ShowMessage('请求执行出错:' + E.Message); end; finally RESTResponse.Free; RESTRequest.Free; RESTClient.Free; end; end;
注意事项
- 确保本地文件路径正确,建议使用绝对路径避免找不到文件的问题
- 实际部署时需补充更完善的异常处理(如网络中断、权限不足等场景)
- 若服务器对请求体的Content-Type有特殊要求,可调整代码中对应的类型参数
内容的提问来源于stack exchange,提问作者Joe Wright
相关产品推荐
相关产品推荐

