【问题标题】:C#.NET 4.0 - HttpWebRequest.GetRepsonse() does not reach web serviceC#.NET 4.0 - HttpWebRequest.GetRepsonse() 无法访问 Web 服务
【发布时间】:2021-06-07 08:58:46
【问题描述】:

我在我们的服务器上运行了一个 .Net Core 2.1 Web API。现在我有一个 .NET 4.0 应用程序,我想在其中使用 Web 服务。

当我在本地主机上本地运行它时,我设法从 api 获取数据。将api部署到服务器,修改应用中的url后,还是不行。

当我调用 WebRequest.GetRepsonse() 时,什么都没有发生,直到它超时并抛出带有 timoeout 消息的异常。

Web API 在 IIS 上运行,我已经在服务器上启用了 TLS 1.0、TLS 1.1、TLS 1.2。

当我从 Postman 发出 POST 请求时,它工作正常。我们正在使用代理进行互联网连接,但由于客户端和服务器都在同一个网络中,我认为它不可能是代理设置。我也尝试设置代理设置,但仍然无法正常工作。

我现在没有想法了。有人可以帮忙吗?

这是我的代码:

客户端应用程序 C#.NET 4.0:

        var resultDt = new DataTable();

        var url = "https://myServer:5100/api/Service/Execute";
        var request = (HttpWebRequest)WebRequest.Create(url);
        request.Credentials = CredentialCache.DefaultCredentials;
        request.UseDefaultCredentials = true;
        
        request.ContentType = "application/json";
        request.Method = "POST";
        
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | (SecurityProtocolType)3072;

        try
        {            
            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                string json = string.Format("{{\"CommandText\":\"{0}\"}}", query);
                streamWriter.Write(json);
            }

            using(var response = (HttpWebResponse)request.GetResponse())
            {
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    var result = reader.ReadToEnd();
                    if (!string.IsNullOrEmpty(result))
                    {
                        var rows = JArray.Parse(result);
                        foreach (var row in rows.Children())
                        {
                            var columns = row.Children<JProperty>();
                            if (resultDt.Columns.Count == 0)
                            {
                                foreach (var column in columns)
                                {
                                    resultDt.Columns.Add(new DataColumn()
                                    {
                                        ColumnName = column.Name,
                                        DataType = typeof(string)
                                    });
                                }
                            }

                            var newRow = resultDt.NewRow();
                            foreach (var column in columns)
                            {
                                newRow[column.Name] = column.Value;
                            }
                            resultDt.Rows.Add(newRow);
                        }
                    }
                }
            }

            
            
        }
        catch (Exception ex)
        {
            Logger.Instance().Fatal(ex.Message, ex);
        }

        return resultDt;

注意: 当我使用 HTTPS 时,程序挂在 request.GetRequestStream() 中。 当我使用 HTTP 时,程序在 request.GetResponse() 中挂起。

很奇怪

【问题讨论】:

  • 如果您运行的是 .NET v4.7.x 或更高版本,则无需操作 ServicePointManager.SecurityProtocol - 值 SecurityProtocol.SystemDefault 现在有结果Allows the operating system to choose the best protocol to use, and to block protocols that are not secure. Unless your app has a specific reason not to, you should use this value. 这是一个功能在 v4.6.2 和 v4.7 之间更改参考:docs.microsoft.com/en-us/dotnet/api/…
  • 必须使用 API 的应用程序是 .NET 4.0,这就是问题所在,我无法更新框架版本。
  • OK - 另一个观察结果。您已经通过 3072 的类型转换添加了 TLS1.2,但跳过了值为 768 的 TLS1.1。我知道您已经在服务器端启用了它,但是......
  • 如果存在协议协商问题,我不会期望超时。使用 fiddler 或 Postman 的拦截器之类的东西来比较 Postman 发送的内容与您的应用发送的内容。
  • 您尝试过没有凭据的请求吗?

标签: c# .net web-services webrequest


【解决方案1】:

我终于找到了解决方案:我只需要将代理设置为一个空实例:

request.Proxy = new WebProxy();

我发现在 Wireshark 中,来自 .NET 4.0 应用程序的请求正在发送到代理端口,而邮递员正在使用正确的端口。

还是谢谢

【讨论】:

    猜你喜欢
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-23
    • 2014-07-15
    • 1970-01-01
    相关资源
    最近更新 更多