Home > Uncategorized > Make a HTTP request from SQL server

Make a HTTP request from SQL server

**Update**

A better approach is listed here: https://blog.dotnetframework.org/2019/09/17/make-a-http-request-from-sqlserver-using-a-clr-udf/

sqlhttp

Here is a UDF that allows HTTP GET requests from SQL server, for example
select dbo.GetHttp(‘http://fiachsapp.appspot.com/’)

A few “Gotcha’s” are;
* The HTML Content must be less than 8000 bytes, otherwise you get the error:

  0x8004271A ODSOLE Extended Procedure    Error in srv_convert.

* The COM object WinHttp.WinHttpRequest.5.1 must be installed on the server, some typical variations are WinHttp.WinHttpRequest.5
and WinHttp.WinHttpRequest. A search for the CLSID in Regedit should find the one you are using.

* You have to enable OLE Automation on the SQL server as follows;
sp_configure ‘show advanced options’, 1;
GO
RECONFIGURE;
GO
sp_configure ‘Ole Automation Procedures’, 1;
GO
RECONFIGURE;
GO

Alter function GetHttp
(
@url varchar(8000)
)
returns varchar(8000)
as
BEGIN
DECLARE @win int
DECLARE @hr  int
DECLARE @text varchar(8000)

EXEC @hr=sp_OACreate ‘WinHttp.WinHttpRequest.5.1’,@win OUT
IF @hr <> 0 EXEC sp_OAGetErrorInfo @win

EXEC @hr=sp_OAMethod @win, ‘Open’,NULL,’GET’,@url,’false’
IF @hr <> 0 EXEC sp_OAGetErrorInfo @win

EXEC @hr=sp_OAMethod @win,’Send’
IF @hr <> 0 EXEC sp_OAGetErrorInfo @win

EXEC @hr=sp_OAGetProperty @win,’ResponseText’,@text OUTPUT
IF @hr <> 0 EXEC sp_OAGetErrorInfo @win

EXEC @hr=sp_OADestroy @win
IF @hr <> 0 EXEC sp_OAGetErrorInfo @win

RETURN @text
END

 

Categories: Uncategorized
  1. Urvish Suthar
    June 28, 2011 at 11:26 am

    Thanks it’s really very useful

    Like

  2. John Neighbors
    August 1, 2011 at 11:22 pm

    I find that the noted Error in srv_convert happens if the length is > 4000. See: http://support.microsoft.com/kb/325492

    It appears this old SQL 2000 issue, which was supposed resolved in a SQL 2000 SP, exist in SQL 2005 and SQL 2008, as of August 2011. 😦

    Like

  3. September 28, 2011 at 9:20 pm

    Very helpful, thank you!

    Like

  4. March 2, 2012 at 7:34 pm

    Thanx a lot!
    I have added link to this post on my Polish SQL and C# blog
    Good solution!

    Like

  5. Ajit
    March 29, 2012 at 11:44 am

    Thanks a ton…

    Really appriciated. Excellent solution.

    Like

  6. Jason Leadbetter
    April 25, 2012 at 9:24 pm

    I’ve used this solution several times in the past…works great! Just ran into an issue with web servers that must be logged into. The login GetHttp works just fine…however any further requests appear to be denied as the web server doesn’t see me as being logged in still. Any idea?

    TIA,

    Jason

    Like

  7. March 15, 2015 at 9:26 am

    I am getting “The operation timed out ” 😦

    Like

    • piscow
      April 9, 2019 at 8:49 am

      me too…

      Like

    • piscow
      April 9, 2019 at 8:50 am

      i am required it in the trigger

      Like

  8. Kaya Smith
    April 28, 2015 at 5:44 pm

    YESSSS!!!!!!!!

    Like

  9. Cong Tran
    July 18, 2016 at 4:04 am

    Thank you very much. 🙂

    Like

  10. May 15, 2019 at 5:47 am

    9 years later, Works Like a charm. New learning curve for me towards Ole Automation Procedures. Thanks a lot man.

    Like

  1. No trackbacks yet.

Leave a comment