客户端之间的WebCLient ShareCookie。C#
你好。 我希望有几个webclient实例共享所有cookie。这是因为我想同时下载许多项目,而且我需要一直登录 这是我的WebClient类:客户端之间的WebCLient ShareCookie。C#,c#,.net,exception,webclient,C#,.net,Exception,Webclient,你好。 我希望有几个webclient实例共享所有cookie。这是因为我想同时下载许多项目,而且我需要一直登录 这是我的WebClient类: using System; using System.Net; namespace Rapideo_Client { /// <summary> /// A custom WebClient featuring a cookie container /// </summary> class W
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;
}
}
}
我在这段代码中得到了一个例外:
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:
我将public方法添加到我的WebCliet中,该方法将Cookies从参数复制到内部集合
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: