我想在c#中使用webclient下载文件.
文件位置的远程端口是20114.如何告诉webclient使用20114端口?
如何使用F#对异步操作类的内置支持,暴露基于事件的异步模式,如WebClient类?
let Download(url : Uri) =
let client = new WebClient()
let html = client.DownloadString(url)
html
Run Code Online (Sandbox Code Playgroud)
当我尝试将其更改为使用"let!"时 在异步块中(如Soma最近的帖子中所述)
let Download(url : Uri) =
async {
let client = new WebClient()
let! html = client.DownloadStringAsync(url)
return html }
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息:
类型约束不匹配.类型单位与Async <'a>类型不兼容类型'unit'与'Async <'a>类型不兼容
编辑:我真的在询问使用*Async()方法的一般问题,WebClient只是一个简单的例子.微软说 "......你应该尽可能使用基于事件的异步模式[而不是BeginFoo()/ EndFoo()]来暴露异步功能......"所以我认为应该有一种简单的方法来消耗任意*来自F#的Async()方法.
我正在使用System.Net.WebClient从URL中检索一些数据.
void GetAirportData()
{
var url = "http://server.example.com/airports.xml?id=OSL";
var webClient = new WebClient();
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(new Uri(url, UriKind.Absolute));
}
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
//How can I see the original URL here, so that I can see which id parameter was passed?
}
Run Code Online (Sandbox Code Playgroud)
我发送的请求将包含一个URL,其中包含一些参数,这些参数会针对每个调用进行更改,我需要知道响应响应时我要求的参数.
我可以使用例如sender对象的.UserState属性吗?
我有以下代码将异步下载文件到我的硬盘驱动器,向控制台喊出当前的进度并最后退出一个再见消息:
webClient.DownloadProgressChanged.Add(fun args ->
if (currentPercentage < args.ProgressPercentage) then
Console.WriteLine(args.ProgressPercentage.ToString() + "%")
currentPercentage <- args.ProgressPercentage
)
webClient.DownloadFileCompleted.Add(fun args ->
Console.WriteLine("Download finished!")
Environment.Exit 0
)
webClient.DownloadFileAsync(new Uri(url_to_download), file_name)
Thread.Sleep Int32.MaxValue
Run Code Online (Sandbox Code Playgroud)
然而,我想知道是否有更优雅的方式来实现这一点,而不必诉诸于主线程中的"永远沉睡",让程序尽头结束Environment.Exit().我对使用没有任何偏见,Environment.Exit()但如果可能的话,我想避免使用它!我能想到避免这种情况的唯一方法是生成一个新线程,然后等待它死掉,但这看起来确实很麻烦.有没有更简单的方法来完成这个?
如何在Alfresco Share中的表单上添加文件/数据上传选择器?我需要点击我的按钮,然后应该出现文件选择器.有人可以帮忙吗?
我正在开发一个代码,每隔30秒轮询一次Bitstamp交换代码.这是我的代码:
public IObservable<string> Stream(Uri way, WebClient wc)
{
Func<IObserver<string>, Task> Fun = async Observer =>
{
var res = await wc.DownloadStringTaskAsync(way);
Observer.OnNext(value: res);
};
return Observable.Create<string>(Fun);
}
public IObservable<string> GetDelay(int secs)
{
var exe = TimeSpan.FromSeconds(secs);
return Observable.Empty<string>("x").Delay(exe);
}
Stream(new Uri("https://bitstamp.net/api/ticker"), new WebClient { }).Concat(GetDelay(30))
.Repeat(5).Subscribe(res => Debug.WriteLine("got result: {0}", res));
Run Code Online (Sandbox Code Playgroud)
问题是WebClient(并且HttpClient)也会在第一次调用后返回缓存的结果,可以通过相同的时间戳看到:
got result: {"high": "690.00", "last": "645.10", "timestamp": "1387715532" ... }
got result: {"high": "690.00", "last": "645.10", "timestamp": "1387715532" ... }
...
即使在关闭网络后,它们也会正常返回结果,所以很明显它们会将其缓存到某处.添加类似"?cache = random"的内容不起作用,因为Bitstamp上的ticker不允许使用请求参数.设置 …
caching webclient httprequest system.reactive windows-phone-8
我对Powershell比较陌生,所以现在真的不知道该怎么回事.我正在尝试从subversion存储库下载文件并获得(401)Unauthorized"错误.我能够使用IE在同一台计算机上使用完全相同的凭据登录该站点并下载该文件.
$source = "http://repository/folder/File.exe"
$destination = "E:\Temp\File.exe"
$wc = New-Object System.Net.WebClient
$user="user"
$pwd=convertto-securestring -string "password" -AsPlainText -force
$creds=New-Object System.Management.Automation.PSCredential -ArgumentList $user, $pwd
$wc.Credentials = New-Object System.Net.NetworkCredential ($user, $Creds.GetNetworkCredential().Password,"DOMAIN")
$download=$wc.DownloadFile($source, "$destination")
Run Code Online (Sandbox Code Playgroud)
使用"2"参数调用"DownloadFile"的异常:"远程服务器返回错误:(401)未经授权."
如果这是跨平台问题的任何想法?以及如何解决这个问题?
谢谢
我正在尝试访问具有Windows安全性的网站:
所以我引用了这篇文章并编写了以下代码:
$web = New-Object Net.WebClient
$web.Credentials = new-object System.Net.NetworkCredential($username, $password, $domain)
$content = $web.DownloadString("https://my.url")
Run Code Online (Sandbox Code Playgroud)
但是在添加凭据之前,我仍然遇到相同的错误:"The remote server returned an error: (401) Unauthorized."我随后引用了此文章,但是在这种情况下,我认为基本身份验证不适用。我已经多次验证了用户名,密码和域。是否存在其他常用的通过身份验证发出Web请求的解决方案?
我创建了一个websocket(使用perl Net :: WebSocket :: Server,但我认为这没关系)。我的问题是,当我关闭网站选项卡(不一定是整个浏览器)时,服务器将断开特定的套接字(称为断开事件)。服务器如何设法知道这一点?我找不到直接的详细说明。
这很好用:
private WebClient _webClient;
private void ButtonStart_Click(object sender, RoutedEventArgs e) {
using (_webClient = new WebClient()) {
_webClient.DownloadFileTaskAsync("https://speed.hetzner.de/100MB.bin", @"D:\100MB.bin");
}
}
private void ButtonStop_Click(object sender, RoutedEventArgs e) {
_webClient.CancelAsync();
}
Run Code Online (Sandbox Code Playgroud)
虽然这段代码(注意异步/等待模式)...:
private WebClient _webClient;
private async void ButtonStart_Click(object sender, RoutedEventArgs e) {
using (_webClient = new WebClient()) {
await _webClient.DownloadFileTaskAsync("https://speed.hetzner.de/100MB.bin", @"D:\100MB.bin");
}
}
private void ButtonStop_Click(object sender, RoutedEventArgs e) {
_webClient.CancelAsync();
}
Run Code Online (Sandbox Code Playgroud)
...引发以下异常:
System.Net.WebException
该请求被中止:该请求被取消。
at System.Net.ConnectStream.EndRead(IAsyncResult asyncResult)
at System.Net.WebClient.DownloadBitsReadCallbackState(DownloadBitsState state, IAsyncResult result)
--- End of stack trace from previous …Run Code Online (Sandbox Code Playgroud) webclient ×10
c# ×3
asynchronous ×2
f# ×2
powershell ×2
.net ×1
alfresco ×1
async-await ×1
caching ×1
components ×1
customizing ×1
httprequest ×1
perl ×1
port ×1
websocket ×1