gpt4 book ai didi

c# - 来自 Asp.net 的 WebClient 给出 "An existing connection was forcibly closed by the remote host"错误

转载 作者:太空狗 更新时间:2023-10-29 19:52:12 25 4
gpt4 key购买 nike

我正在尝试发送到我们的星号框以解析电话列表

从一个控制台应用程序这有效:

 class Program
{
static void Main(string[] args)
{


Console.WriteLine( HttpPost());
System.Threading.Thread.Sleep(10000);
}

public static string HttpPost()
{
var URI = @"http://sip.ligmarine.com/admin/config.php?quietmode=on&type=tool&display=printextensions";
var Parameters = "display=printextensions&quietmode=on&type=tool&core=core&featurecodeadmin=featurecodeadmin&paging=paging";
var retVal = "";
WebClient wc = new WebClient();

wc.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("maint:password")));
wc.Headers.Add("referer", @"http://sip.ligmarine.com/admin/config.php?type=tool&display=printextensions");
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

retVal = Convert.ToBase64String(Encoding.ASCII.GetBytes("maint:password"));
//Console.Write("Resulting Request Headers: ");
//Console.WriteLine(wc.Headers.ToString());
byte[] byteArray = Encoding.ASCII.GetBytes(Parameters);
//Console.WriteLine("Uploading to {0} ...", URI);
// Upload the input string using the HTTP 1.0 POST method.
byte[] responseArray = wc.UploadData(URI, "POST", byteArray);
// Console.WriteLine("\nResponse received was {0}", );

retVal = Encoding.ASCII.GetString(responseArray);

return retVal;
}
}

从我们的 IIS6 托管 ASP.NET 页面我得到

现有连接被远程主机强行关闭 说明:当前网页执行过程中出现未处理的异常
要求。请查看堆栈跟踪以获取有关错误和
的更多信息 它起源于代码。

 Exception Details: System.Net.Sockets.SocketException: An existing connection was      forcibly closed by the remote host

Source Error:

Line 37: //Console.WriteLine("Uploading to {0} ...", URI);
Line 38: // Upload the input string using the HTTP 1.0 POST method.
Line 39: byte[] responseArray = wc.UploadData(URI, "POST", byteArray);
Line 40: // Console.WriteLine("\nResponse received was {0}", );
Line 41:

HttpPost 方法与页面加载完全相同:

protected void Page_Load(object sender, EventArgs e)
{

var ret = HttpPost();
Response.Write(ret);
}

最佳答案

我有非常相似的情况,但解决方案不同。在我的 Windows 10 开发机 + 控制台应用程序上,WebClient.UploadDatahttps 地址工作正常。但是,当相同的函数被复制到 ASP.NET MVC 应用程序,并发布到不同的 Web 服务器(Windows 2008 R2)时,它给出了这个异常:

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send . ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host

两个项目都使用 .NET Framework 4.6.1

通过使调用使用 TLS1.2 解决。在 UploadData 之前添加:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

Source

关于c# - 来自 Asp.net 的 WebClient 给出 "An existing connection was forcibly closed by the remote host"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2813674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com