- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有以下函数来获取页面。我的问题是我想计算花费了多少互联网连接
入站(下载)和出站流量(发送)
我该怎么做?谢谢
我的函数
public static string func_fetch_Page(string srUrl, int irTimeOut = 60,
string srRequestUserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0",
string srProxy = null)
{
string srBody = "";
string srResult = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(srUrl);
request.Timeout = irTimeOut * 1000;
request.UserAgent = srRequestUserAgent;
request.KeepAlive = true;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
WebHeaderCollection myWebHeaderCollection = request.Headers;
myWebHeaderCollection.Add("Accept-Language", "en-gb,en;q=0.5");
myWebHeaderCollection.Add("Accept-Encoding", "gzip, deflate");
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
using (WebResponse response = request.GetResponse())
{
using (Stream strumien = response.GetResponseStream())
{
using (StreamReader sr = new StreamReader(strumien))
{
srBody = sr.ReadToEnd();
srResult = "success";
}
}
}
}
catch ()
{
}
return srBody;
}
C# .net 4.5 WPF 应用程序
@Simon Mourier 我如何计算花费的流量
public static long long_GlobalDownload_KByte = 0;
public static long long_GlobalSent_KByte = 0;
Time _timer_fetch_download_upload = new Timer(getDownload_Upload_Values, null, 0, 100 * 1000);
public static void getDownload_Upload_Values(Object state)
{
using (Process p = Process.GetCurrentProcess())
{
foreach (var cnx in TcpConnection.GetAll().Where(c => c.ProcessId == p.Id))
{
Interlocked.Add(ref long_GlobalDownload_KByte, Convert.ToInt64(cnx.DataBytesIn));
Interlocked.Add(ref long_GlobalSent_KByte, Convert.ToInt64(cnx.DataBytesOut));
}
}
}
最佳答案
一个 Windows API 可以为您提供以下信息:GetPerTcpConnectionEStats用于 IPV4 连接和关联的 IPV6 GetPerTcp6ConnectionEStats 函数。注意你需要使用 SetPerTcpConnectionEStats在能够获得任何统计数据之前,通常需要管理员权限...
要获取所有连接的列表,您可以使用 GetExtendedTcpTable功能。它还可以为您提供连接的进程 ID,这非常有用。
这些是 native API,使用起来并不那么容易,但我创建了一个 TcpConnection 类来包装所有这些。它在一个名为 IPStats 的小型 WPF 应用程序中可用:https://github.com/smourier/IPStats
因此,这里的难点在于将您的 .NET HttpWebRequest 链接到连接列表中的 TcpConnection。在连接存在之前您无法获取任何统计信息,但是一旦您创建了连接,您就可以使用如下代码获取相应的统计信息:
static IEnumerable<TcpConnection> GetProcessConnection(IPEndPoint ep)
{
var p = Process.GetCurrentProcess();
return TcpConnection.GetAll().Where(c => ep.Equals(c.RemoteEndPoint) && c.ProcessId == p.Id);
}
HttpWebRequest req = ...
// this is how you can get the enpoint, or you can also built it by yourself manually
IPEndPoint remoteEndPoint;
req.ServicePoint.BindIPEndPointDelegate += (sp, rp, rc) =>
{
remoteEndPoint = rp;
return null;
};
// TODO: here, you need to connect, so the connection exists
var cnx = GetProcessConnection(remoteEndPoint).FirstOrDefault();
// access denied here means you don't have sufficient rights
cnx.DataStatsEnabled = true;
// TODO: here, you need to do another request, so the values are incremented
// now, you should get non-zero values here
// note TcpConnection also has int/out bandwidth usage, and in/out packet usage.
Console.WriteLine("DataBytesIn:" + cnx.DataBytesIn);
Console.WriteLine("DataBytesOut:" + cnx.DataBytesOut);
// if you need all connections in the current process, just do this
ulong totalBytesIn = 0;
ulong totalBytesOut = 0;
Process p = Process.GetCurrentProcess();
foreach (var cnx in TcpConnection.GetAll().Where(c => c.ProcessId == p.Id))
{
totalBytesIn += cnx.DataBytesIn;
totalBytesOut += cnx.DataBytesOut;
}
有3个缺点:
更新:如果您想持续监控给定进程的所有连接,我编写了一个 ProcessTcpConnections 实用程序类,它会记住所有连接并汇总它们的使用情况。它将像这样使用(在控制台应用程序示例中):
class Program
{
static void Main(string[] args)
{
ProcessTcpConnections p = new ProcessTcpConnections(Process.GetCurrentProcess().Id);
Timer timer = new Timer(UpdateStats, p, 0, 100);
do
{
// let's activate the network so we measure something...
using (WebClient client = new WebClient())
{
client.DownloadString("http://www.example.com");
}
Console.ReadKey(true); // press any key to download again
}
while (true);
}
private static void UpdateStats(object state)
{
ProcessTcpConnections p = (ProcessTcpConnections)state;
p.Update();
Console.WriteLine("DataBytesIn:" + p.DataBytesIn + " DataBytesOut:" + p.DataBytesOut);
}
}
public class ProcessTcpConnections : TcpConnectionGroup
{
public ProcessTcpConnections(int processId)
: base(c => c.ProcessId == processId)
{
ProcessId = processId;
}
public int ProcessId { get; private set; }
}
public class TcpConnectionGroup
{
private List<TcpConnectionStats> _states = new List<TcpConnectionStats>();
private Func<TcpConnection, bool> _groupFunc;
public TcpConnectionGroup(Func<TcpConnection, bool> groupFunc)
{
if (groupFunc == null)
throw new ArgumentNullException("groupFunc");
_groupFunc = groupFunc;
}
public void Update()
{
foreach (var conn in TcpConnection.GetAll().Where(_groupFunc))
{
if (!conn.DataStatsEnabled)
{
conn.DataStatsEnabled = true;
}
TcpConnectionStats existing = _states.Find(s => s.Equals(conn));
if (existing == null)
{
existing = new TcpConnectionStats();
_states.Add(existing);
}
existing.DataBytesIn = conn.DataBytesIn;
existing.DataBytesOut = conn.DataBytesOut;
existing.LocalEndPoint = conn.LocalEndPoint;
existing.RemoteEndPoint = conn.RemoteEndPoint;
existing.State = conn.State;
existing.LastUpdateTime = DateTime.Now;
}
}
public ulong DataBytesIn
{
get
{
ulong count = 0; foreach (var state in _states) count += state.DataBytesIn; return count;
}
}
public ulong DataBytesOut
{
get
{
ulong count = 0; foreach (var state in _states) count += state.DataBytesOut; return count;
}
}
private class TcpConnectionStats
{
public ulong DataBytesIn { get; set; }
public ulong DataBytesOut { get; set; }
public IPEndPoint LocalEndPoint { get; set; }
public IPEndPoint RemoteEndPoint { get; set; }
public TcpState State { get; set; }
public DateTime LastUpdateTime { get; set; }
public bool Equals(TcpConnection connection)
{
return LocalEndPoint.Equals(connection.LocalEndPoint) && RemoteEndPoint.Equals(connection.RemoteEndPoint);
}
}
}
关于c# - 如何计算 HttpWebRequest 花费的出站和入站互联网流量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25296153/
有没有其他方法可以直接使用 terraform 启用这些规则,而无需在 GCP 中创建单独的防火墙规则,然后将标签附加到计算引擎 目前我正在这样做 resource "google_compu
全民国家安全日活动 免费抽随机话费、流量 微信打开链接进入活动 下拉页面点击【我要抽奖】进去完成两个任务获得两次抽奖机会 可抽2-10元话费以及流量 抽奖非必中 仅限移动用户参与 活动期间内每日
随着人们的紧凑生活,从事互联网行业的人大多都把一天的时间安排的满满的,这用户忙碌的时候,根本无心去关注你的推广,只有抓住了用户零零碎碎的时间对其进行推广,同时他也能打发无聊的时间,这样的效果就非常轻
我建立了一个库来做 IGMP 的东西。现在,愚蠢的是,我的图书馆还进行了存在监控以确保其他人仍然是该组的一部分。 IGMP 在较低级别做完全相同的事情。分开消息,轮询路由器它仍然是同一组的一部分,整个
我经营一个具有广告集成功能的媒体网站。最近,我们发现很多人滥用这一点,通过在上传内容上进行流量交换来获取虚假收入。最流行的可能是使用 HitLeap Viewer 应用程序的 HitLeap。 我很好
上下文: 所以我在 AKS 集群中有一个测试脚本;此脚本使用 Azure Active Directory 中的用户的单点登录功能登录到站点。 测试脚本位于集群 A 中,站点位于集群 B 中。还为集群
是否可以指示 Fiddler 仅显示定向到特定主机名的流量?也就是说Fiddler流量可以针对Host进行过滤吗? 最佳答案 请参阅此屏幕截图。位于屏幕的右上方 关于fiddler - 过滤 Fidd
这个问题在这里已经有了答案: Android 8: Cleartext HTTP traffic not permitted (36 个回答) 2年前关闭。 我正在从事一个项目,但我被困在登录/注册页
我有一个 Android VOIP 应用程序。由于某些网络会阻止 VOIP 流量,因此我想找到一些绕过阻止的方法。我认为 VPN 可以做到这一点,但没有任何 VPN 解决方案可以轻松实现。使用 And
我构建了使用 SignalR 的服务器和客户端代码。该网站运行良好,但我在任何浏览器(chrome、IE、Firefox)中都看不到网络流量。我知道网络流量在那里,因为该网站正在运行。 有没有办法在浏
我实现了一个自制的嗅探器(基于 winpcap),并尝试在浏览 HTTPS 网站(gmail 和 facebook)时使用它来嗅探 TCP 连接上的端口 443,但我的代码无法检测到任何流量。 我研究
是否可以使用 Android 手机收集同一小区内手机的 IMEI 或唯一手机 ID?可能已经有一些 hack 可以使用 osmocom ...我正在寻找的是一个易于工作的解决方案来扫描交通(通过计算汽
此 Android 代码是否是在通话期间测试 http 网络可用性的正确方法,或者它是否排除了应包含的网络,反之亦然: public boolean isOnline() { Telephon
我需要能够加密从 Web 服务器到数据库服务器的 MySQL 流量。我知道如何根据 my.cnf 中的服务器和客户端设置将 MySQL 设置为使用 SSL,但是,这需要使用 PHP 中的 mysql_
我想查看所有传出 USB 的流量,并可能根据内容策略阻止进出 USB 的数据交易。这将如何完成?有什么方法可以在 C# 中实现此目的,还是它更像是 C++ 类型的问题? 最佳答案 您可以使用类似 Cr
我需要捕获进程进行的 TCP 通信。但是,我不能只运行该进程,查找其 PID,然后捕获。我需要获取启动后立即发生的通信。 它显然是通过未知端口(不是 80)向另一个进程发出 JSON 请求,该进程注册
如果我有一个名为 www.testsite.com 的页面,并且我使用 url 中的查询字符串链接到该页面,是否可以以某种方式将相同的查询字符串附加到所有传出链接/流量? 例如,假设我像这样链接到该页
我在我的机器上运行本地 HTTP 代理服务器并执行一些日志记录。我也想记录 SSL 流量。为此,我运行了另一个用 Python 编写的代理服务器,它充当 SSL 服务器,带有我的自签名证书,HTTP
我正在编写一个应该检测 VPN 流量的程序。据我了解这个主题,似乎隧道协议(protocol)的检测就像防火墙规则一样容易,使用它们的专用端口: PPTP: 端口 1723/TCP OpenVPN:端
所以,情况是:我想知道程序将请求发送到什么路径。使用 Wireshark,我只能知道它发送的是 https 请求和相应的域,但不知道路径。 我认为即使不破解程序也可以至少检查出站 https 流量。
我是一名优秀的程序员,十分优秀!