WebClient.UploadFileASync 的非常奇怪的行为
Very strange behaviour with WebClient.UploadFileASync
我将身份验证 cookie 存储到我的应用程序中的静态 CookieContainer 中,感谢关于此 link (1)
的回答
我实现了在 DocumentCompleted
事件上获取 CookieContainer 的代码:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (!this.webBrowser1.Document.Title.Replace(" ", string.Empty).ToLower().Contains("xxx"))
{
CookieContainer ck = GetUriCookieContainer(this.webBrowser1.Url);
validSession = ck;
Succeeded = true;
this.Close();
}
}
因为我正在使用 WebClient
,所以我需要创建它的扩展以便它可以存储 CookieContainer:
public class WebClientEx : WebClient
{
public WebClientEx()
: this(new CookieContainer())
{ }
public WebClientEx(CookieContainer c)
{
this.CookieContainer = c;
}
public CookieContainer CookieContainer { get; set; }
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
var castRequest = request as HttpWebRequest;
if (castRequest != null)
{
castRequest.CookieContainer = this.CookieContainer;
}
return request;
}
}
从 (1) 存储 CookieContainer 后,我无需身份验证即可请求页面的任何内容:
WebClientEx e = new WebClientEx(validSession);
string x = e.UploadString(new Uri("http://localhost:14590/default.aspx"), "ola");
// x now contains the html page of default.aspx
问题是当我尝试使用 UploadFileAsync
将文件上传到服务器时,我将尝试逐步描述问题:
- 打开应用程序
- 打开登录表单(默认 url 将导航到 default.aspx 页面)
- 用
WebBrowser
控件登录网站
- WebBrowser 将导航至 default.aspx 页面
- 存储 cookie
- 使用
UploadFileAsync
方法上传文件
UploadFileCompleted
事件的结果 (e.result) 是登录页面的 html 内容,这意味着我在 WebClient 中传递的 CookieContainer 不起作用。
但是请查看这些步骤,上传功能将正常运行:
- 打开应用程序
- 打开登录表单(默认 url 将导航到 default.aspx 页面)
- WebBrowser 将导航至 default.aspx 页面
- 存储 cookie
- 打开登录表单,现在 webBrowser 将直接导航到 default.aspx,因为经过身份验证的会话仍然可用。
- 使用
UploadFileAsync
方法上传文件 -> 成功
- 我确定我在第一次登录尝试时获得的 CookieContainer 是正确的,因为我可以使用 WebClient.UploadString() 或任何 HttpWebRequest 到服务器,通过我检查的方式第二次尝试时的 CookieContainer 与第一次相同。
我不明白为什么会这样?你知道为什么吗?请注意,如果我使用 WebClient.UploadString() 等其他方法,如果 cookieContainer 可用,则不会重定向回登录页面。
由于某些未知原因,CookieContainer 导致了这个问题,在观看 fiddler 之后,我意识到从 webclient
到服务器的第一次调用不包含任何 cookie,所以我去掉了 CookieContainer 并替换它有了这个:
WebClient e = new WebClient();
e.Headers.Add(HttpRequestHeader.Cookie, "cookies I get from webbrowser control");
瞧,一切都很好!
我将身份验证 cookie 存储到我的应用程序中的静态 CookieContainer 中,感谢关于此 link (1)
的回答我实现了在 DocumentCompleted
事件上获取 CookieContainer 的代码:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (!this.webBrowser1.Document.Title.Replace(" ", string.Empty).ToLower().Contains("xxx"))
{
CookieContainer ck = GetUriCookieContainer(this.webBrowser1.Url);
validSession = ck;
Succeeded = true;
this.Close();
}
}
因为我正在使用 WebClient
,所以我需要创建它的扩展以便它可以存储 CookieContainer:
public class WebClientEx : WebClient
{
public WebClientEx()
: this(new CookieContainer())
{ }
public WebClientEx(CookieContainer c)
{
this.CookieContainer = c;
}
public CookieContainer CookieContainer { get; set; }
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
var castRequest = request as HttpWebRequest;
if (castRequest != null)
{
castRequest.CookieContainer = this.CookieContainer;
}
return request;
}
}
从 (1) 存储 CookieContainer 后,我无需身份验证即可请求页面的任何内容:
WebClientEx e = new WebClientEx(validSession);
string x = e.UploadString(new Uri("http://localhost:14590/default.aspx"), "ola");
// x now contains the html page of default.aspx
问题是当我尝试使用 UploadFileAsync
将文件上传到服务器时,我将尝试逐步描述问题:
- 打开应用程序
- 打开登录表单(默认 url 将导航到 default.aspx 页面)
- 用
WebBrowser
控件登录网站 - WebBrowser 将导航至 default.aspx 页面
- 存储 cookie
- 使用
UploadFileAsync
方法上传文件 UploadFileCompleted
事件的结果 (e.result) 是登录页面的 html 内容,这意味着我在 WebClient 中传递的 CookieContainer 不起作用。
但是请查看这些步骤,上传功能将正常运行:
- 打开应用程序
- 打开登录表单(默认 url 将导航到 default.aspx 页面)
- WebBrowser 将导航至 default.aspx 页面
- 存储 cookie
- 打开登录表单,现在 webBrowser 将直接导航到 default.aspx,因为经过身份验证的会话仍然可用。
- 使用
UploadFileAsync
方法上传文件 -> 成功 - 我确定我在第一次登录尝试时获得的 CookieContainer 是正确的,因为我可以使用 WebClient.UploadString() 或任何 HttpWebRequest 到服务器,通过我检查的方式第二次尝试时的 CookieContainer 与第一次相同。
我不明白为什么会这样?你知道为什么吗?请注意,如果我使用 WebClient.UploadString() 等其他方法,如果 cookieContainer 可用,则不会重定向回登录页面。
由于某些未知原因,CookieContainer 导致了这个问题,在观看 fiddler 之后,我意识到从 webclient
到服务器的第一次调用不包含任何 cookie,所以我去掉了 CookieContainer 并替换它有了这个:
WebClient e = new WebClient();
e.Headers.Add(HttpRequestHeader.Cookie, "cookies I get from webbrowser control");
瞧,一切都很好!