c#中socket截取的http包里怎样可以取到url地址呢

jinmingyuan jinmingyuan 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区



socket.BeginReceive(buf, 0, len_receive_buf, SocketFlags.None, new AsyncCallback(CallReceive), this);
/// <summary>
/// 根据数据包内容设置各字段
/// </summary>
/// <param name="buf"></param>
public void SetArgs(byte[] buf)
{
if( buf == null )
{
return;
}

string tempReceiveString = BitConverter.ToString( buf );

m_PackageLength = Convert.ToInt32( tempReceiveString.Substring(6, 2), 16 ) * 16 * 16
+ Convert.ToInt32( tempReceiveString.Substring(9, 2), 16 );
string receiveString = tempReceiveString.Substring(0, m_PackageLength * 3 - 1);


m_IPHeaderLength = Convert.ToInt32( receiveString.Substring(1, 1), 16 ) * 4;
m_IPHeaderContent = receiveString.Substring( 0, m_IPHeaderLength * 3 - 1 );

m_IPVersion = m_IPHeaderContent.Substring( 0, 1 );


string protocol = m_IPHeaderContent.Substring(27, 2);
switch(protocol)
{
case "01":
m_Protocol = "ICMP";
break;
case "02":
m_Protocol = "IGMP";
break;
case "06":
m_Protocol = "TCP";
break;
case "11":
m_Protocol = "UDP";
break;
default:
m_Protocol = "OTHER";
break;
}

string originationIPAddress = m_IPHeaderContent.Substring( 36, 11 );
m_OriginationIPAddress = getIPAddress( originationIPAddress );

string destinationIPAddress = m_IPHeaderContent.Substring( 48, 11 );
m_DestinationIPAddress = getIPAddress( destinationIPAddress );

m_MessageLength = m_PackageLength - m_IPHeaderLength;
m_MessageContent = receiveString.Substring( m_IPHeaderLength * 3 );

try // 这两个字段没用到,但读出来的TCPHeaderLength有可能为空,之后读内容就会因为长度为-1而报错。具体原因待查
{
m_TCPHeaderLength = Convert.ToInt32( m_MessageContent.Substring(36, 1), 16 ) * 4;
m_TCPHeaderContent = m_MessageContent.Substring( 0, m_TCPHeaderLength * 3 - 1 );
}
catch
{
}

m_OriginationPort = ( Convert.ToUInt32( m_MessageContent.Substring(0, 2), 16 ) * 16 * 16
+ Convert.ToUInt32( m_MessageContent.Substring(3, 2), 16 ) ).ToString();
m_DestinationPort = ( Convert.ToUInt32( m_MessageContent.Substring(6, 2), 16 ) * 16 * 16
+ Convert.ToUInt32( m_MessageContent.Substring(9, 2), 16 ) ).ToString();
}


我想从socket中取到URL地址,不知道buf里有没有,目前能取到ip地址等,急需取到url地址,谢谢
...全文
评论 13
收藏 已收藏
分享
返回顶部
jinmingyuan
C# 111,129 成员 642,539 内容