c语言gethttp
实现步骤:
1)用Wireshark软件抓包得到test.pcap文件
2)程序:分析pcap文件头 -> 分析pcap_pkt头 -> 分析帧头 -> 分析ip头 -> 分析tcp头 -> 分析http信息
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<netinet/in.h>
#include<time.h>
#define BUFSIZE 10240
#define STRSIZE 1024
typedef long bpf_int32;
typedef unsigned long bpf_u_int32;
typedef unsigned short u_short;
typedef unsigned long u_int32;
typedef unsigned short u_int16;
typedef unsigned char u_int8;
//pacp文件头结构体
struct pcap_file_header
{
bpf_u_int32 magic; /* 0xa1b2c3d4 */
u_short version_major; /* magjor Version 2 */
u_short version_minor; /* magjor Version 4 */
bpf_int32 thiszone; /* gmt to local correction */
bpf_u_int32 sigfigs; /* accuracy of timestamps */
bpf_u_int32 snaplen; /* max length saved portion of each pkt */
bpf_u_int32 linktype; /* data link type (LINKTYPE_*) */
};
//时间戳
struct time_val
{
long tv_sec; /* seconds 含义同 time_t 对象的值 */
long tv_usec; /* and microseconds */
};
//pcap数据包头结构体
struct pcap_pkthdr
{
struct time_val ts; /* time stamp */
bpf_u_int32 caplen; /* length of portion present */
bpf_u_int32 len; /* length this packet (off wire) */
};
//数据帧头
typedef struct FramHeader_t
{ //Pcap捕获的数据帧头
u_int8 DstMAC[6]; //目的MAC地址
u_int8 SrcMAC[6]; //源MAC地址
u_short FrameType; //帧类型
} FramHeader_t;
//IP数据报头
typedef struct IPHeader_t
{ //IP数据报头
u_int8 Ver_HLen; //版本+报头长度
u_int8 TOS; //服务类型
u_int16 TotalLen; //总长度
u_int16 ID; //标识
u_int16 Flag_Segment; //标志+片偏移
u_int8 TTL; //生存周期
u_int8 Protocol; //协议类型
u_int16 Checksum; //头部校验和
u_int32 SrcIP; //源IP地址
u_int32 DstIP; //目的IP地址
} IPHeader_t;
//TCP数据报头
typedef struct TCPHeader_t
{ //TCP数据报头
u_int16 SrcPort; //源端口
u_int16 DstPort; //目的端口
u_int32 SeqNO; //序号
u_int32 AckNO; //确认号
u_int8 HeaderLen; //数据报头的长度(4 bit) + 保留(4 bit)
u_int8 Flags; //标识TCP不同的控制消息
u_int16 Window; //窗口大小
u_int16 Checksum; //校验和
u_int16 UrgentPointer; //紧急指针
}TCPHeader_t;
//
void match_http(FILE *fp, char *head_str, char *tail_str, char *buf, int total_len); //查找 http 信息函数
//
int main()
{
struct pcap_file_header *file_header;
struct pcap_pkthdr *ptk_header;
IPHeader_t *ip_header;
TCPHeader_t *tcp_header;
FILE *fp, *output;
int pkt_offset, i=0;
int ip_len, http_len, ip_proto;
int src_port, dst_port, tcp_flags;
char buf[BUFSIZE], my_time[STRSIZE];
char src_ip[STRSIZE], dst_ip[STRSIZE];
char host[STRSIZE], uri[BUFSIZE];
//初始化
file_header = (struct pcap_file_header *)malloc(sizeof(struct pcap_file_header));
ptk_header = (struct pcap_pkthdr *)malloc(sizeof(struct pcap_pkthdr));
ip_header = (IPHeader_t *)malloc(sizeof(IPHeader_t));
tcp_header = (TCPHeader_t *)malloc(sizeof(TCPHeader_t));
memset(buf, 0, sizeof(buf));
//
if((fp = fopen(“test.pcap”,”r”)) == NULL)
{
printf(“error: can not open pcap file\n”);
exit(0);
}
if((output = fopen(“output.txt”,”w+”)) == NULL)
{
printf(“error: can not open output file\n”);
exit(0);
}
//开始读数据包
pkt_offset = 24; //pcap文件头结构 24个字节
while(fseek(fp, pkt_offset, SEEK_SET) == 0) //遍历数据包
{
i++;
//pcap_pkt_header 16 byte
if(fread(ptk_header, 16, 1, fp) != 1) //读pcap数据包头结构
{
printf(“\nread end of pcap file\n”);
break;
}
pkt_offset += 16 + ptk_header->caplen; //下一个数据包的偏移值
strftime(my_time, sizeof(my_time), “%Y-%m-%d %T”, localtime(&(ptk_header->ts.tv_sec))); //获取时间
// printf(“%d: %s\n”, i, my_time);
//数据帧头 14字节
fseek(fp, 14, SEEK_CUR); //忽略数据帧头
//IP数据报头 20字节
if(fread(ip_header, sizeof(IPHeader_t), 1, fp) != 1)
{
printf(“%d: can not read ip_header\n”, i);
break;
}
inet_ntop(AF_INET, (void *)&(ip_header->SrcIP), src_ip, 16);
inet_ntop(AF_INET, (void *)&(ip_header->DstIP), dst_ip, 16);
ip_proto = ip_header->Protocol;
ip_len = ip_header->TotalLen; //IP数据报总长度
// printf(“%d: src=%s\n”, i, src_ip);
if(ip_proto != 0×06) //判断是否是 TCP 协议
{
continue;
}
//TCP头 20字节
if(fread(tcp_header, sizeof(TCPHeader_t), 1, fp) != 1)
{
printf(“%d: can not read ip_header\n”, i);
break;
}
src_port = ntohs(tcp_header->SrcPort);
dst_port = ntohs(tcp_header->DstPort);
tcp_flags = tcp_header->Flags;
// printf(“%d: src=%x\n”, i, tcp_flags);
if(tcp_flags == 0×18) // (PSH, ACK) 3路握手成功后
{
if(dst_port == 80) // HTTP GET请求
{
http_len = ip_len – 40; //http 报文长度
match_http(fp, “Host: “, “\r\n”, host, http_len); //查找 host 值
match_http(fp, “GET “, “HTTP”, uri, http_len); //查找 uri 值
sprintf(buf, “%d: %s src=%s:%d dst=%s:%d %s%s\r\n”, i, my_time, src_ip, src_port, dst_ip, dst_port, host, uri);
//printf(“%s”, buf);
if(fwrite(buf, strlen(buf), 1, output) != 1)
{
printf(“output file can not write”);
break;
}
}
}
} // end while
fclose(fp);
fclose(output);
return 0;
}
//查找 HTTP 信息
void match_http(FILE *fp, char *head_str, char *tail_str, char *buf, int total_len)
{
int i;
int http_offset;
int head_len, tail_len, val_len;
char head_tmp[STRSIZE], tail_tmp[STRSIZE];
//初始化
memset(head_tmp, 0, sizeof(head_tmp));
memset(tail_tmp, 0, sizeof(tail_tmp));
head_len = strlen(head_str);
tail_len = strlen(tail_str);
//查找 head_str
http_offset = ftell(fp); //记录下HTTP报文初始文件偏移
while((head_tmp[0] = fgetc(fp)) != EOF) //逐个字节遍历
{
if((ftell(fp) – http_offset) > total_len) //遍历完成
{
sprintf(buf, “can not find %s \r\n”, head_str);
exit(0);
}
if(head_tmp[0] == *head_str) //匹配到第一个字符
{
for(i=1; i<head_len; i++) //匹配 head_str 的其他字符
{
head_tmp[i]=fgetc(fp);
if(head_tmp[i] != *(head_str+i))
break;
}
if(i == head_len) //匹配 head_str 成功,停止遍历
break;
}
}
// printf(“head_tmp=%s \n”, head_tmp);
//查找 tail_str
val_len = 0;
while((tail_tmp[0] = fgetc(fp)) != EOF) //遍历
{
if((ftell(fp) – http_offset) > total_len) //遍历完成
{
sprintf(buf, “can not find %s \r\n”, tail_str);
exit(0);
}
buf[val_len++] = tail_tmp[0]; //用buf 存储 value 直到查找到 tail_str
if(tail_tmp[0] == *tail_str) //匹配到第一个字符
{
for(i=1; i<tail_len; i++) //匹配 head_str 的其他字符
{
tail_tmp[i]=fgetc(fp);
if(tail_tmp[i] != *(tail_str+i))
break;
}
if(i == tail_len) //匹配 head_str 成功,停止遍历
{
buf[val_len-1] = 0; //清除多余的一个字符
break;
}
}
}
// printf(“val=%s\n”, buf);
fseek(fp, http_offset, SEEK_SET); //将文件指针 回到初始偏移
}
㈡ C语言或者C++如何调用一个http接口并得到返回结果
用jni
首先
java
中
public
class
testhello
{
static
{
system.loadlibrary("testhellos");
}
public
static
native
void
hello(string
msg);
public
native
void
getsysid();
public
native
string
getkeycode(string
sysid);
public
native
boolean
testkeycode(string
sysid,
string
keycode);
public
static
void
main(string[]
args)
{
//
hello("hello,kimm!");
testhello
t=
new
testhello();
t.getsysid();
}
}
用javac
testhello.java,
java
testhello,javah
-classpath
.
-verbose
testhello
。将生产的头文件用到c++
中的
heardfileds
中。然后在
sources
files
中实现
heardfieds
的方法。实现的方法,其实就是你要调用c++的方法、
㈢ 用纯C语言 怎样编程,解析HTTP协议。
按照HTTP协议的规定,理清楚相关功能。
然后软件模型的方式解释HTTP的运行过程,可以使用UML语言。
然后将UML语言转换成C语言即可。
㈣ c语言怎么实现http 请求头发送
1。建立到服务器的TCP连接
2。向服务器发送GET或者POST报文,报文格式请参考HTTP协议
3。接收服务器返回的报文
㈤ 在linux下如何利用C语言实现HTTP的get和post方法
下载wget的源码看看就知道了
㈥ C语言建立TCP连接后,怎么发送HTTP的GET请求
send发送请求串 回车 再回车。 一行结束后 换另一个语句 最后要有一个空行
GET ***** 回车
回车
㈦ c语言中get的用法
面向对象编程的一个规则是:使用户只需要指定对象能做什么,而不需要去指定或者了解对象是如何完成该的。 下面我就跟你们详细介绍下c语言中get的用法,希望对你们有用。
c语言中get的用法如下:
通过get和set访问器来访问,就保持了这种规则,而且还提供了额外的功能,如对值进行检查,实现只读或只写属性等。
总的来说就是可以通过set get 来对类的属性进行封装,面向对象的程序是不可以直接用类自己定义的属性的
刚刚学C#的时候,很多人都不明白属性的访问器有什么用呢,我上网见了不少的评论,拉下来给大家看看
一般面向对象编程语言都要求成员变量不能直接暴露给外部访问,如下:
public class A
{
public int Age; //这是不好的,待会有程序员可能把-1赋给Age
}
为了防止乱赋值,C#设计了一个属性机制,要求把成员变量设为私有,在通过属性来控制成员变量的读写,如下:
public class B
{
private int age; //私有成员变量
public int Age //age的属性
{
get{ return age; } //读取器
set{ //写入器
if(value>0)
age = value; //value是准备写入的值
}
}
}
这样Age就像保安一样,挡在age的前面。访问age的时候需要通过Age许可,如下:
B b = new B();
b.Age = -1; //这是不行的,-1被带入value,value<0,判断不成立
b.Age = 10; //这是可行的
如果还有不懂的话可以留言,我尽量教会你,下面是我一个简单是例子
//-------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
public class Myclass
{
private string id="";
private string name="";
public string ID
{
get
{
return id;
}
set
{
id =value;
}
}
public string Name
{
get
{
return name;
}
set
{
name =value;
}
}
}
class Program
{
static void Main(string[] args)
{
Myclass clazz = new Myclass ();
clazz.ID ="我爱你阿";
clazz.Name="王素莹";
Console.WriteLine("{0}他是谁?{1}",clazz.ID,clazz.Name);
Console.ReadLine();
}
}
}
㈧ C语言编写http proxy代理程序问题
没有函数可以做这个,你要自己把客户发给你的URL中的命令(GET。。)HOST NAME, PORT NAME, HTTP/1.1OR1.0 还有PATH找出来,然后和服务器建立连接,再把header 改编发给服务器,具体的格式可以用wireshark看,然后接受服务器返回给你的包,原封不动再返回给client
㈨ 如何用c语言实现http服务器
去看一下《Advanced Linux Programming》这本书吧,第11章讲的就是怎么用C语言实现一Http服务器。 这里有下载地址(英文的): http://www.advancedlinuxprogramming.com/alp-folder 英文看起来不顺的话可以上网找找有没有中文版的这本书,应该叫Linux高级编程吧~~~参考资料: http://www.advancedlinuxprogramming.com/alp-folder