Tag: webclient

WebClient是否使用KeepAlive?

我需要向单个主机发出大约50个HTTP请求(API调用)。 性能很重要,所以我想使用HTTP KeepAlive。 WebClient是否支持此function?

C#摆脱WebClient中的Connection头

我正在使用WebClient()使用C#。 我正在测试发送的标头,我注意到会自动添加以下标头。 Connection : Keep-Alive 有什么办法可以删除吗?

使用进度从FTP下载文件 – TotalBytesToReceive始终为-1?

我正在尝试从带有进度条的FTP服务器下载文件。 文件正在下载,并且ProgressChanged事件正在调用,除非事件args TotalBytesToReceive始终为-1。 TotalBytes增加,但我无法计算没有总数的百分比。 我想我可以通过其他ftp命令找到文件大小,但我想知道为什么这不起作用? 我的代码: FTPClient request = new FTPClient(); request.Credentials = credentials; request.DownloadProgressChanged += new DownloadProgressChangedEventHandler(request_DownloadProgressChanged); //request.DownloadDataCompleted += new DownloadDataCompletedEventHandler(request_DownloadDataCompleted); request.DownloadDataAsync(new Uri(folder + file)); while (request.IsBusy) ; …. static void request_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { if (e.TotalBytesToReceive == -1) { l.reportProgress(-1, FormatBytes(e.BytesReceived) + ” out of ?” ); } else { l.reportProgress(e.ProgressPercentage, “Downloaded […]

使用ServerCertificateValidationCallback的最佳实践

我正在开发一个在两个后端服务器之间使用一些HTTP通信的项目。 服务器使用X509证书进行身份validation。 不用说,当服务器A(客户端)建立与服务器B(服务器)的连接时,存在SSL / TLSvalidation错误,因为使用的证书不是来自受信任的第三方权限。 通常,处理它的方法是使用ServicePointManager.ServerCertificateValidationCallback ,例如: ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, error) => { return cert.GetCertHashString() == “xxxxxxxxxxxxxxxx”; }; 这种方法有效,除非它不理想。 它本质上做的是覆盖应用程序完成的每个http请求的validation过程。 因此,如果另一个类将尝试运行HTTP请求,它将失败。 此外,如果另一个类为了自己的目的重写ServicePointManager.ServerCertificateValidationCallback ,那么我的通信开始突然失败。 想到的唯一解决方案是创建一个单独的AppDomain来执行客户端HTTP请求。 这可行,但实际上 – 只有这样才能执行HTTP请求是愚蠢的。 开销将是惊人的。 考虑到这一点,有没有人研究过.NET中是否有更好的实践,这将允许访问Web服务,同时处理客户端SSL / TLSvalidation而不影响其他Web客户端?

如何指定用于WebClient类的SSL协议

我有一个应用程序使用HTTPS POST将数据发送到服务器。 我使用System.Net.WebClient对象来执行此操作。 这是一个发送一些数据的函数: private byte[] PostNameValuePairs(string uri, NameValueCollection pairs) { byte[] response; String responsestring = “”; using (WebClient client = new WebClient()) { client.Headers = GetAuthenticationHeader(); string DataSent = GetNameValueCollectionValuesString(pairs); try { response = client.UploadValues(uri, pairs); responsestring = Encoding.ASCII.GetString(response); } catch (Exception e) { responsestring = “CONNECTION ERROR: ” + e.Message; return Encoding.ASCII.GetBytes(responsestring); } […]

.NET:我是否需要在异步下载时保留对WebClient的引用?

我在一段生产代码中使用以下方法: private void DownloadData(Uri uri) { WebClient webClient = new WebClient(); DownloadDataCompletedEventHandler eh = null; eh = delegate(object sender, DownloadDataCompletedEventArgs e) { webClient.DownloadDataCompleted -= eh; ((IDisposable) webClient).Dispose(); OnDataDownloaded(); }; webClient.DownloadDataCompleted += eh; webClient.DownloadDataAsync(uri); } 我现在担心,在调用DownloadDataCompleted事件之前, WebClient实例被垃圾收集可能导致难以重现的错误:退出我的DownloadData()方法后,没有对WebClient对象的明显引用,因此可能会发生这种情况。 所以我的问题是:这可以实际发生吗? 我无法重现该问题,因此可能会发生一些内部事情阻止WebClient对象被垃圾收集(例如,对象可能在等待响应时在某处注册自己的全局对象)。 代码在.NET 2.0上运行,如果这有任何区别。

将WebClient与socks代理一起使用

有没有办法在WebClient使用socks代理? 特别是它提供的DownloadString方法? 我不想使用任何第三方的东西,如privoxy,freecap,我不能使用像Chilkat那样的商业库。 我尝试使用来自http://www.mentalis.org/的东西,实际上我使用了他们的WebRequest实现,但它们似乎没有类似于WebClient的东西。

从txt文件加载URL并在浏览器同步WebClient httpRequest中加载URL

我从微软的这个例子开始使用Windows Phone编程: http : //code.msdn.microsoft.com/wpapps/Hybrid-Web-App-75b7ef74/view/SourceCode 该应用仅显示浏览器并加载URL。 现在我想直接从.txt文件加载其他URL。 例如: http : //www.test.de/appurl.txt然后我想在Windows Phone应用程序中加载URL。 – >例如: http : //anotherserver.de/index.html?mobileApp 我的问题是,URL必须加载同步而不是异步。 我实现了一个AutoResetEvent,但它不起作用…… 希望有人可以帮助我,谢谢! 这是我的代码: public partial class MainPage : PhoneApplicationPage { // URL zur WebApp // TODO: URL muss aus diesem TEXT-File ausgelesen werden! private string _appURL = “http://www.test.de/appurl.txt”; public string _homeURL = “”; //private string _homeURL = […]

使用WebClient上载文件目录

我搜索并搜索过,找不到办法。 我在要上传的目录中有文件。 文件名不断变化,因此我无法按文件名上传。 这是我尝试过的。 using (WebClient client = new WebClient()) { client.Credentials = new NetworkCredential(“User”, “Password”); foreach (var filePath in files) client.UploadFile(“ftp://site.net//PICS_CAM1//”, “STOR”, @”PICS_CAM1\”); } 但我收到编译器错误: “文件”这个名称在当前上下文中不存在 我所研究的一切都说这应该有效。 有没有人有通过WebClient上传文件目录的好方法?

使用webclient.DownloadFileAsync下载的文件有0KB

我正在尝试使用WebClient下载zend-framework(来自http://framework.zend.com/releases/ZendFramework-1.11.11/ZendFramework-1.11.11.zip ) string url = “http://framework.zend.com/releases/ZendFramework-1.11.11/ZendFramework-1.11.11.zip”; WebClient downloader= new WebClient(); downloader.DownloadFileAsync(new Uri(url), “C:\\temp.zip”); 该文件已创建,但它是空的。 我使用fiddler检查了响应,我得到HTTP 200,正确的内容长度,但“connection:closed”,fiddler在“body”列中显示“-1”。 我尝试将用户代理(从谷歌浏览器请求复制)和“连接:保持活动”添加到标题,但这些都没有帮助。 我也很确定,我的程序使用相同的URL一次或两次下载此文件。 WebClient触发的事件没有错误。 有任何想法吗?