【问题标题】:Why code behave different in c# Interactive (Roslyn) and in debug\release mode?为什么代码在 c# Interactive (Roslyn) 和调试\发布模式下表现不同?
【发布时间】:2017-06-16 06:45:05
【问题描述】:

更新 实际上,在挖掘网络监控之后,我发现请求方式的差异是由执行 Roslyn 的代码和正常的调试\发布模式产生的。 debug\release 模式下 Fiddler 跟踪报错:

[Fiddler] The connection to 'host.com' failed.  <br/>System.Security.SecurityException Failed to negotiate HTTPS connection with server.fiddler.network.https&gt; HTTPS handshake to host.com (for #76) failed. System.IO.IOException Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. &lt; An existing connection was forcibly closed by the remote host

原始问题: 我得到了一段代码:

HttpTransportOverWebClient c = new HttpTransportOverWebClient();
c.GetURL("https://web.page/query")

GetUrl 是一些包装器:

public bool GetURL(string URL)
        {
            try
            {
                web.Headers.Add("User-Agent", browserAgent);
                byte[] data = web.DownloadData(URL);
                string characterSet = web.Response.CharacterSet;
                string encoding = (characterSet == null || characterSet == "" || characterSet.ToUpper() == "NONE") ? "UTF-8" : web.Response.CharacterSet.ToUpper();
                html = Encoding.GetEncoding(encoding).GetString(data);
                return true;
            }
            catch (Exception ex)
            {
                lastException = web.LastException == null ? new WebException(ex.Message) : web.LastException;
            }
            return false;
        }

c#Interactive 模式下的行为:

GetURL 返回 true 并且 html 包含数据

调试\发布模式下的行为:

GetURL 返回 false 并且 ex 包含以下错误:

An exception occurred during a WebClient request.
   at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
   at System.Net.WebClient.DownloadData(Uri address)
   at HttpCrawler.HttpTransportOverWebClient.GetURL(String URL) in ..HttpTransportOverWebClient.cs:line xx

内部异常:

 at System.Net.WebClient.DownloadBitsState.SetResponse(WebResponse response)
   at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream, CompletionDelegate completionDelegate, AsyncOperation asyncOp)
   at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)

请多指教

【问题讨论】:

    标签: c# https webclient tls1.2


    【解决方案1】:

    解决方案

    我想 C#interactive 以某种方式预先配置,它能够将 ServicePointManager.SecurityProtocol 默认为旧版本。作为解决方案,我添加到代码中:

    using System.Net;
    ....
    ServicePointManager.SecurityProtocol = 
           SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-14
      • 1970-01-01
      • 2015-03-25
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多