社区
Linux/Unix社区
帖子详情
linux下发送http请求
琴乐音
2007-11-14 10:27:45
想在linux下写一个daemon程序,向一个服务器发送请求, 获得服务器的文件, 服务器收到请求后,将文件传过来。
怎么在linux下用c或是其他语言 构造一个http(get)请求呢?
...全文
2120
11
打赏
收藏
linux下发送http请求
想在linux下写一个daemon程序,向一个服务器发送请求, 获得服务器的文件, 服务器收到请求后,将文件传过来。 怎么在linux下用c或是其他语言 构造一个http(get)请求呢?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
11 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
adream99
2007-11-19
打赏
举报
回复
顶
琴乐音
2007-11-19
打赏
举报
回复
lynx 是linux下的浏览器吧, 我是要做个daemon程序的哦。
dinner3000
2007-11-19
打赏
举报
回复
顶楼上的
ckc
2007-11-19
打赏
举报
回复
lynx -source http://服务器的地址
就可以了,不需要编程
fi9
2007-11-19
打赏
举报
回复
就是用recv来进行接收
或者用read也可以
更多的信息可以man recv或者man read
琴乐音
2007-11-16
打赏
举报
回复
上面的问题已经解决了,发送请求和接受200ok响应也可以了。 现在有个问题是, 我想在收到响应时判断响应体是否有数据,服务器传过来的压缩文件, 要怎么做呢? 服务器怎么发不管 , 只管客户端接受的部分,
recv函数可以用在这里么?
最后一起给分,不够再加。
琴乐音
2007-11-15
打赏
举报
回复
多谢上面几位的回复, 了解了大概的思路, 为什么我在linux上的terminal command line 里运行 看不到结果呢?
琴乐音
2007-11-15
打赏
举报
回复
to :
cangwu_lee 兄
试了你的程序, htconnect调用后失败,返回结果为小于1的数, 怎么连接不成功呢, 抓包 好象是DNS解析失败了。 我是用虚拟机运行的linux。
cangwu_lee
2007-11-14
打赏
举报
回复
//將樓上的代碼稍微修改,可以得到網站返回的HTML
//環境 CYGWIN 中。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
int htconnect(char *host, int port){
int white_sock;
struct hostent * site;
struct sockaddr_in me;
site = gethostbyname(host);
if (site==NULL) return -2;
white_sock = socket(AF_INET,SOCK_STREAM,0);
if (white_sock <0) return -1;
memset(&me, 0, sizeof(struct sockaddr_in));
memcpy(&me.sin_addr, site-> h_addr_list[0], site-> h_length);
me.sin_family = AF_INET;
me.sin_port = htons(port);
return (connect(white_sock, (struct sockaddr *)&me,sizeof(struct sockaddr)) <0) ? -1 : white_sock;
}
int htsend(int sock, char *fmt, ...){
char BUF[1024];
va_list argptr;
va_start(argptr,fmt);
vsprintf(BUF,fmt,argptr);
va_end(argptr);
printf(BUF);
return send(sock,BUF,strlen(BUF),0);
}
int main(int argc,char **argv){
int black_sock;
char bugs_bunny[3];
if (argc<2){
printf( "Please input the hostName. Example:\n %s www.sohu.com\n ",argv[0]);
return;
}
black_sock = htconnect(argv[1],80);
if (black_sock <1)return;
htsend(black_sock, "GET / HTTP/1.0\n");
htsend(black_sock, "Host: %s\n", argv[1]);
htsend(black_sock, "\n");
//htsend(black_sock, "%c ", 10);
while (read(black_sock, bugs_bunny, 1)> 0)
printf( "%c ",bugs_bunny[0]);
printf( "\n ");
close(black_sock);
}
編譯、執行
gcc -o linux_c_http_get linux_c_http_get.c
./linux_c_http_get www.sohu.com
cceczjxy
2007-11-14
打赏
举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
int htconnect(char *domain,int port)
{
int white_sock;
struct hostent * site;
struct sockaddr_in me;
site = gethostbyname(domain);
if (site==NULL) return -2;
white_sock = socket(AF_INET,SOCK_STREAM,0);
if (white_sock<0) return -1;
memset(&me,0,sizeof(struct sockaddr_in));
memcpy(&me.sin_addr,site->h_addr_list[0],site->h_length);
me.sin_family = AF_INET;
me.sin_port = htons(port);
return (connect(white_sock,(struct sockaddr *)&me,sizeof(struct sockaddr))<0) ? -1 : white_sock;
}
int htsend(int sock,char *fmt,...)
{
char BUF[1024];
va_list argptr;
va_start(argptr,fmt);
vsprintf(BUF,fmt,argptr);
va_end(argptr);
printf(BUF);
return send(sock,BUF,strlen(BUF),0);
}
int main(int argc,char **argv)
{
int black_sock;
char bugs_bunny[3];
if (argc<2)
{
printf("input your host.example:\n %s www.sohu.com\n",argv[0]);
return;
}
black_sock = htconnect(argv[1],80);
if (black_sock<0) return;
htsend(black_sock,"GET / HTTP/1.0%c",10);
htsend(black_sock,"Host: %s%c",argv[1],10);
htsend(black_sock,"%c",10);
while (read(black_sock,bugs_bunny,1)>0)
printf("%c",bugs_bunny[0]);
printf("\n");
close(black_sock);
}
vigorousfish
2007-11-14
打赏
举报
回复
分析一下HTTP的GET类型消息
将头所需要的每一行信息通过sprintf填充到buf中去,然后通过socket发向服务器.
注意,头当中每一行需要用\r\n来结尾,整个头用\r\n结尾
例如
sprintf(head,"HTTP/1.1 200 OK\r\n""Server: test/1.0\r\n""Date: May,23 06:20:12 2006 GTC\r\n""Content-Type: text/html\r\n""Accept-Ranges: bytes\r\n""Content-Length: 156\r\n""\r\n");
linux
下使用shell
发送
http请求
curl命令默认下就是使用get方式
发送
http请求
。 curl www.baidu.com 2. post请求 使用-d参数,形式如下: curl -d "param1=value1¶m2=value2" www.baidu.com 3.设置代理 curl -x 192...
Linux
指令
发送
Http请求
curl命令默认下就是使用get方式
发送
http请求
。 1 curl www.baidu.com 2. post请求 使用-d参数,形式如下: 1 curl -d "param1=value1¶m2=value2" www.baidu.com ...
Linux
使用shell
发送
http请求
Linux
使用shell
发送
http请求
一、curl 1. get请求 curl命令默认下就是使用get方式
发送
http请求
。 curl www.baidu.com 2. post请求 curl -X POST www.baidu.com 使用-d带参数请求参数,形式如下: curl -X POST &...
使用
Linux
命令
发送
http请求
get请求 curl命令模拟get请求: curl “http://www.baidu.com” 如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地 ...curl命令模拟get请求携带参数(
linux
): curl -v http://127.0.0.
linux
发送
http请求
命令
如果结果内容比较少,需要直接看到结果可以考虑使用curl进行模拟请求,如果返回结果比较多,则可考虑wget进行模拟请求。curl “http://www.baidu.com” 如果这里的URL指向的是一个文件或者一幅图都可以直接下载到...
Linux/Unix社区
23,116
社区成员
74,506
社区内容
发帖
与我相关
我的任务
Linux/Unix社区
Linux/Unix社区 应用程序开发区
复制链接
扫一扫
分享
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章