c#-在Mac OS X上为Mono-并行HTTP下载限制为2
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-在Mac OS X上为Mono-并行HTTP下载限制为2,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3382字,纯文字阅读大概需要5分钟。
内容图文
![c#-在Mac OS X上为Mono-并行HTTP下载限制为2](/upload/InfoBanner/zyjiaocheng/655/e59842f29db84789998bcd9e4618b6c4.jpg)
我正在使用Mono开发一个程序(适用于Mac OS X和Debian),该程序可以同时下载多个文件.
但是,尽管我使用了构造函数new RollingDownload(10),但我只能同时下载2个文件.我正在使用的代码是这个
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading;
using System.Net;
using System.Diagnostics;
using System.IO;
namespace worker
{
public class RollingDownload
{
private static ConcurrentQueue<Download> _downloads = new ConcurrentQueue<Download>();
private static short _NumberOfThreads;
private static short DefaultTimeoutSeconds = 20;
public RollingDownload (short NumberOfThreads)
{
_NumberOfThreads = NumberOfThreads;
}
public void addDownload(Download download) {
_downloads.Enqueue(download);
}
public void SpawnWebRequests()
{
ServicePointManager.DefaultConnectionLimit = _NumberOfThreads;
ServicePointManager.MaxServicePoints = _NumberOfThreads;
IList<Thread> threadList = new List<Thread>();
for (int i = 0; i < _NumberOfThreads; i++)
{
var thread = new Thread(ProcessWebRequests);
threadList.Add(thread);
thread.Start();
}
for (int i = 0; i < _NumberOfThreads; i++)
{
threadList[i].Join();
}
}
private static void ProcessWebRequests()
{
while (true)
{
Download download;
Console.WriteLine (Thread.CurrentThread.ManagedThreadId);
if(_downloads.TryDequeue(out download)) {
ProcessWebRequest(download);
} else {
break;
}
}
}
private static void ProcessWebRequest(Download download)
{
try
{
var request = (HttpWebRequest)WebRequest.Create(download.Url);
request.Method = "GET"; // or "GET", since some sites (Amazon) don't allow HEAD
request.Timeout = DefaultTimeoutSeconds * 1000;
request.AllowAutoRedirect = true;
//request.ServicePoint.ConnectionLimit = _NumberOfThreads;
request.KeepAlive = false;
//request.ServicePoint = ServicePointManager.FindServicePoint(new Uri(download.Url));
// Get the response (throws an exception if status != 200)
using (var response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode == HttpStatusCode.OK) {
/*string contents = "";
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
contents = reader.ReadToEnd();
}*/
download.onCompleted(response.GetResponseStream(), response.StatusCode);
}
}
}
catch (WebException ex)
{
var response = ((HttpWebResponse)ex.Response);
var status = response != null
? response.StatusCode
: HttpStatusCode.RequestTimeout;
Console.WriteLine(String.Format("Broken link ({0}): {1}", status, download.Url), ex);
// Don't rethrow, as this is an expected exception in many cases
}
catch (Exception ex)
{
Console.WriteLine(String.Format("Error processing link {0}", download.Url), ex);
// Rethrow, something went wrong
throw;
}
}
}
public class Download
{
public string Url { get; set; }
public string PathToSave { get; set; }
public Download (String Url)
{
this.Url = Url;
}
public void onCompleted (Stream response, HttpStatusCode httpcode)
{
Console.WriteLine ("hello everybody: " + httpcode.ToString ());
}
}
}
好吧,我不知道. #mono IRC频道中的某人表示,我应该使用this ticket解决问题,但我不知道在哪里可以找到machine.config或如何在monodevelop中添加它.
我正在开发的应用程序是使用C#的控制台应用程序(无ASP!).
听到你们的消息真是太好了.
解决方法:
您的下载是否来自同一主机?如果是这样,则需要向RollingDownload的构造函数中添加一些代码(或其他初始化代码):
string downloadHost = ...;
ServicePoint sp = ServicePointManager.FindServicePoint(new Uri(downloadHost));
sp.ConnectionLimit = _NumberOfThreads;
[信用this blog可以帮助我解决早期的类似问题.]
内容总结
以上是互联网集市为您收集整理的c#-在Mac OS X上为Mono-并行HTTP下载限制为2全部内容,希望文章能够帮你解决c#-在Mac OS X上为Mono-并行HTTP下载限制为2所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。