C# 使用HttpWebRequest时如何限制bandwith使用?
使用HttpWebRequest时如何限制bandwith使用?例如,您可以将连接到带宽限制HTTP代理。也许这不是一个方便的解决方案,但它肯定会起作用。如果您是在代码中完成的,我建议使用Rx之类的工具来帮助简化计时器的使用等C# 使用HttpWebRequest时如何限制bandwith使用?,c#,.net,httpwebrequest,bandwidth-throttling,C#,.net,Httpwebrequest,Bandwidth Throttling,使用HttpWebRequest时如何限制bandwith使用?例如,您可以将连接到带宽限制HTTP代理。也许这不是一个方便的解决方案,但它肯定会起作用。如果您是在代码中完成的,我建议使用Rx之类的工具来帮助简化计时器的使用等 class Uploader { /// <summary>Thread-safe flag to ensure that a packet isn't currently sending</summary> private vol
class Uploader
{
/// <summary>Thread-safe flag to ensure that a packet isn't currently sending</summary>
private volatile bool isPacketSending = false;
/// <summary>
/// HTTP Posts a stream to a web address with a maximum bytes per second until the file is uploaded
/// </summary>
/// <param name="address">The web address to post the file to</param>
/// <param name="requestBody">The request body to stream at a maximum speed</param>
/// <param name="bytesPerSecond">The maximum number of bytes to send every second</param>
/// <returns>Returns an observable sequence of the bytes read so far</returns>
public IObservable<long> PostStreamThrottledAsync(Uri address, Stream requestBody, int bytesPerSecond)
{
if (!requestBody.CanRead)
{
throw new InvalidOperationException("Request body stream cannot be read from");
}
return Observable.Using(
() =>
{
var client = new WebClient();
return client.OpenWrite(address);
},
outputStream => Observable.Return(0L).Concat(Observable.Interval(TimeSpan.FromSeconds(1)))
.TakeWhile(tick => SendPacket(requestBody, outputStream, bytesPerSecond) != 0)
.Select(_ => requestBody.Position));
}
/// <summary>
/// Sends a packet up to the maximum bytes specified
/// </summary>
/// <param name="requestBody">The stream to read from</param>
/// <param name="output">The stream to write to</param>
/// <param name="bytesPerSecond">The number of bytes to send</param>
/// <returns>Returns the number of bytes actually sent; zero if at end of stream; -1 if we are exceeding throughput capacity.</returns>
private int SendPacket(Stream requestBody, Stream output, int bytesPerSecond)
{
if (isPacketSending)
{
return -1;
}
try
{
isPacketSending = true;
var buffer = new byte[bytesPerSecond];
var bytesRead = requestBody.Read(buffer, 0, bytesPerSecond);
if (bytesRead != 0)
{
output.Write(buffer, 0, bytesRead);
}
return bytesRead;
}
finally
{
isPacketSending = false;
}
}
}
类上传器
{
///线程安全标志,以确保数据包当前未发送
私有易失性bool isPacketSending=false;
///
///HTTP以每秒最大字节数将流发布到web地址,直到上载文件为止
///
///要将文件发布到的web地址
///请求主体以最大速度传输
///每秒要发送的最大字节数
///返回到目前为止读取的字节的可观察序列
公共IObservable PostStreamThrottledAsync(Uri地址、流请求体、int bytesPerSecond)
{
如果(!requestBody.CanRead)
{
抛出新的InvalidOperationException(“无法读取请求正文流”);
}
返回可观察。使用(
() =>
{
var client=new WebClient();
返回client.OpenWrite(地址);
},
outputStream=>Observable.Return(0L).Concat(Observable.Interval(TimeSpan.FromSeconds(1)))
.TakeWhile(勾选=>SendPacket(请求正文、输出流、字节秒)!=0)
.Select(=>requestBody.Position));
}
///
///发送最大指定字节数的数据包
///
///要读取的流
///要写入的流
///要发送的字节数
///返回实际发送的字节数;如果在流末尾,则返回零;如果超出吞吐量,则返回-1。
私有int SendPacket(流请求主体、流输出、int字节秒)
{
如果(isPacketSending)
{
返回-1;
}
尝试
{
isPacketSending=true;
var buffer=新字节[bytesPerSecond];
var bytesRead=requestBody.Read(缓冲区,0,字节秒);
如果(字节读取!=0)
{
输出写入(缓冲区,0,字节读取);
}
返回字节读取;
}
最后
{
isPacketSending=false;
}
}
}
我喜欢这个。有点让我觉得自己的节流阀有点傻:)问题在于如何用代码解决它,而不是如何使用其他外部工具来实现它。