SFERRARO


I have some code in a SQL CLR stored procedure that calls out to a web service at UPS to obtain tracking information for a package.

The code fails on the last line, in the call to WebRequest.GetRequestStream(), with the following exception:

"System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it\r\n at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)\r\n at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)\r\n at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)\r\n --- End of inner exception stack trace ---\r\n at System.Net.HttpWebRequest.GetRequestStream()\r\n at UPSTracking.TrackShipment(String TrackingUri)"

Here is the relevant code:

WebProxy proxyObject = new WebProxy();
WebRequest request = WebRequest.Create("https://www.ups.com/ups.app/xml/Track");
request.Proxy = proxyObject;
request.Method = "POST";
request.ContentLength = strXMLData.Length;
request.ContentType = "application/x-www-form-urlencoded";
StreamWriter sw = new StreamWriter(request.GetRequestStream());

This same code works just fine when not running in the SQL CLR assembly. The assembly is marked as External, but I've also tried marking it as Unsafe, which did not work either.

Any suggestions on what the problem might be or how to troubleshoot this further would be greatly appreciated.

Thanks,

Steve F.




Re: WebRequest over HTTPS fails under SQLCLR, but works elsewhere

SFERRARO


I haven't solved this yet, but wanted to add a couple other pieces of information:

- This problem seems to only be related to making the request over SSL. The code works fine over port 80.

- The same code listed above works just fine in a standalone WinForms application (using SSL) on the same machine (my dev box).

Could SQL Server be blocking the outgoing request on port 447 Is this something I have to configure

Again, thanks in advance for any information or advice. This is driving me crazy!

Steve F.






Re: WebRequest over HTTPS fails under SQLCLR, but works elsewhere

Jens Petter Abrahamsen

This might not answer your question, in that in my case even regular http traffic didn't work from SQL CLR. Turned out the answer was to set the proxy for the webrequest in the C# code. For some reason this wasn't necessary when running under Visual Studio (I guess it uses the IE settings or something).


Since your problem is only SSL releated, I guess it's a certificate problem. On several occations (with Java) I've had to update the root certificate for the virtual machine. Maybe the SQL CLR has separate copies of certificates (for the Certificate Authorities) from the windows CLR, and this is why it works when run as a winform app.