C#HttpWebRequest基础连接已关闭:发送时发生意外错误 [英] C# HttpWebRequest The underlying connection was closed: An unexpected error occurred on a send

查看:1116
本文介绍了C#HttpWebRequest基础连接已关闭:发送时发生意外错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在Google搜索,并尝试了所有可以找到或想到的解决方案。我尝试加载的网站正在运行TLS1.2,我尝试对其进行测试以确保不是TLS1.2问题的其他一些网站也是如此。

  byte [] buffer = Encoding.ASCII.GetBytes(
mod = www& ssl = 1& dest = account_settings.ws
+& username = + username.Replace(, 20%)
+& pass = = + password.Replace( , 20%));

ServicePointManager.MaxServicePointIdleTime = 1000;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

HttpWebRequest WebReq =
(HttpWebRequest)WebRequest.Create(
https://secure.runescape.com/m=weblogin/login.ws);

WebReq.Method = POST;
WebReq.KeepAlive = false;

WebReq.Referer =
https://secure.runescape.com/m=weblogin/loginform.ws
+?mod = www& ssl = 1& expired = 0& dest = account_settings.ws;

WebReq.ContentType = application / x-www-form-urlencoded;
WebReq.ContentLength = buffer.Length;
Stream PostData = WebReq.GetRequestStream();
PostData.Write(buffer,0,buffer.Length);
PostData.Close();
HttpWebResponse WebResp =(HttpWebResponse)WebReq.GetResponse();
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
回复= _Answer.ReadToEnd();
curAccount ++;
if(reply.Contains( Login Success)))
{
返回true;
}
其他
{
返回false;
}

不管我怎么尝试,我总是收到异常


基础连接已关闭:发送中发生意外错误。


在我发现的更多详细信息下


身份验证失败,因为远程方已关闭传输流。



解决方案

在.Net框架的4.0版本中, ServicePointManager.SecurityProtocol 仅提供设置两个选项进行设置:




  • Ssl3:安全套接字层(SSL)3.0安全协议。

  • Tls:传输层安全性(TLS)1.0安全性协议



  • 在框架的下一版本中, SecurityProtocolType 枚举器已使用较新的Tls协议进行了扩展,因此,如果您的应用程序可以使用4.5版,还可使用:




    • Tls11:指定传输层安全性(TLS)1.1安全协议

    • Tls12:指定传输层安全性(TLS)1.2安全协议。



    因此,如果您使用的是.Net 4.5,请更改行

      ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; 

      ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 

    ,以便ServicePointManager将创建支持Tls12连接的流。



    请注意,枚举值可用作标记,因此您可以将多个协议与逻辑或结合起来

      ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | 
    SecurityProtocolType.Tls11 |
    SecurityProtocolType.Tls12;

    注意

    尝试保持您支持的协议尽可能低,并且符合当今的安全标准。 Ssll3不再被认为是安全的,并且Tls1.0 SecurityProtocolType.Tls 的使用正在下降。


    I've been Googling and trying all the solutions I could find or think of myself. The site I'm trying to load is running TLS1.2 as is a few other sites I tried to test with to make sure it wasn't a TLS1.2 issue. The other sites loaded fine.

    byte[] buffer = Encoding.ASCII.GetBytes(
        "mod=www&ssl=1&dest=account_settings.ws"
        + "&username=" + username.Replace(" ", "20%")
        + "&password=" + password.Replace(" ", "20%"));
    
    ServicePointManager.MaxServicePointIdleTime = 1000;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
    
    HttpWebRequest WebReq =
        (HttpWebRequest)WebRequest.Create(
            "https://secure.runescape.com/m=weblogin/login.ws");
    
    WebReq.Method = "POST";
    WebReq.KeepAlive = false;
    
    WebReq.Referer =
        "https://secure.runescape.com/m=weblogin/loginform.ws"
        + "?mod=www&ssl=1&expired=0&dest=account_settings.ws";
    
    WebReq.ContentType = "application/x-www-form-urlencoded";
    WebReq.ContentLength = buffer.Length;
    Stream PostData = WebReq.GetRequestStream();
    PostData.Write(buffer, 0, buffer.Length);
    PostData.Close();
    HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
    Stream Answer = WebResp.GetResponseStream();
    StreamReader _Answer = new StreamReader(Answer);
    reply = _Answer.ReadToEnd();
    curAccount++;
    if (reply.Contains("Login Successful"))
    {
         eturn true;
    }
    else
    {
         eturn false;
    }
    

    No matter what I try I keep getting the exception

    The underlying connection was closed: An unexpected error occurred on a send.

    Under more details I found

    Authentication failed because the remote party has closed the transport stream.

    解决方案

    In 4.0 version of the .Net framework the ServicePointManager.SecurityProtocol only offered two options to set:

    • Ssl3: Secure Socket Layer (SSL) 3.0 security protocol.
    • Tls: Transport Layer Security (TLS) 1.0 security protocol

    In the next release of the framework the SecurityProtocolType enumerator got extended with the newer Tls protocols, so if your application can use th 4.5 version you can also use:

    • Tls11: Specifies the Transport Layer Security (TLS) 1.1 security protocol
    • Tls12: Specifies the Transport Layer Security (TLS) 1.2 security protocol.

    So if you are on .Net 4.5 change your line

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
    

    to

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    

    so that the ServicePointManager will create streams that support Tls12 connections.

    Do notice that the enumeration values can be used as flags so you can combine multiple protocols with a logical OR

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | 
                                           SecurityProtocolType.Tls11 |
                                           SecurityProtocolType.Tls12;
    

    Note
    Try to keep the number of protocols you support as low as possible and up-to-date with today security standards. Ssll3 is no longer deemed secure and the usage of Tls1.0 SecurityProtocolType.Tls is in decline.

    这篇关于C#HttpWebRequest基础连接已关闭:发送时发生意外错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆