底层连接已关闭:(HttpWebRequest) - C#

2024-03-16

我正在编写一段代码来通过 POST 请求验证用户名和密码,但收到一条错误消息,提示“底层连接已关闭”。

我正在尝试将带有 GET 请求的旧代码转换为带有 POST 请求的新代码。

我的 GET 代码工作正常(旧代码):

string url = "https://www.example.com/?username=" + username + "&password=" + password;

XmlDocument xmldoc = new XmlDocument();
ServicePointManager.ServerCertificateValidationCallback += new 
RemoteCertificateValidationCallback(customXertificateValidation);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

xmldoc.Load(url);
XmlNode name = xmldoc.SelectSingleNode("/Node/Name");

我的 POST 请求代码抛出错误(新代码):

var request = (HttpWebRequest)WebRequest.Create("https://www.example.com/authenticate.aspx");
ServicePointManager.ServerCertificateValidationCallback += new 
RemoteCertificateValidationCallback(customXertificateValidation);    
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
var postData = "user=username";
postData += "&pass=password";
var data = Encoding.ASCII.GetBytes(postData);

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;

using (var stream = request.GetRequestStream())
{
    stream.Write(data, 0, data.Length);
}

var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

Error:

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.ComponentModel.Win32Exception: The client and server cannot communicate, because they do not possess a common algorithm
at System.Net.SSPIWrapper.AcquireCredentialsHandle(SSPIInterface SecModule, String package, CredentialUse intent, SecureCredential scc)
at System.Net.Security.SecureChannel.AcquireCredentialsHandle(CredentialUse credUsage, SecureCredential& secureCredential)
at System.Net.Security.SecureChannel.AcquireClientCredentials(Byte[]& thumbPrint)
at System.Net.Security.SecureChannel.GenerateToken(Byte[] input, Int32 offset, Int32 count, Byte[]& output)
at System.Net.Security.SecureChannel.NextMessage(Byte[] incoming, Int32 offset, Int32 count)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.ConnectStream.WriteHeaders(Boolean async)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
at System.Net.HttpWebRequest.GetRequestStream()
at OdlumBrown.Members.AuthenticateUser(String username, String password) in c:\Website\example.com\App_Code\Membership.cs:line 148

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

底层连接已关闭:(HttpWebRequest) - C# 的相关文章

随机推荐

  • 我应该使用 java.text.MessageFormat 来处理没有占位符的本地化消息吗?

    我们正在本地化在 Java 5 上运行的 Web 应用程序的用户界面文本 并且对于如何输出在属性文件中定义的消息 使用的类型 遇到了困境 java util Properties http java sun com j2se 1 5 0 d
  • 宏有什么好的用途吗? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 据我所知 宏在编译器正确看到程序文本之前就重新排列了它 因此可能会导致问题 我很少在 C 代码中看到它们 大部分是在 C 代码中 我所
  • SwiftUI 工作表显示包含错误数据的工作表

    我有一个显示 1 2 和 3 的列表 点击文本时 应用程序会打开一张包含点击数字的工作表 但是 如果我点击第二行或第三行中的文本 工作表中显示的数字仍然是 1 我做错了什么 import SwiftUI struct ContentView
  • Mac OS 登录项是否带有参数?

    在 Mac OS 中 我创建了一些 AppleScript 来添加和删除启动应用程序 帐户 系统窗格下的 登录项 bin bash usr bin osascript e tell application System Events to
  • 在最后一次出现的字符处拆分然后连接

    我想分割一个attribute在最后一次出现字符时 添加一个字符串并将数组重新连接在一起 这是一个简化的demo https jsfiddle net Ldjoqtk1 1 在演示中我想拆分src最后一次出现的属性 然后添加 fx to t
  • Node.js 生成的 csv 文件显示英镑符号 (£)

    我正在用这个json2scv https github com zemirco json2csv包解析我的数据 示例 json 数据在下面的代码中描述 我正在尝试使用以下代码在我的 Node js 应用程序中生成 CSV 文件 如果我在 E
  • jquery 对话框打开时窗口向上滚动

    我正在尝试使用 jquery 1 4 和 jquery ui 1 8rc3 custom js 打开模态 jquery 对话框 在所有浏览器中 对话框打开都没有问题 但在 IE 7 和 6 中 对话框打开后 窗口会自行滚动到底部 我尝试将窗
  • 如何将 Selenium 输出导出到 PHP?

    Firefox 的 Selenium IDE 似乎没有默认导出到 PHP 我需要 Selenium 才能将其输出转换为 PHP 以在 Drupal 中使用 如何安装PHP语言导出才能达到这个目标 现在它是 mozilla 中的一个附加组件
  • 显示 GDB 中当前的汇编指令

    我正在 GDB 中进行一些汇编级调试 有没有办法让 GDB 以与显示当前源代码行相同的方式显示当前的汇编指令 每个命令后的默认输出如下所示 0x0001433f 990 Foo bar p 这给了我当前指令的地址 但我必须继续参考disas
  • Bokeh HTML 模板格式化程序不起作用

    请看一下页面 https docs bokeh org en latest docs reference models widgets tables html bokeh models widgets tables HTMLTemplate
  • Express 路由器参数验证

    Express 4x api 文档声称您可以将正则表达式作为第二个参数传递给router param http expressjs com 4x api html router param为了验证参数 该方法现在可用于有效验证参数 以及 可
  • Unity如何在玩家移动时让我的枪停止射击?

    I am making a gun shooting script but I do not know how to disable shooting when the player is running Can someone help
  • fancyBox2:幻灯片按钮和图像边框

    是否可以只有幻灯片开 关按钮和位置 标题区域内的某个地方 我想要这个的原因是 因为按钮栏缩小了显示图像的大小 我只想有这个选项 打开或关闭幻灯片 导航通过正常的左右方向完成 单击图像 是否可以减少图像周围的边框 尤其是底部边框 我尝试了填充
  • 通过 TeamViewer 连接时全屏 WPF 应用程序崩溃

    我创建了一个运行相当稳定的 WPF 应用程序 但有一个问题我无法解决 当我通过 TeamViewer 版本 11 连接到正在运行 WPF 应用程序 全屏模式 的平板电脑时 应用程序崩溃 当我结束远程连接时也会发生同样的情况 我得到以下异常
  • Ubuntu 14.02 上的 Tkinter 将宽度报告为两个显示器的总和

    我在新的 Ubuntu 14 02 机器上有两个显示器 一台显示器为 1024X768 VGA 第二台显示器是 1920X1080 HDMI 无论我做什么 root winfo screenwidth 报告 2944 两个显示器的总和 如果
  • 如何使用 Spring Security 保护 Vaadin 流应用程序

    我正在尝试将 vaadin 10 与 spring security 集成 使用 vaadin 提供的 spring 项目库 但我对它们如何准确交互感到困惑 如果我转到受保护的网址 在本例中为 about 直接在浏览器中键入它 则会显示登录
  • 如何使用jquery仅从文本中获取数字

    我只想获取数字 123 而不是文本 确认 这是我的代码 p 123confirm p 我认为 RegExp 是个好主意 var sd this text replace 0 9 gi Replace everything that is n
  • 如何使用 python 的 beautiful soup 获取标签之间的内容及其以 HTML 结尾的内容?

    我有一个 HTML 行 如下所示 span class cd headline text Is this model too thin for Yves Saint Laurent span 我想提取标题 即 这个模型对于 Yves Sai
  • 传单地图上的 d3 圆圈

    我使用以下代码生成带有圆圈的地图 http jsbin com OTaKEDor 2 edit http jsbin com OTaKEDor 2 edit
  • 底层连接已关闭:(HttpWebRequest) - C#

    我正在编写一段代码来通过 POST 请求验证用户名和密码 但收到一条错误消息 提示 底层连接已关闭 我正在尝试将带有 GET 请求的旧代码转换为带有 POST 请求的新代码 我的 GET 代码工作正常 旧代码 string url http