當前位置:首頁 » 編程語言 » c語言http請求

c語言http請求

發布時間: 2022-06-12 11:25:39

c語言實現的http請求中,User-Agent該填什麼

User Agent表示的是客戶端軟體類型,也就是瀏覽器類型

Ⅱ 如何使用c語言解析httppost請求

這個和具體的網頁有關系的,你可以用HttpWatch之類的抓包工具分析一個網頁的請求和返回。 然後就可以自己模仿相關的請求訪問該網頁了。

Ⅲ C語言建立TCP連接後,怎麼發送HTTP的GET請求

send發送請求串 回車 再回車。 一行結束後 換另一個語句 最後要有一個空行
GET ***** 回車
回車

Ⅳ 如何通過 c/c++ 實現http請求

示常式序,轉載自CNBLOG,做了針對C語言編譯器的適應性修正:

#include<stdio.h>
#include<winsock2.h>

#pragmacomment(lib,"ws2_32.lib")/*WinSock使用的庫函數*/

/*定義常量*/
#defineHTTP_DEF_PORT80/*連接的預設埠*/
#defineHTTP_BUF_SIZE1024/*緩沖區的大小*/
#defineHTTP_HOST_LEN256/*主機名長度*/

char*http_req_hdr_tmpl="GET%sHTTP/1.1 "
"Accept:image/gif,image/jpeg,*/* Accept-Language:zh-cn "
"Accept-Encoding:gzip,deflate Host:%s:%d "
"User-Agent:Huiyong'sBrowser<0.1> Connection:Keep-Alive ";


/**************************************************************************
*
*函數功能:解析命令行參數,分別得到主機名,埠號和文件名.命令行格式:
*[http://www..com:8080/index.html]
*
*參數說明:[IN]buf,字元串指針數組;
*[OUT]host,保存主機;
*[OUT]port,埠;
*[OUT]file_name,文件名;
*
*返回值:void.
*
**************************************************************************/
voidhttp_parse_request_url(constchar*buf,char*host,
unsignedshort*port,char*file_name)
{
intlength=0;
charport_buf[8];
char*buf_end=(char*)(buf+strlen(buf));
char*begin,*host_end,*colon,*file;

/*查找主機的開始位置*/

begin=(char*)(strstr(buf,"//"));
begin=(begin?begin+2:(char*)(buf));

colon=strchr(begin,':');
host_end=strchr(begin,'/');

if(host_end==NULL)
{
host_end=buf_end;
}
else
{/*得到文件名*/
file=strrchr(host_end,'/');
if(file&&(file+1)!=buf_end)
strcpy(file_name,file+1);
}
if(colon)/*得到埠號*/
{
colon++;

length=host_end-colon;
memcpy(port_buf,colon,length);
port_buf[length]=0;
*port=atoi(port_buf);

host_end=colon-1;
}

/*得到主機信息*/
length=host_end-begin;
memcpy(host,begin,length);
host[length]=0;
}


intmain(intargc,char**argv)
{
WSADATAwsa_data;
SOCKEThttp_sock=0;/*socket句柄*/
structsockaddr_inserv_addr;/*伺服器地址*/
structhostent*host_ent;

intresult=0,send_len;
chardata_buf[HTTP_BUF_SIZE];
charhost[HTTP_HOST_LEN]="127.0.0.1";
unsignedshortport=HTTP_DEF_PORT;
unsignedlongaddr;
charfile_name[HTTP_HOST_LEN]="index.html";
charfile_nameforsave[HTTP_HOST_LEN]="index1.html";
FILE*file_web;

if(argc!=2)
{
printf("[Web]input:%shttp://www.test.com[:8080]/index.html",argv[0]);
return-1;
}

http_parse_request_url(argv[1],host,&port,file_name);
WSAStartup(MAKEWORD(2,0),&wsa_data);/*初始化WinSock資源*/

addr=inet_addr(host);
if(addr==INADDR_NONE)
{
host_ent=gethostbyname(host);
if(!host_ent)
{
printf("[Web]invalidhost ");
return-1;
}

memcpy(&addr,host_ent->h_addr_list[0],host_ent->h_length);
}

/*伺服器地址*/
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(port);
serv_addr.sin_addr.s_addr=addr;

http_sock=socket(AF_INET,SOCK_STREAM,0);/*創建socket*/
result=connect(http_sock,(structsockaddr*)&serv_addr,sizeof(serv_addr));
if(result==SOCKET_ERROR)/*連接失敗*/
{
closesocket(http_sock);
printf("[Web]failtoconnect,error=%d ",WSAGetLastError());
return-1;
}

/*發送HTTP請求*/
send_len=sprintf(data_buf,http_req_hdr_tmpl,argv[1],host,port);
result=send(http_sock,data_buf,send_len,0);
if(result==SOCKET_ERROR)/*發送失敗*/
{
printf("[Web]failtosend,error=%d ",WSAGetLastError());
return-1;
}

file_web=fopen(file_nameforsave,"a+");

do/*接收響應並保存到文件中*/
{
result=recv(http_sock,data_buf,HTTP_BUF_SIZE,0);
if(result>0)
{
fwrite(data_buf,1,result,file_web);

/*在屏幕上輸出*/
data_buf[result]=0;
printf("%s",data_buf);
}
}while(result>0);

fclose(file_web);
closesocket(http_sock);
WSACleanup();

return0;
}

Ⅳ C語言,http報文,post請求,求大神詳解

URL要放在POST和HTTP/1.1之間,注意加空格。

URL好像不需要域名部分。

Ⅵ c語言怎麼實現http 請求頭發送

1。建立到伺服器的TCP連接
2。向伺服器發送GET或者POST報文,報文格式請參考HTTP協議
3。接收伺服器返回的報文

Ⅶ C語言寫的http協議post請求亂碼

Windows 的命令行下的字元集用的是 gb2312,但你 http 請求回來的數據字元集編碼是 utf-8。
轉換一下字元集編碼就好了。

Ⅷ http協議解析 請求行的信息怎麼提取 c語言源碼

實現步驟:
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請求

GET %s/ HTTP/1.0\r\n
%s 後面的/沒有的

熱點內容
mysql資料庫基本語句 發布:2025-02-07 16:41:48 瀏覽:249
醫院門禁密碼多少 發布:2025-02-07 16:41:43 瀏覽:527
伺服器遭美國ip攻擊簽名 發布:2025-02-07 16:22:48 瀏覽:546
如何配置二良腌料 發布:2025-02-07 16:11:54 瀏覽:735
資料庫課程設計學生管理系統 發布:2025-02-07 16:11:50 瀏覽:764
美國文化密碼是什麼 發布:2025-02-07 16:07:14 瀏覽:261
安卓手機下雪特效怎麼p 發布:2025-02-07 15:49:30 瀏覽:319
輪胎存儲銘牌 發布:2025-02-07 15:43:38 瀏覽:74
防盜鎖編程 發布:2025-02-07 15:31:33 瀏覽:860
安卓如何快速選擇圖片 發布:2025-02-07 15:30:43 瀏覽:468