SQL Server - Call a URL using GET or POST method and fetch Status Code and Response Text How to execute a GET or POST call to an external URL within a Stored Procedure (using HTTP or HTTPS protocol) and retrieve the StatusCode and ResponseText

SQL Server - Recuperare il Product Key da una installazione esistente

We already talked about the MSXML2.XMLHTTP and MSXML2.ServerXMLHTTP objects in an older post: these objects provides methods and properties for server-safe HTTP access between different Web servers and can be used to exchange data between different servers or services. In a typical SQL Server scenario, the MSXML2.ServerXMLHTTP is used to issue HTTP or HTTPS calls to external URIs to send and receive data.

If you've stumbled upon this post, you're most likely looking for that: a way to execute GET or POST requests to remote servers using the HTTP or HTTPS protocol within a T-SQL Stored Procedure. Without further ado, here's the code snippet you're looking for:

The above code sample has been specifically designed to overcome a known bug in the MSXML2.XMLHTTP and MSXML2.ServerXMLHTTP objects: they both cannot handle more than 8000 characters for their output handlers. This basically mean that you wouldn't be able to get a Response Text bigger than 8000 characters with a nvarchar(MAX) variale such as the following one:

If you try to do that, you'll run into an error or get an empty string.

To fix that, the above method creates a "temporary table variable" instead, who is then used to store the response text: such workaround does not trigger any internal errors and therefore allows the actual storage of a response text of any length.

XMLHTTP vs ServerXMLHTTP

XMLHTTP is designed for client applications and relies on URLMon, which is built upon Microsoft Win32 Internet (WinInet). ServerXMLHTTP is designed for server applications and relies on a new HTTP client stack, ServerXMLHTTP offers reliability and security and is server-safe. For more information, see the MSXML Software Development Kit (SDK) documentation.

As the name suggests, ServerXMLHTTP is recommended for server applications and XMLHTTP is recommended for client applications. XMLHTTP has some advantages such as caching and auto-discovery of proxy settings support. It can be used on Windows 95 and Windows 98 platforms, and it is well-suited for single-user desktop applications.

There are other advantages in using ServerXMLHTTP, such as the following:

  • Data can be exchanged between local and remote systems as a stream or as XML documents.
  • Because the underlying protocol is HTTP or HTTPS, data can be exchanged between the systems that are behind firewalls.
  • Can also be used to send HTTP requests from different environments such as Active Server Pages (ASP), Microsoft Visual Basic, and Microsoft Visual C++.

HTTP and HTTPS

Both ServerXMLHTTP and XMLHTTP components have limited HTTPS support in MSXML3. Specifically, they do not fully support Secure Sockets Layer (SSL) certificates, which are used for authentication. The components do support the HTTPS protocol, but the request fails if the server requires a client certificate. However, ServerXMLHTTP includes SSL certificate support since MSXML 3.0 Service Pack 1, which is a built-in package in most modern Windows and Windows Server distributions: that's why you should always use it for server-based tasks.

ServerXMLHTTP limitations

The number of instances of ServerXMLHTTP that can exist simultaneously within a single process primarily depends upon the amount of memory available for applications on the system. However, other factors, such as CPU processing capacity, or available socket connections can further limit the number of instances that can be active simultaneously.

With MSXML 3.0, the maximum number of instances that can exist simultaneously within a single process is 5,460.

Conclusions

That's about it: we sincerely hope that this short guide will help other developers and system administrators to better understand the MSXML2.XMLHTTP and MSXML2.ServerXMLHTTP objects.

 

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

4 Comments on “SQL Server - Call a URL using GET or POST method and fetch Status Code and Response Text How to execute a GET or POST call to an external URL within a Stored Procedure (using HTTP or HTTPS protocol) and retrieve the StatusCode and ResponseText

  1. Ryan, can you help create sp (sql2017) based on get method? When, sp is requested, they check data from uri and if it newest, sp insert data from request to table fields. Please, write to email please.

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.