【问题标题】:calling webservice within another webservice在另一个 Web 服务中调用 Web 服务
【发布时间】:2020-06-10 23:40:36
【问题描述】:

Web 服务 A 是在经典的 .NET Framework 4.6.1 中开发的,Web 服务 B 是在 .netcore 3.1 中开发的。

Web 服务 A 正在尝试使用 Web 服务 B,但抛出错误“底层连接已关闭:无法为 SSL/TLS 安全通道建立信任关系。”

我用postman消费webservice b,成功了 创建控制台应用程序以使用 Web 服务 b 并成功。

这是一个例子

这是使用.netCore3.1开发的

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; }
}

从邮递员调用它会返回所需的输出。 http://localhost:5000/test/NetCoreEndpoint

{"Value": "9"}

以下webservice是用.net framework 4.6.1开发的

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(); <------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;
}
}
}

【问题讨论】:

  • @Enigmativity 我更新了描述选项以包含一个可重现的示例
  • 抱歉,您说您正在从另一个网络服务中调用一个网络服务,但您的代码和您的示例仅显示一个网络服务。你能显示正确的代码吗?此外,您需要说明错误发生的位置。
  • @Enigmativity,对不起,我刚刚包含了每个 web 服务的端点,我更新了描述以包含完整的代码....抱歉造成混淆

标签: .net ssl .net-core


【解决方案1】:

答案正盯着我看。在我的本地环境中没有安装有效的证书,我的 .net 核心 prohect 配置是将所有 http 请求转发到 HTTPS。希望这对其他人有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 2011-02-16
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多