0

I want API A to call an endpoint on API B but they are both on the same server. Using an HTTPS endpoint like a front end app would do does not work.

Is it possible for a web server to make an HTTPS request to itself, and how?

Can I use localhost?

Should I try with HTTP?

Any other suggestion?

I would like to keep both APIs on same server. This is a Production IIS server so the solution needs to be a safe one. There is a SSL certificate on the server.

Below please see the error if it helps:

    System.Net.Http.HttpRequestException.    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at API.Controllers.StudyController.<GenerateVisitReportPDF>d__158.MoveNext() in C:\Users\...\Controllers\StudyController.cs:line 9914
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at API.Controllers.StudyController.<SubmitForApproval>d__152.MoveNext() in C:\Users\...\Controllers\StudyController.cs:line 8879
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__1`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Filters.ActionFilterAttribute.<ExecuteActionFilterAsyncCore>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()

If you need more information please let me know.

Jon
  • 9
  • 4
  • Does this answer your question? [Is it possible for a web server to make a HTTPS request to itself?](https://stackoverflow.com/questions/29236234/is-it-possible-for-a-web-server-to-make-a-https-request-to-itself) – T.S. Jul 22 '20 at 22:13
  • Of course. As long as they are hosted in different virtual applications or different sites. You can register different domain on your DNS and set different domain for both APIs. Then you can share same port and make call from API A to API B easily based on their URL. – Jokies Ding Jul 23 '20 at 06:48
  • @T.S.thank you for the link. That post is asking the same thing but does not answer the question. – Jon Jul 23 '20 at 13:13
  • @JokiesDing 1- Is your answer based on both APIs communicating on the same server? 2- Is it possible to call one of the APIs with localhost or HTTP? – Jon Jul 23 '20 at 13:22
  • Of course. But you have to ensure the API can be accessed from http://localhost. What error message did you get when you try to call that API from another one? – Jokies Ding Jul 24 '20 at 01:56
  • @JokiesDing This is the error: `The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.` I tested calling one of the APIs from Postman and I had to turn the "SSL certificate verification" setting off for it to work. I will try this in Production by making one API make the call with `handler.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;` to the other API. – Jon Jul 24 '20 at 15:37
  • @JokiesDing continuation from above comment: the code I wrote above disables SSL certificate verification. Regarding localhost, the server uses SSL certificates, so isn't that going to be conflicting somehow? Using HTTPS with localhost? If not, how would I ensure an API in Production can be accessed from localhost? – Jon Jul 24 '20 at 15:52

0 Answers0