标签: webclient-download

wb.DownloadFileAsync throw"WebClient不支持并发I/O操作." 例外

我需要这个代码的帮助

#region Events

public class DownloadProgressChangedEventArg
{
    private long _ProgressPercentage;
    private long _BytesReceived;
    private long _TotalBytesToReceive;

    public DownloadProgressChangedEventArg(long BytesReceived, long TotalBytesToReceive)
    {
        _BytesReceived = BytesReceived;
        _TotalBytesToReceive = TotalBytesToReceive;
        _ProgressPercentage = BytesReceived * 100 / (TotalBytesToReceive);
    }

    public long BytesReceived { get { return _BytesReceived; } set { _BytesReceived = value; } }
    public long ProgressPercentage { get { return _ProgressPercentage; } set { _ProgressPercentage = value; } }
    public long TotalBytesToReceive { get { return _TotalBytesToReceive; } set { _TotalBytesToReceive …
Run Code Online (Sandbox Code Playgroud)

c# webclient-download

3
推荐指数
1
解决办法
1万
查看次数

Webclient.DownloadFile().aspx文件无法打开

我第一次使用PowerShell.我已经学会了如何使用webclient下载文件,使用以下代码.

$client = New-Object System.NET.Webclient
$client.DownloadFile( $url, $path )
Run Code Online (Sandbox Code Playgroud)

这似乎很好地适用于我最终要做的事情,即一次从网页下载多个文件.我在一个站点上尝试了这个,该站点的文件格式为.pfva文件,这些文件以PDF格式打开.没问题.这也是受密码保护的网站.

所以转移到我真正想要使用它的网站.同样,一个需要登录的站点,虽然我只是在我的浏览器上登录然后运行webclient.可能为什么我永远不必在命令脚本中传递身份验证....

此站点的文件格式为.aspx文件.它们将作为PDF文件打开.我只需单击该文件,保存或打开,它就可以自然地作为PDF文件工作.但是当我使用webclient.download文件时,它会下载到正确的位置......但是在尝试打开它时出现错误.

"Adobe无法打开文件.它没有正确编码"......这些都是这样的.我现在无法得到这个消息因为我在工作.下载的URL采用以下格式.....

https://www.WebsiteABC.com/ShowDocument.aspx?DocPath=%7e%5cDocument%5cb75c6093-697a-4e59-bc26-fa2eb24f57f7%5cAUTHORIZATION.PDF
Run Code Online (Sandbox Code Playgroud)

为什么不打开!?!有没有解决的办法.任何帮助,将不胜感激.谢谢!.

哦,顺便说一句,我将$ path设置为我计算机上某个目录中的.PDF文件...因为我读到应该提供一个文件的路径,而不仅仅是一个目录.

powershell downloadfile .aspxauth aspxcombobox webclient-download

3
推荐指数
1
解决办法
5867
查看次数

C#进程无法访问文件,因为它正由另一个进程使用

请帮我解决我面临的问题.

我在C#中为xml文件编写了一个导入器.每次运行导入时,我都需要从URL下载XML文件.

我已经下载了以下代码来下载它:

var xmlPath = @"C:\Desktop\xxx.xml";
public void DownloadFile(string url, string saveAs)
{
    using(var webClient = new WebClient())
    {
        webClient.DownloadFileAsync(new Uri(url), saveAs);
    }
}
Run Code Online (Sandbox Code Playgroud)

_downloader.DownloadFile(Config.FeedUrl, xmlPath);调用该方法.Url位于配置文件(Config.FeedUrl)中.

然后,当我尝试GetProperties(xmlPath);我得到异常"进程无法访问该文件,因为该文件正由另一个进程使用.

我确保目的地存在,但我不知道为什么我会收到此错误.

有人可以帮帮我吗?

谢谢

c# asynchronous webclient-download

2
推荐指数
1
解决办法
4122
查看次数

在我的下载程序中添加暂停和继续功能

我正在用C#创建一个下载程序.我正在使用WebClient类.要在按钮上暂停下载,我可以考虑使用Thread.所以当我创建线程并将其附加到我的文件下载如下

WebClient web = new WebLCient();
Thread dwnd_thread = new Thread(Program.web.DownloadFileAsync(new Uri(Program.src), Program.dest));
Run Code Online (Sandbox Code Playgroud)

它给了我以下错误:"'System.Threading.Thread.Thread(System.Threading.ThreadStart)'的最佳重载方法匹配'有一些无效的参数"和"Argument'1':无法从'void'转换为' System.Threading.ThreadStart'".

然后我想如果我暂停我的系统主线程然后它可以阻止我在下面的代码行使用的整个过程

System.Threading.Thread.Sleep(100);
Run Code Online (Sandbox Code Playgroud)

但它什么都不做.有人可以告诉我什么是更好的暂停/下载方法以及如何使用线程暂停我的下载过程.

c# multithreading download webclient-download

2
推荐指数
1
解决办法
1万
查看次数

可以使用Powershell列出URL目录的内容吗?

我们在FTP服务器上托管了一堆zip文件,这些文件也可以通过HTTP访问.我很想做(gci http://test.com/test/*.zip)之类的东西,并给我网络服务器上存在的所有zip文件.

有没有人知道如何以干净的方式做这样的事情?

TIA

powershell webclient-download

2
推荐指数
1
解决办法
5350
查看次数

WebClient()。DownloadString()返回旧数据

我正在使用此代码从URL获取返回字符串

webClient.Encoding = Encoding.UTF8;
response = webClient.DownloadString("http://somesite.com/code.php");
Console.Write(response);
Run Code Online (Sandbox Code Playgroud)

code.php这个样子的

<?php
$data = file_get_contents('code.txt');
echo $data;
?>
Run Code Online (Sandbox Code Playgroud)

问题是当我更改code.txt文件的内容时,该webClient.DownloadString()方法返回文件的旧内容code.txt。当我http://somesite.com/code.php在浏览器中打开URL时,它可以很好地工作。

任何解决方案将不胜感激!

我的问题似乎重复了,但我不太明白这里所说的是什么:C#WebClient disable cache

如果有人可以解释并提供示例代码,那就太好了!

php c# webclient webclient-download

2
推荐指数
1
解决办法
1326
查看次数

C#从Web服务下载文件

我有一个Web服务,例如此示例,用于从服务器下载zip文件。通过网络浏览器打开URL时,可以正确下载zip文件。问题是当我尝试通过桌面应用程序下载zip文件时。我使用以下代码进行下载:

WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri(@"http://localhost:9000/api/file/GetFile?filename=myPackage.zip"), @"myPackage.zip");
Run Code Online (Sandbox Code Playgroud)

测试之后,我下载了myPackage.zip,但它为空,为0kb。关于此或任何其他服务器代码+客户端代码示例的任何帮助?

c# webclient webclient-download asp.net-web-api

2
推荐指数
1
解决办法
4045
查看次数

C# WebClient - DownloadString 编码错误

我正在尝试从 Amazon 下载 html 文档,但由于某种原因,我得到了一个错误的编码字符串,例如“??K??g??g?e”。

这是我试过的代码:

using (var webClient = new System.Net.WebClient())
{
    var url = "https://www.amazon.com/dp/B07H256MBK/";
    webClient.Encoding = Encoding.UTF8;
    var result = webClient.DownloadString(url);
}
Run Code Online (Sandbox Code Playgroud)

使用 HttpClient 时也会发生同样的事情:

var url = "https://www.amazon.com/dp/B07H256MBK/";
var httpclient = new HttpClient();
var html = await httpclient.GetStringAsync(url);
Run Code Online (Sandbox Code Playgroud)

我还尝试以字节读取结果,然后将其转换回 UTF-8,但我仍然得到相同的结果。另请注意,这并不总是发生。例如,昨天我运行这段代码大约 2 个小时,我得到了一个正确编码的 HTML 文档。但是今天我总是得到一个糟糕的编码结果。它每隔一天发生一次,所以它不是一次性的。

================================================== ================

但是,当我使用 HtmlAgilitypack 的包装器时,它每次都按预期工作:

var url = "https://www.amazon.com/dp/B07H256MBK/";
HtmlWeb htmlWeb = new HtmlWeb();
HtmlDocument doc = htmlWeb.Load(url);
Run Code Online (Sandbox Code Playgroud)

即使我明确定义了正确的编码,是什么导致 WebClient 和 HttpClient 得到错误的编码字符串?默认情况下,HtmlAgilityPack 的包装器是如何工作的?

谢谢你的帮助!

.net c# asp.net webrequest webclient-download

2
推荐指数
1
解决办法
580
查看次数

如何在C#中成功下载.exe文件?

我正在尝试test.exe使用以下代码下载文件:

public void DownloadFile()
{
    using(var client = new WebClient())
    {
         client.DownloadFileAsync(new Uri("http://www.acromix.net16.net/download/test.exe"), "test.exe");
    }
}
Run Code Online (Sandbox Code Playgroud)

使用这个简单的代码,我调试它,这是输出: 产量

我不知道,也不知道为什么它是0 KB(应该是328 KB).[ / downloads ]

我怎样才能使它工作?

编辑: 托管网站(000webhost)阻止.exe文件下载...

c# download downloadfileasync async-await webclient-download

1
推荐指数
1
解决办法
844
查看次数

WP7上的WebClient - 抛出"使用此方法的请求不能有请求正文"

如果我在Consoleapp中执行此代码,它可以正常工作:

        string uriString = "http://url.com/api/v1.0/d/" + Username + "/some?amount=3&offset=0";

        WebClient wc = new WebClient();

        wc.Headers["Content-Type"] = "application/json";
        wc.Headers["Authorization"] = AuthString.Replace("\\", "");

        string responseArrayKvitteringer = wc.DownloadString(uriString);
        Console.WriteLine(responseArrayKvitteringer);
Run Code Online (Sandbox Code Playgroud)

但是,如果我将代码移动到我的WP7项目,如下所示:

        string uriString = "http://url.com/api/v1.0/d/" + Username + "/some?amount=3&offset=0";

            WebClient wc = new WebClient();

            wc.Headers["Content-Type"] = "application/json";
            wc.Headers["Authorization"] = AuthString.Replace("\\", "");

            wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
            wc.DownloadStringAsync(new Uri(uriString));

    void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        MessageBox.Show(e.Result);
    }
Run Code Online (Sandbox Code Playgroud)

我得到了例外:使用此方法的请求不能有请求正文.

为什么?

解决方案是删除Content-type:

   string uriString = "http://url.com/api/v1.0/d/" + Username + "/some?amount=3&offset=0";

            WebClient wc = new WebClient();

            //wc.Headers["Content-Type"] = "application/json"; …
Run Code Online (Sandbox Code Playgroud)

webclient windows-phone-7 webclient-download

0
推荐指数
1
解决办法
3744
查看次数

使用DownloadDataAsync下载AWAIT多个文件

我有一个zip文件创建者,它接收一个String[]Urls,并返回一个包含所有文件的zip文件String[]

我想会有很多这方面的例子,但我似乎找不到"如何异步下载多个文件并在完成后返回"的答案

如何一次下载{n}个文件,仅在所有下载完成后返回词典?

private static Dictionary<string, byte[]> ReturnedFileData(IEnumerable<string> urlList)
{
    var returnList = new Dictionary<string, byte[]>();
    using (var client = new WebClient())
    {
        foreach (var url in urlList)
        {
            client.DownloadDataCompleted += (sender1, e1) => returnList.Add(GetFileNameFromUrlString(url), e1.Result);
            client.DownloadDataAsync(new Uri(url));
        }
    }
    return returnList;
}

private static string GetFileNameFromUrlString(string url)
{
    var uri = new Uri(url);
    return System.IO.Path.GetFileName(uri.LocalPath);
}
Run Code Online (Sandbox Code Playgroud)

.net c# webclient async-await webclient-download

-3
推荐指数
1
解决办法
1845
查看次数