title: calling webservice within another webservice
body: <p>Web service A is developed in the classic .NET Framework 4.6.1 and web service B is developed in .netcore 3.1. </p>
  
  <p>Web service A is trying to consume Web service B but throws an error "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."</p>
  
  <p>I used postman to consume webservice b and was successfull
  created console application to consume web service b and was successful.</p>
  
  <p>here is an example </p>
  
  <p>This is developed using .netCore3.1</p>
  
  <pre><code>namespace WebServiceBNetCore.Controllers
  {
  [ApiController]
  [Route("[controller]")]
  public class TestController: ControllerBase
  {
  [HttpPost, Route("NetCoreEndpoint")]
  public string test([FromBody]AssignModel model)
  {
  return string.Format("you entered {0}",model.Value);
  }
  }
  public class AssignModel
  {
  public string Value { get; set; }
  }
  </code></pre>
  
  <p>calling it from postman returns the desired output.
  <a href="http://localhost:5000/test/NetCoreEndpoint" rel="nofollow noreferrer">http://localhost:5000/test/NetCoreEndpoint</a></p>
  
  <pre><code>{"Value": "9"}
  </code></pre>
  
  <p>The following webservice is developed with .net framework 4.6.1</p>
  
  <pre><code>namespace WcfServiceREST
  {
  [ServiceContract]
  public interface IService1
  {
  [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, UriTemplate = "Test")]
  string GetDataFromMeAndNetCoreWS(int value);
  }
  }
  
  namespace WcfServiceREST
  {
  public class Service1 : IService1
  {
  public string GetDataFromMeAndNetCoreWS(int value)
  {
  string mydata  = string.Format("You entered: {0} in classic .Net 4.6.1", value);
  string json = "{\"Value\": \"9\"}";
  string netcoreData = PostToRESTfulService("http://localhost:5000/test/NetCoreEndpoint", json­­);
  return (mydata + netcoreData);
  }
  
  private string PostToRESTfulService(string address, string 
  jsonParameters, bool do_app_json = false, bool acceptHeaderAsXML = false)
  {
      string response;
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
      request.Method = "POST";
  
      if (do_app_json)
      {
          request.ContentType = @"application/json";
      }
      else
      {
          request.ContentType = @"text/json; charset=utf-8";
      }
      if (acceptHeaderAsXML)
      {
          request.Accept = "application/xml";
      }
      request.Timeout = 3600000;// 1 hour
      try
      {
          using (var streamWriter = new StreamWriter(request.GetRequestStream()))
          {
              string json = jsonParameters;
              streamWriter.Write(json);
              streamWriter.Flush();
              streamWriter.Close();
          }
          HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse(); &lt;------error occurs here 
          using (StreamReader reader = new StreamReader(httpResponse.GetResponseStream()))
          {
              response = reader.ReadToEnd();
          }
      }
      catch (WebException ex)
      {
          if (ex.Response != null)
          {
              string error = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
              throw new WebException(error, ex);
          }
          throw;
      }
      catch (Exception)
      {
          throw;
      }
      return response;
  }
  }
  }
  </code></pre>