如何获取http中的content-type?

pioneer_55 2010-12-10 08:54:40
用winpcap抓包之后用过滤器过滤出使用http协议的数据包,接下来如何获得content-type呢?恳请各位大牛指教!
...全文
799 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
pioneer_55 2010-12-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zzw820626 的回复:]
用正则表达式匹配下比较简单。
不然就搜索content-type关键字,然后取后面内容
[/Quote]

正则表达式是什么?
用字符串匹配的话效率非常低
zzw820626 2010-12-11
  • 打赏
  • 举报
回复
用正则表达式匹配下比较简单。
不然就搜索content-type关键字,然后取后面内容
Sou2012 2010-12-11
  • 打赏
  • 举报
回复
以前写的HTTP下载器的片断。

#pragma once

#include <string>
#include <map>

#include "MainDlg.h"
#include "StringTool.h"

using namespace std;

class CHttp
{
private:
int Recv(char *buf, int len)
{
int totalRecv = 0;
int recvByte = 0;

while (totalRecv < len)
{
recvByte = ::recv(m_socket, &buf[totalRecv], len - totalRecv, 0);
if (recvByte <= 0)
{
// recvByte == 0, connection has been gracefully closed
// recvByte < 0, SOCKET_ERROR
return -1;
}
totalRecv += recvByte;
}
return totalRecv;
}

bool Send(char *buf, int len)
{
int totalSend = 0;
int sendByte = 0;

while (totalSend < len)
{
sendByte = ::send(m_socket, &buf[totalSend], len - totalSend, 0);
if (sendByte <= 0)
{
return false;
}
totalSend += sendByte;
}
return true;
}

bool GetHttpHeader(SOCKET s, char *headerBuf, int len)
{
char *pBuf = new char[1];
int i = 0;
while (i < len)
{
if (!Recv(pBuf, 1))
{
break;
}

headerBuf[i] = pBuf[0];

if (strstr(headerBuf, "\r\n\r\n") != NULL)
{
return true;
}

i++;
}
delete pBuf;
return false;
}

void ParseHttpHeader(char *headerBuf)
{
char *temp = headerBuf;

int pos = 0;
while ((temp = strstr(temp + 2, "\r\n")) != NULL)
{
int nRet = (temp - headerBuf) - pos;
if (nRet == 2)
{
// is \r\n\r\n, http header end
break;
}

char *line = new char[nRet + 1];
line[nRet] = '\0';

strncpy(line, &headerBuf[pos], nRet);

pos += nRet;

InitHttpHeaderMap(line);

delete [] line;
line = NULL;
}
}
public:
int GetHttpCode()
{
return atoi(GetHeaderField(string("http-code")).c_str());
}

int GetContentLength()
{
return atoi(GetHeaderField(string("\r\nContent-Length")).c_str());
}

string GetServer()
{
return GetHeaderField(string("\r\nServer"));
}

string GetConnection()
{
return GetHeaderField(string("\r\nConnection"));
}

string GetContentDisposition()
{
return GetHeaderField(string("\r\nContent-Disposition"));
}

string GetAcceptRanges()
{
return GetHeaderField(string("\r\nAccept-Ranges"));
}

string GetContentType()
{
return GetHeaderField(string("\r\nContent-Type"));
}

string GetLocation()
{
string location = GetHeaderField(string("\r\nLocation"));
if (location == "empty")
{
location = GetHeaderField(string("\r\nlocation"));
}
return location;
}

string GetDate()
{
return GetHeaderField(string("\r\nDate"));
}

private:
void ParseHttpCode(char *line, char *szHttpCode)
{
szHttpCode[0] = line[9];
szHttpCode[1] = line[10];
szHttpCode[2] = line[11];

szHttpCode[3] = '\0';
}

void InitHttpHeaderMap(char *line)
{
if (m_http_header_map.empty())
{
char szHttpCode[4] = {0};
ParseHttpCode(line, szHttpCode);

string s_httpCode = szHttpCode;
m_http_header_map.insert(pair<string, string>("http-code", s_httpCode));
}
else
{
char *p = strchr(line, ':');

int nKeyLen = p - line;
int nValueLen = -(line - p);

char *key = new char[nKeyLen + 1];
key[nKeyLen] = '\0';

char *value = new char[nValueLen + 1];
value[nValueLen] = '\0';

strncpy(key, line, nKeyLen);
value = p + 1;

m_http_header_map.insert(pair<string, string>(key, value));
}
}

string GetHeaderField(string key)
{
http_header_map_iter iter = m_http_header_map.find(key);
if (iter != m_http_header_map.end())
{
return (*iter).second;
}
return "empty";
}

void GetIpAddrByHost(const char *host, SOCKADDR_IN *pAddr)
{
if (strlen(host) > 0)
{
HOSTENT *lpHostEnt = gethostbyname(host);
if (!lpHostEnt)
{
return;
}
if (lpHostEnt)
{
char ipAddrBuf[20];
sprintf(ipAddrBuf, "%d.%d.%d.%d",
lpHostEnt->h_addr_list[0][0] & 0x00ff,
lpHostEnt->h_addr_list[0][1] & 0x00ff,
lpHostEnt->h_addr_list[0][2] & 0x00ff,
lpHostEnt->h_addr_list[0][3] & 0x00ff);
pAddr->sin_addr.S_un.S_addr = inet_addr(ipAddrBuf);
}
}
}
private:
static const unsigned int HTTP_HEADER_BUFFER_SIZE = 1024;
private:
map<string, string> m_http_header_map;
typedef map<string, string>::iterator http_header_map_iter;

char m_http_header[HTTP_HEADER_BUFFER_SIZE];
private:
SOCKET m_socket;
};
Sou2012 2010-12-11
  • 打赏
  • 举报
回复
这样的。 当HTTP响应回来的时候,一直读入, 碰到\r\n\r\n 就停止。

\r\n\r\n之前的都是HTTP 的响应头

然后响应头的第一行。都是\r\n区分的。。

比如
Content-Type: text/html\r\n
看到上面的冒号了吗? 用截取字符串,就很容易取出来 text/html了。

Sou2012 2010-12-11
  • 打赏
  • 举报
回复
。。其实最简单的。。使用Find也找到了。。 我以前写过的。

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧