Tag: webclient

使用WebClient.UploadFileAsync时,某些计算机上的文件未上载到FTP服务器

我正在尝试将文件上传到FileZilla FTP服务器。 我在我的电脑上设置了服务器,我使用一个简单的C#脚本来上传文件。 当我从同一台机器运行脚本时,它可以正常工作。 当我尝试从相同或不同的局域网中的另一台计算机运行该脚本时,我遇到了以下问题: (not logged in) (ClientIP)> USER userID (not logged in) (ClientIP)> 331 Password required for userID (not logged in) (ClientIP)> PASS *************** userID (ClientIP)> 230 Logged on userID (ClientIP)> OPTS utf8 on userID (ClientIP)> 202 UTF8 mode is always enabled. No need to send this command. userID (ClientIP)> PWD userID (ClientIP)> […]

UploadValuesAsync响应时间

我正在编写测试工具来测试HTTP Post。 测试用例将在10秒间隔内使用webclient类中的UploadValuesAsync发送8个http请求。 它在每8个请求后睡10秒。 我正在记录每个请求的开始时间和结束时间。 当我计算平均响应时间时。 我大约800毫秒。 但是当我在Web客户端中使用UploadValues方法同步运行此测试用例时,我的平均响应时间为250毫秒。 你能告诉我为什么这两种方法有区别吗? 我期待Aync的响应时间减少,但我没有得到。 这是发送8个请求异步的代码 var count = 0; foreach (var nameValueCollection in requestCollections) { count++; NameValueCollection collection = nameValueCollection; PostToURL(collection,uri); if (count % 8 == 0) { Thread.Sleep(TimeSpan.FromSeconds(10)); count = 0; } } UPDATED这是发送8个SYNC请求的代码 public void PostToURLSync(NameValueCollection collection,Uri uri) { var response = new ServiceResponse { Response = […]

将文件上载到文件服务器

使用下面的链接,我为我的应用程序编写了一个代码。 我无法做到正确,请参考链接并帮助我使用它… 使用webclient类将文件上载到文件服务器 以下是我的代码: – protected void Button1_Click(object sender, EventArgs e) { filePath = FileUpload1.FileName; try { WebClient client = new WebClient(); NetworkCredential nc = new NetworkCredential(uName, password); Uri addy = new Uri(“\\\\192.168.1.3\\upload\\”); client.Credentials = nc; byte[] arrReturn = client.UploadFile(addy, filePath); Console.WriteLine(arrReturn.ToString()); } catch (Exception ex) { Console.WriteLine(ex.Message); } } 我还使用过: – File.Copy(filePath,“\\ 192.168.1.3 \ […]

WebClient.DownloadProgressChanged:Console.WriteLine()阻止UI线程

我有以下简单的代码: private void btn_download_Click(object sender, EventArgs e){ WebClient client = new WebClient(); client.DownloadProgressChanged += client_DownloadProgressChanged; client.DownloadFileAsync(new Uri(“http://…/file.zip”), “file.zip”); } void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e){ //Prints: “Downloaded 3mb of 61.46mb (4%)” Console.WriteLine(“Downloaded ” + ((e.BytesReceived / 1024f) / 1024f).ToString(“#0.##”) + “mb” + ” of ” + ((e.TotalBytesToReceive / 1024f) / 1024f).ToString(“#0.##”) + “mb” + ” (” […]

在C#4中为ProgressChanged事件添加额外参数

这是我的代码(Simple Downloader): public void DownloadFile(string urlAddress, string location) { using (webClient = new WebClient()) { webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); Uri URL = urlAddress.StartsWith(“http://”, StringComparison.OrdinalIgnoreCase) ? new Uri(urlAddress) : new Uri(“http://” + urlAddress); try { webClient.DownloadFileAsync(URL, location); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } 这是我的问题: 如何在ProgressChanged事件中添加额外的参数( int RowNumber )? 我的意思是这样的: […]

在WebClient中加载动态生成的HTML代码

好吧,我正在使用WebClient.DownloadString来废弃网页,不幸的是, DownloadString让我获得了没有CSS和JS更新的页面源代码(在页面加载时在Internet Explorer中创建)。 所以我想知道如何使用WebClient以与Internet Explorer或WebBrowser控件相同的方式加载整个页面? (使用css和js代码注入)

使用WebClient保存具有适当扩展名的图像

我需要检索并将图像从网站保存到我的本地文件夹。 图像类型在.png,.jpg和.gif之间变化 我试过用过 string url = @”http://redsox.tcs.auckland.ac.nz/CSS/CSService.svc/”; string saveLoc = @”/project1/home_image”; using (var wc = new WebClient()) { wc.DownloadFile(url, saveLoc); } 但这会将文件’home_image’保存在没有扩展名的文件夹中。 我的问题是你如何确定扩展名? 有一个简单的方法吗? 可以使用HTTP请求的Content-Type吗? 如果是这样,你怎么做?

Dotnet webclient超时但浏览器为json webservice工作文件

我试图使用以下代码将以下json webservice https://mtgox.com/code/data/getDepth.php的结果转换为字符串。 using (WebClient client = new WebClient()) { string data = client.DownloadString(“https://mtgox.com/code/data/getDepth.php”); } 但它总是返回超时exception而没有数据。 我计划使用fastjson将响应转换为对象,并期望这是一个困难的部分而不是返回页面的内容。 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(“https://mtgox.com/code/data/getDepth.php”); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (StreamReader sr = new StreamReader(response.GetResponseStream())) { string data = sr.ReadToEnd(); } } 也导致了同样的错误。 谁能指出我做错了什么?

WebClient.OpenFileAsync是否触发DownloadProgressChanged

根据 http://msdn.microsoft.com/en-us/library/system.net.webclient.downloadprogresschanged.aspx , OpenFileAsync应该在每次进行时触发DownloadProgressChanged。 我根本无法解雇它。 不过,可以使用DownloadDataAsync和DownloadFileAsync来解决问题。 这是一个简单的例子: using System; using System.Net; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { WebClient client = new WebClient(); client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted); client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged); client.OpenReadAsync(new Uri(“http://www.stackoverflow.com”)); Console.ReadKey(); } static void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { Console.WriteLine(“{0}% Downloaded”, e.ProgressPercentage); } static void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs […]

如何等待webclient OpenReadAsync完成

我在Windows Phone 8.1应用程序中使用WebClient从Internet下载一些东西。 下面是我在我的应用程序中使用的示例代码 – 我在下面调用方法,但我的webclient没有等待完成读取操作并在OpenReadAsync调用后立即返回。 我怎样才能确保我的方法返回操作必须等到OpenReadCompleted事件完成后? 我见过多个类似的问题,但找不到解决方案。 MyCustomObject externalObj; // my custom object private static void CheckNetworkFile() { try { WebClient webClient = new WebClient(); webClient.OpenReadCompleted += (s, e) => { externalObj = myReadWebclientResponse(e.Result); // my custom method to read the response }; webClient.OpenReadAsync(new Uri(“http://externalURl.com/sample.xml”, UriKind.Absolute)); } catch (Exception) { externalObj = null; } […]