delphi tidhttp
时间: 2023-07-24 20:00:15 浏览: 53
Delphi中的TIdHTTP是一个非常强大的组件,可以用于执行HTTP请求。以下是使用TIdHTTP实现GET和POST请求的示例代码:
1. GET请求:
```delphi
uses
IdHTTP;
function HttpGet(const AUrl: string): string;
var
IdHTTP: TIdHTTP;
begin
IdHTTP := TIdHTTP.Create(nil);
try
Result := IdHTTP.Get(AUrl);
finally
IdHTTP.Free;
end;
end;
```
使用示例:
```delphi
Memo1.Text := HttpGet('https://www.example.com');
```
2. POST请求:
```delphi
uses
IdHTTP, IdSSLOpenSSL, IdMultipartFormData;
function HttpPost(const AUrl: string; const AParams: TIdMultipartFormDataStream): string;
var
IdHTTP: TIdHTTP;
begin
IdHTTP := TIdHTTP.Create(nil);
try
IdHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
Result := IdHTTP.Post(AUrl, AParams);
finally
IdHTTP.Free;
end;
end;
```
使用示例:
```delphi
var
Params: TIdMultipartFormDataStream;
begin
Params := TIdMultipartFormDataStream.Create;
try
Params.AddFormField('username', 'john');
Params.AddFormField('password', '123456');
Memo1.Text := HttpPost('https://www.example.com/login', Params);
finally
Params.Free;
end;
end;
```
以上代码仅供参考,具体实现方式可能因实际需求而有所不同。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)