现有页面我已经使用这个代码如何执行网址或超链接不留使用asp.net
WebClient webClient = new WebClient();
byte[] reqHTML;
reqHTML = webClient.DownloadData(url);
执行的URL。在这里,我有一个问题,而使用此代码,无论是否设置cookie?
现有页面我已经使用这个代码如何执行网址或超链接不留使用asp.net
WebClient webClient = new WebClient();
byte[] reqHTML;
reqHTML = webClient.DownloadData(url);
执行的URL。在这里,我有一个问题,而使用此代码,无论是否设置cookie?
如果您的意思是来自正在执行的ASP.NET页面的cookie,那么否:我很确定WebClient
在当前正在执行的Web请求中不会看到的所有 Cookie。
如果你想要这个功能,你可以使用浏览器的AJAX吗?也许通过jQuery?这应该按照标准浏览器规则来传递上下文等。
或者,您将不得不亲自处理这些cookie(即将它们复制到WebClient
中,如果需要,请将其复原)。
cookie默认情况下不会通过WebClient发送。你虽然可以写你的实现,使用cookie container:
public class CookieAwareWebClient : WebClient
{
private CookieContainer _container = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
((HttpWebRequest)request).CookieContainer = _container;
return request;
}
}