问题描述
我正在尝试连接到仅支持 TLS 1.2 的 Web 服务.我在设置 option9 (WinHttpRequestOption_SecureProtocols
) 以使用 TLS 1.2
I am trying to connect to a web service that only supports TLS 1.2. I'm having trouble with the syntax for setting option9 (WinHttpRequestOption_SecureProtocols
) to use TLS 1.2
我试过了
EXEC sp_OASetProperty @objectID, 'Option', '2720', 9
但没有骰子.
推荐答案
不是因为它是个好主意,而是因为其他人不必弄清楚如何使用可怕的 sp_OAxxx 存储过程...
Not because it's a good idea, but so that no one else has to figure out how to use the horrid sp_OAxxx stored procedures...
这是我的古老的 HTTP 存储过程的更新 使用 WinHttp 并设置该选项.Option 属性是一个索引属性",因此使用 sp_OASetProperty 调用它是很奇怪的.
Here's an update to my ancient HTTP stored procedure to use both WinHttp and set that option. The Option property is an "indexed property" so calling it with sp_OASetProperty is wierd.
create or alter procedure get_http @url varchar(2000)
as
begin
/*
exec get_http 'https://www.bing.com'
*/
declare @hr int;
declare @win int;
declare @errorMessage varchar(2000);
begin try
EXEC @hr=sp_OACreate 'WinHttp.WinHttpRequest.5.1',@win OUT
IF @hr <> 0
begin;
set @errorMessage = concat('sp_OACreate failed ', convert(varchar(20),cast(@hr as varbinary(4)),1));
throw 60000, @errorMessage, 1;
end;
EXEC @hr=sp_OAMethod @win, 'Open',NULL,'GET',@url,'false'
IF @hr <> 0
begin;
set @errorMessage = concat('Open failed ', convert(varchar(20),cast(@hr as varbinary(4)),1));
throw 60000, @errorMessage, 1;
end;
--Option is an indexed property, so newvalue = 2048 and index = 9
--sp_OASetProperty objecttoken , propertyname , newvalue [ , index... ]
EXEC @hr=sp_OASetProperty @win, 'Option', 2048, 9
IF @hr <> 0
begin;
set @errorMessage = concat('set Option failed ', convert(varchar(20),cast(@hr as varbinary(4)),1) );
throw 60000, @errorMessage, 1;
end;
EXEC @hr=sp_OAMethod @win,'Send'
IF @hr <> 0
begin;
set @errorMessage = concat('Send failed ', convert(varchar(20),cast(@hr as varbinary(4)),1));
throw 60000, @errorMessage, 1;
end;
declare @status int
EXEC @hr=sp_OAGetProperty @win,'Status', @status out
IF @hr <> 0
begin;
set @errorMessage = concat('get Status failed ', convert(varchar(20),cast(@hr as varbinary(4)),1));
throw 60000, @errorMessage, 1;
end;
if @status <> 200
begin;
set @errorMessage = concat('web request failed ', @status);
throw 60000, @errorMessage, 1;
end;
declare @response table(text nvarchar(max));
insert into @response(text)
EXEC @hr=sp_OAGetProperty @win,'ResponseText';
IF @hr <> 0
begin;
set @errorMessage = concat('get ResponseText failed ', convert(varchar(20),cast(@hr as varbinary(4)),1));
throw 60000, @errorMessage, 1;
end;
select *
from @response
EXEC @hr=sp_OADestroy @win
IF @hr <> 0 EXEC sp_OAGetErrorInfo @win;
end try
begin catch
declare @error varchar(200) = error_message()
declare @source varchar(200);
declare @description varchar(200);
declare @helpfile varchar(200);
declare @helpid int;
exec sp_OAGetErrorInfo @win, @source out, @description out, @helpfile out, @helpid out;
declare @msg varchar(max) = concat('COM Failure ', @error,' ',@source,' ',@description)
EXEC @hr=sp_OADestroy @win;
--IF @hr <> 0 EXEC sp_OAGetErrorInfo @win;
throw 60000, @msg, 1;
end catch
end
这篇关于使用 WinHttp.WinHttpRequest.5.1 for TLS 1.2 在 SQL Server 存储过程中设置选项 9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!