|
|
| |
|
在Delphi中使用XMLHttpRequest进行HTTPS请求 | |
| 阅读 91 次 更新时间:2025/2/28 | |
|
在Delphi中使用 在Delphi中使用 delphi
uses
ComObj, SysUtils;
procedure TForm1.Button1Click(Sender: TObject);
var
XMLHttp: OleVariant;
ResponseText: WideString;
begin
try
// 创建XMLHttpRequest对象
XMLHttp := CreateOleObject('MSXML2.XMLHTTP');
// 配置请求方法和URL
XMLHttp.Open('GET', 'https://example.com/api/data', False);
// 发送请求
XMLHttp.Send;
// 检查请求状态
if XMLHttp.Status = 200 then
begin
// 获取响应文本
ResponseText := XMLHttp.ResponseText;
ShowMessage(ResponseText);
end
else
begin
ShowMessage('请求失败,状态码: ' + IntToStr(XMLHttp.Status));
end;
except
on E: Exception do
ShowMessage('发生错误: ' + E.Message);
end;
end;
代码说明:
注意事项:
procedure TForm1.Button2Click(Sender: TObject); var XMLHttp: OleVariant; jsonstr:string; begin jsonstr := '{"companycode":"'+edit1.text+'","companypassword":"'+edit2.text+'"}'; XMLHttp := CreateOleObject('MSXML2.XMLHTTP'); XMLHttp.Open('POST', 'https://XXXXXy', False); XMLHttp.SetRequestHeader('Content-Type', 'application/json'); XMLHttp.Send(jsonstr); // 发送POST数据 //ShowMessage(XMLHttp.ResponseText); // 显示服务器返回的响应 memo1.Text:=xmlhttp.ResponseText; end; | |
|
|
|