Tag: downloadstring

如何在不阻止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 […]

多语言Google Translate API正在退回(503)服务器不可用

我正在尝试使用谷歌翻译进行翻译,但它提供错误服务器不可用。 我的猜测是,当我尝试将相同的东西放在地址栏中时,我们会收到一个填写validation码。如果我们通过validation码而不是只下载一个txt文件。 我认为这可能是validation码页面的问题,而不是服务器不可用。 调用函数。 string result = TranslateGoogle(“Life is great and one is spoiled when it goes on and on and on”, “en”, “hi”); Console.WriteLine(result); 翻译谷歌function public string TranslateGoogle(string text, string fromCulture, string toCulture) { fromCulture = fromCulture.ToLower(); toCulture = toCulture.ToLower(); string[] tokens = fromCulture.Split(‘-‘); if(tokens.Length > 1) fromCulture = tokens[0]; tokens = toCulture.Split(‘-‘); if(tokens.Length […]

WebClient下载字符串(几个字符页面)太慢了

我正在尝试从URL下载字符串。 不幸的是,它很慢。 这是我的代码: // One of these types for two bad solutions anyway // byte[] result = new byte[12]; // string result; using (var webClient = new System.Net.WebClient()) { String url = “http://bg2.cba.pl/realmIP.txt”; //result = webClient.DownloadString(url); // slow as hell //webClient.OpenRead(url).Read(result, 0, 12); // even slower } 这需要大约4-5秒,这对我来说似乎非常不合适…… 该url的内容为IP XX.YYY.ZZ.FF

WebClient.DownloadString结果与浏览器结果2不匹配

以下代码: WebClient wc = new WebClient(); wc.Encoding = Encoding.UTF8; string Url = “http://www.tsetmc.com/tsev2/data/instinfodata.aspx?i=59266699437480384&c=64″; return wc.DownloadString(Url); 代码返回: Q T MP J A| ^D ~ C ” l ;I&3=j= iG H9Ȓ J ^ j T Q=HH ‘Qm 1 hF 4 * { x \o? 当我在任何网络浏览器中访问该URL时,我得到: 12:29:45,A ,3540,3567,3600,3621,3690,3515,140,238204,849582597,1,20140914,122945;;1@2825@3523@3583@1700@1,1@2000@3522@3600@8700@2,1@500@3511@3640@2500@1,;19774,99736,1 有没有办法得到正确的字符串? 另外,我使用这个在线解码器,但我没有得到正确答案: 通用在线解码器

无法解析的StackExchange API响应

我编写了一个小程序来分析我的StackExchange API中的配置文件数据,但是api会向我发送unsarse- / unreadable数据。 收到的数据:(使用c#自行下载) \ u001f \ B \ 0 \ 0 \ 0 \ 0 \ 0 \ U0004 \ 0mRMo0 \ F /:d $ C’^ {/ \ u0006 \ u0018G> \我\ u0015 \ U0004݀d> GRL’o \ u0004G%JP \ u001c-EM> 0Xbm〜 \ u0018tk \ u0014M] rdLGv0〜FJ = 1 \ u00031I> kTRA \“(/ +; […]

C#从巨大的url列表中下载数据

我有一个巨大的网页列表,显示一个状态,我需要检查。 一些url位于同一网站内,另一个url位于另一个网站上。 现在我正试图通过使用下面的代码以并行的方式做到这一点,但我觉得我造成了太多的开销。 while(ListOfUrls.Count > 0){ Parallel.ForEach(ListOfUrls, url => { WebClient webClient = new WebClient(); webClient.DownloadString(url); … run my checks here.. }); ListOfUrls = GetNewUrls….. } 这可以用更少的开销来完成,并且可以更多地控制我使用/重用的Web客户端和连接数量吗? 那么,最终工作可以更快完成吗?

DownloadStringAsync等待请求完成

我使用此代码来检索url内容: private ArrayList request(string query) { ArrayList parsed_output = new ArrayList(); string url = string.Format( “http://url.com/?query={0}”, Uri.EscapeDataString(query)); Uri uri = new Uri(url); using (WebClient client = new WebClient()) { client.DownloadStringAsync(uri); } // how to wait for DownloadStringAsync to finish and return ArrayList } 我想使用DownloadStringAsync因为DownloadString挂起了应用程序GUI,但我希望能够根据request返回结果。 我怎么能等到DownloadStringAsync完成请求?