客户端之间的 WebCLient ShareCookies。 C#

发布于 2024-10-22 22:01:40 字数 3103 浏览 1 评论 0原文

你好。 我希望有几个共享所有 cookie 的网络客户端实例。那是因为我想同时下载很多项目,并且需要一直登录。

这是我的 WebClientEx 类:

using System;
using System.Net;

namespace Rapideo_Client
{
    /// <summary>
    /// A custom WebClient featuring a cookie container
    /// </summary>

    class WebClientEx : WebClient
    {
        public static CookieContainer CookieContainer { get; private set; }

        public WebClientEx()
        {
            CookieContainer = new CookieContainer();
        }

        protected override WebRequest GetWebRequest(Uri address)
        {
            var request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                (request as HttpWebRequest).CookieContainer = CookieContainer;
            }
            return request;
        }
    }
}

这是我遇到异常的代码的一部分:

        using (WebClientEx tempWebClient = new WebClientEx())
        {
            siteTemp = tempWebClient.DownloadString("http://rapideo.pl/lista");
        }

我在这段代码中遇到的异常:

    System.Net.WebException was unhandled by user code
  Message=An exception occurred during a WebClient request.
  Source=System
  StackTrace:
       at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
       at System.Net.WebClient.DownloadString(Uri address)
       at System.Net.WebClient.DownloadString(String address)
       at Rapideo_Client.Rapideo.ReadTransferMB() in e:\documents\visual studio 2010\Projects\Rapideo Client\Rapideo Client\Rapideo.cs:line 78
       at Rapideo_Client.Rapideo.Refresh() in e:\documents\visual studio 2010\Projects\Rapideo Client\Rapideo Client\Rapideo.cs:line 50
       at Rapideo_Client.MainWindow.RefreshTimer_Tick(Object sender, EventArgs e) in e:\documents\visual studio 2010\Projects\Rapideo Client\Rapideo Client\MainWindow.xaml.cs:line 74
       at System.Timers.Timer.MyTimerCallback(Object state)
  InnerException: System.IO.IOException
       Message=Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
       Source=System
       StackTrace:
            at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)
            at System.Net.WebClient.DownloadBitsState.RetrieveBytes(Int32& bytesRetrieved)
            at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream, CompletionDelegate completionDelegate, AsyncOperation asyncOp)
            at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
       InnerException: System.Net.Sockets.SocketException
            Message=An existing connection was forcibly closed by the remote host
            Source=System
            ErrorCode=10054
            NativeErrorCode=10054
            StackTrace:
                 at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
                 at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
            InnerException: 

Helo.
I want to have few instances of webclient that all share all the cookies. That is because I want to download many items at the same time and I need to be logged in all the time.

This is my WebClientEx Class:

using System;
using System.Net;

namespace Rapideo_Client
{
    /// <summary>
    /// A custom WebClient featuring a cookie container
    /// </summary>

    class WebClientEx : WebClient
    {
        public static CookieContainer CookieContainer { get; private set; }

        public WebClientEx()
        {
            CookieContainer = new CookieContainer();
        }

        protected override WebRequest GetWebRequest(Uri address)
        {
            var request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                (request as HttpWebRequest).CookieContainer = CookieContainer;
            }
            return request;
        }
    }
}

This is part of code where I'm getting exception:

        using (WebClientEx tempWebClient = new WebClientEx())
        {
            siteTemp = tempWebClient.DownloadString("http://rapideo.pl/lista");
        }

And this exception that I'm getting in this code:

    System.Net.WebException was unhandled by user code
  Message=An exception occurred during a WebClient request.
  Source=System
  StackTrace:
       at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
       at System.Net.WebClient.DownloadString(Uri address)
       at System.Net.WebClient.DownloadString(String address)
       at Rapideo_Client.Rapideo.ReadTransferMB() in e:\documents\visual studio 2010\Projects\Rapideo Client\Rapideo Client\Rapideo.cs:line 78
       at Rapideo_Client.Rapideo.Refresh() in e:\documents\visual studio 2010\Projects\Rapideo Client\Rapideo Client\Rapideo.cs:line 50
       at Rapideo_Client.MainWindow.RefreshTimer_Tick(Object sender, EventArgs e) in e:\documents\visual studio 2010\Projects\Rapideo Client\Rapideo Client\MainWindow.xaml.cs:line 74
       at System.Timers.Timer.MyTimerCallback(Object state)
  InnerException: System.IO.IOException
       Message=Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
       Source=System
       StackTrace:
            at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)
            at System.Net.WebClient.DownloadBitsState.RetrieveBytes(Int32& bytesRetrieved)
            at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream, CompletionDelegate completionDelegate, AsyncOperation asyncOp)
            at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
       InnerException: System.Net.Sockets.SocketException
            Message=An existing connection was forcibly closed by the remote host
            Source=System
            ErrorCode=10054
            NativeErrorCode=10054
            StackTrace:
                 at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
                 at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
            InnerException: 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一百个冬季 2024-10-29 22:01:41

我向 WebCliet 添加了公共方法,将 Cookies 从参数复制到内部集合

I added public method to my WebCliet that copies Cookies from argument to internal collection

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文