Tag: webclient

HTTP请求标头中缺少基本HTTP身份validation条目

我有以下代码: WebClient client = new WebClient(); String un = “Username”; String pw = “Password”; client.Credentials = new System.Net.NetworkCredential(un,pw); client.DownloadFileCompleted += new AsyncCompletedEventHandler(downloadFileCompleted); client.DownloadFileAsync(new Uri(url), Config.LocalDir + @”\data\supportData.xml”); 在服务器上使用NetworkMonitor我收到以下内容: Http: Request, GET /audiClave/REST/en/actions Command: GET URI: /audiClave/REST/en/actions ProtocolVersion: HTTP/1.1 Host: 210.xxx.xxx.xxx:8080 Connection: Keep-Alive HeaderEnd: CRLF 没有validation条目。 我错过了什么?

WebClient.DownloadString(url)当这个url返回404页面时,我怎么能跳过这个?

我正在使用WebClient.DownloadString(url)来下载一个网页,当一个url停止并且不再有效的404网页时。 当我遇到这个错误时,我想跳过这些页面。 如果url是404页面,则不会开始下载。 所以我无法解析未下载的数据……

C#异步方法仍然挂起UI

我有这两种方法,我想运行异步以保持UI响应。 但是,它仍然悬挂着UI。 有什么建议? async void DoScrape() { var feed = new Feed(); var results = await feed.GetList(); foreach (var itemObject in results) { var item = new ListViewItem(itemObject.Title); item.SubItems.Add(itemObject.Link); item.SubItems.Add(itemObject.Description); LstResults.Items.Add(item); } } public class Feed { async public Task<List> GetList() { var client = new WebClient(); string content = await client.DownloadStringTaskAsync(new Uri(“anyUrl”)); var lstItemObjects […]

Windows 8中不存在WebClient类

我想使用HTTP Web服务,我已经为wp7开发了一个应用程序。 我使用WebClient类,但我不能将它用于Windows 8(“错误:无法找到类型或命名空间”)。 我还能用什么? 你能给我一个代码示例吗? 当命名空间不存在时,Microsoft是否有一个站点可以提供帮助?

在post请求中发送文件+参数

我正在使用此代码将参数发送到网页并从中获取正确的响应。 System.Net.WebClient oWeb = new System.Net.WebClient(); oWeb.Proxy = System.Net.WebRequest.DefaultWebProxy; oWeb.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials; oWeb.Headers.Add(“Content-Type”, “application/x-www-form-urlencoded”); byte[] bytArguments = System.Text.Encoding.ASCII.GetBytes(“value1=123&value2=xyz”); byte[] bytRetData = oWeb.UploadData(“http://website.com/file.php”, “POST”, bytArguments); response = System.Text.Encoding.ASCII.GetString(bytRetData); 但是现在我想发送一个类似( .doc )的文件到它+上面的参数( value1 , value2 ),但我不知道该怎么做。

WebClient.DownloadFileAsync – 一次下载一个文件

我使用下面的代码从TFS服务器下载多个附件: foreach (Attachment a in wi.Attachments) { WebClient wc = new WebClient(); wc.Credentials = (ICredentials)netCred; wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted); wc.DownloadFileAsync(a.Uri, “C:\\” + a.Name); } 我想使用DownloadFileAsync下载多个文件,但我希望它们逐个下载。 有人可能会问“你为什么不使用同步的DownloadFile方法?” 这是因为: 我想利用DownloadFileAsync提供的事件。 我不想创建Webclient的多个实例以避免泛滥服务器。 这是我想到的解决方案: foreach (Attachment a in wi.Attachments) { WebClient wc = new WebClient(); wc.Credentials = (ICredentials)netCred; wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted); wc.DownloadFileAsync(a.Uri, “C:\\” + a.Name); while (wc.IsBusy) { […]

如何以编程方式下载C#中的大文件

我需要以编程方式下载大文件,然后再进行处理。 最好的方法是什么? 由于文件很大,我想要特定的时间等待,以便我可以强行退出。 我知道WebClient.DownloadFile()。 但似乎没有办法特定等待一段时间才能强行退出。 try { WebClient client = new WebClient(); Uri uri = new Uri(inputFileUrl); client.DownloadFile(uri, outputFile); } catch (Exception ex) { throw; } 另一种方法是使用命令行实用程序(wget)下载文件并使用ProcessStartInfo触发命令并使用Process’WellForExit(int ms)强制退出。 ProcessStartInfo startInfo = new ProcessStartInfo(); //set startInfo object try { using (Process exeProcess = Process.Start(startInfo)) { //wait for time specified exeProcess.WaitForExit(1000 * 60 * 60);//wait till 1m […]

在C#中从FTP读取文件到内存

我想从FTP服务器读取文件而不将其下载到本地文件。 我写了一个函数,但它不起作用: private string GetServerVersion() { WebClient request = new WebClient(); string url = FtpPath + FileName; string version = “”; request.Credentials = new NetworkCredential(ftp_user, ftp_pas); try { byte[] newFileData = request.DownloadData(new Uri(FtpPath)+FileName); string fileString = System.Text.Encoding.UTF8.GetString(newFileData); } catch (WebException e) { } return version; }

使用Webclient.Uploadfile在文件上载期间获取上载进度

我有一个应用程序,使用webclient将文件上传到服务器。 我想在文件上传过程中显示进度条。 我将如何实现这一目标?

WebClient运行javascript

我有一个。 aspx页面,它有一些控制分页的JavaScript函数。 我可以通过webbrowser在WebBrowser1_DocumentCompleted使用以下方法运行此javascript函数 WebBrowser1.Document.Window.DomWindow.execscript (“somefunction();”, “JavaScript”) webbrowser非常慢,我更喜欢使用System.Net.WebClient.DownloadString 。 有一些方法可以使用更快或其他方式的System.Net.WebClient方法运行此脚本吗?