Tag: webclient

WebClient下载字符串与WebBrowser View源不同

我正在创建一个C#4.0应用程序来使用Web客户端下载网页内容。 WebClientfunction public static string GetDocText(string url) { string html = string.Empty; try { using (ConfigurableWebClient client = new ConfigurableWebClient()) { /* Set timeout for webclient */ client.Timeout = 600000; /* Build url */ Uri innUri = null; if (!url.StartsWith(“http://”)) url = “http://” + url; Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out innUri); try { client.Headers.Add(“User-Agent”, “Mozilla/4.0 (compatible; MSIE […]

从Windows Phone 8中的URL获取JSON字符串

所以我试图在Windows Phone 8应用程序中从Url获​​取Json字符串。 我只需要调用一个返回此字符串的callbackurl,这就是它,但不知怎的,我已经被困在这几天了,我只是不明白该怎么做 我有一个urlparser类,包含2个方法: public void ParseJsonUrl(string url) { Uri uri = new Uri(url); WebClient webClient = new WebClient(); webClient.DownloadStringCompleted += webClient_DownloadStringCompleted; webClient.DownloadStringAsync(uri); } void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { var jsonData = JsonConvert.DeserializeObject(e.Result); Debug.WriteLine(jsonData.parameter1); } 现在我只是想显示我的Json字符串中包含的一个参数,当然我的方法会在我开始工作后执行其他操作 我在urlparser.cs文件的开头有一个名为“parameters”的类,看起来像 public class parameter { public string parameter1 { get; set; } public string parameter2 { get; […]

我的自定义SSLvalidation逻辑如何安全地处理例外的RemoteCertificateNameMismatch?

我尝试将文件上传到我的域https://vault.veodin.com/ ,该文件位于webfaction.com 当您打开此URL时,浏览器会警告您名称不匹配,因为SSL证书是针对webfaction.com而不是针对veodin.com发布的 因此,当我尝试使用.Net WebClient将文件上载到此域时,会出现sslPolicyError System.Net.Security.SslPolicyErrors.RemoteCertificateNameMismatch。 出于我的目的,足以确保上传目标托管在* .webfaction.com上。 信任certificate.subject是否安全? 背景: 更新:我使用自定义CertificateValidationCallback来validation证书主题和证书颁发者是否符合我的预期。 ServicePointManager.ServerCertificateValidationCallback = MyCertificatePolicy.CertificateValidationCallBack; … public class MyCertificatePolicy { public static bool CertificateValidationCallBack( object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { // If the certificate is a valid, signed certificate, return true. if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None) { return true; } //if there is a […]

C#进度条未与下载同步(WebClient类)

我正在编写一个系统,其中包含一个小的FTP模块,它根本不是主要function,但需要…… 我必须将进度条与WebClient类事件DownloadProgressChangedEventHandler和AsyncCompletedEventHandler链接,进度条增量正常,并且ASyncCompletedEventHandler启动MessageBox(按预期),问题是进度条看到加载太慢… 问题: 我的MessageBox弹出100%(由事件处理程序启动),但是当MessageBox弹出我的进度条仅为+ -80%(但.VALUE真的是100)时,第一个虽然我有,但是他们添加了一个Windows Vista中的“平滑”效果会使进度条相对于其真实值减慢速度。 如果您有任何人遇到同样的问题,感谢您的帮助。

跨多个HttpWebRequests使用相同的CookieContainer是否安全?

我正在做一种WebCrawler,我需要在请求之间保持Cookies状态。 我下载所有页面异步创建新的HttpWebRequest实例,但设置相同的CookieContainer。 页面可以写入和读取cookie。 我可以安全地做吗? 有没有任何替代方法不是CookieContainer的子类并在所有方法中放置锁? MSDN说这个类不是线程安全的,但在实践中,我能做到吗?

当服务器返回错误时,使用WebClient访问响应主体的任何方法?

当使用WebClient类时响应状态为4xx时,是否有办法访问响应主体,例如: (webClient, evt) => // this is the event handler for the UploadStringCompleted event { if (evt.Error != null) { // can I access the response text? } });

如何在不阻止UI的情况下使用WebClient?

有人可以指向我一个教程或提供一些示例代码来调用System.Net.WebClient().DownloadString(url)方法,而不会在等待结果时冻结UI吗? 我认为这需要用线程来完成? 是否有一个简单的实现我可以使用没有太多的开销代码? 谢谢! 实现了DownloadStringAsync,但UI仍然处于冻结状态。 有任何想法吗? public void remoteFetch() { WebClient client = new WebClient(); // Specify that the DownloadStringCallback2 method gets called // when the download completes. client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(remoteFetchCallback); client.DownloadStringAsync(new Uri(“http://www.google.com”)); } public void remoteFetchCallback(Object sender, DownloadStringCompletedEventArgs e) { // If the request was not canceled and did not throw // an […]

如何为WebClient.DownloadFileAsync捕获404 WebException

这段代码: try { _wcl.DownloadFile(url, currentFileName); } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null) if ((ex.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound) Console.WriteLine(“\r{0} not found. “, currentFileName); } 下载文件并通知是否发生404错误。 我决定异步下载文件: try { _wcl.DownloadFileAsync(new Uri(url), currentFileName); } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null) if ((ex.Response as HttpWebResponse).StatusCode == […]

使用WebClient快速失败

使用修改后的WebClient ,我定期从具有以下特征的服务下载数据: 数据下载(~1GB)大约需要20分钟 有时服务决定不返回任何数据(请求挂起),或者返回第一个字节需要几分钟到几小时。 如果服务在合理(可配置)的时间内没有返回任何数据,我希望快速失败,同时允许有足够的时间进行下载,以便取得成功。 似乎WebRequest.Timeout属性控制请求完成的总时间,而ReadWriteTimeout控制数据传输开始后读取数据的总时间。 我是否缺少一个属性来控制建立连接和返回第一个字节之间的最长等待时间? 如果没有这样的财产,我该如何处理这个问题呢?

使用WebClient.DownloadFileAsync时如何处理exception

我通过以下方式使用WebClient从Internet下载一些文件: try { ManualResetEvent mr = new ManualResetEvent(false); mr.Reset(); using (WebClient wc = new WebClient()) { wc.DownloadFileCompleted += ((sender, args) => { if (args.Error == null) { File.Move(filePath, Path.ChangeExtension(filePath, “.jpg”)); mr.Set(); } else { //how to pass args.Error? } }); wc.DownloadFileAsync(new Uri(string.Format(“{0}/{1}”, Settings1.Default.WebPhotosLocation, Path.GetFileName(f.FullName))), filePath); mr.WaitOne(); } } catch (Exception ex) { //Catch my error […]