0

我們無法通過SQL Server 2000上的存儲過程發送http文章。SQL是我們必須發送http文章的唯一方法。Http post SQL Server 2000存儲過程

我們顯然沒有發送[fin,ack],並且連接不夠快速關閉。誰能告訴我問題是什麼?

CREATE procedure HTTP_POST(@sUrl varchar(200), @response varchar(8000) out) 
As 

Declare 
@obj int 
,@hr int 
,@status int 
,@msg varchar(255) 

exec @hr = sp_OACreate 'MSXML2.ServerXMLHttp', @obj OUT 

exec @hr = sp_OAMethod @obj, 'open', NULL, 'POST', @sUrl, false 
if @hr <>0 begin set @msg = 'sp_OAMethod Open failed' goto eh end 

exec @hr = sp_OAMethod @obj, 'setRequestHeader', NULL, 'Content-Type', 
'application/x-www-form-urlencoded' 
if @hr <>0 begin set @msg = 'sp_OAMethod setRequestHeader failed' goto 
eh end 

exec @hr = sp_OAMethod @obj, send, NULL, '' 
if @hr <>0 begin set @msg = 'sp_OAMethod Send failed' goto eh end 

exec @hr = sp_OAGetProperty @obj, 'status', @status OUT 
if @hr <>0 begin set @msg = 'sp_OAMethod read status failed' goto 
eh 
end 

if @status <> 200 begin set @msg = 'sp_OAMethod http status ' + 
str(@status) goto eh end 

exec @hr = sp_OAGetProperty @obj, 'responseText', @response OUT 
if @hr <>0 begin set @msg = 'sp_OAMethod read response failed' goto 
eh end 

exec @hr = sp_OADestroy @obj 
return 
eh: 

exec @hr = sp_OADestroy @obj 
return 
GO 

我調用存儲過程像這樣

exec HTTP_POST 'http://123.123.123.123/example.webservice?time=201205021000000' 
+0

你可以在SQL之外測試(而不是生產)嗎?如果是這樣,如果從VB腳本運行,你會得到相同的問題?你能執行一個xp_cmdshell並運行一個腳本嗎? –

+0

這可能是一個選項,雖然是否可以通過使用此存儲過程強制關閉連接?我想如果連接被強制關閉,這可能會解決我的問題 – Imsopov

+0

我不後悔。我只是提供一種方法來首先找出問題。 –

回答

1

我猜問題是與底層的O/S(Windows)中;在處理連接後很可能會使TCP連接長時間處於打開狀態。

要驗證,檢查netstat -a -p tcp執行您的sproc之前和之後;尋找連接並查看它何時死亡。我猜測你的sproc執行完成後,連接會消失,坐在TIME_WAIT狀態。 Windows將保持一個連接活着2-4分鐘IIRC。一種可能抵消這種情況的方法是添加自定義HTTP頭(Connection: Close),以便遠程服務器在執行後關閉連接。一些「常規」代碼看起來像這樣,你只需要適應它到你的SQL函數:serverXMLHTTP.setRequestHeader("Connection","Close");

如果這一切都行不通,我會使用Wireshark來跟蹤連接和哪些數據包發送時幫助找出問題。祝你好運

+0

它保持連接打開。我不得不將結果從我們的應用程序移到一張表中分開處理。現在工作正常 – Imsopov