http://blog.163.com/liguiqin_500/blog/static/763064200912693950579/
-
#include <stdlib.h>
-
#include <errno.h>
-
#include <string.h>
-
#include <unistd.h>
-
#include <sys/socket.h>
-
#include <netinet/in.h>
-
#include <arpa/inet.h>
-
#include <netdb.h>
-
#include <stdio.h>
-
-
void posturl()
-
{
-
int sockfd;
-
struct sockaddr_in addr;
-
struct hostent *pURL;
-
-
char *pHost = 0, *pPOST = 0;
-
static char text[BUFSIZ];
-
char header[BUFSIZ] = "";
-
int i;
-
-
/*
-
* 设定socket参数,并未真正初始化
-
*/
-
sockfd = socket(AF_INET, SOCK_STREAM, 0);
-
-
addr.sin_family = AF_INET;
-
inet_aton("192.168.10.200",&addr.sin_addr);
-
addr.sin_port = htons(80);
-
-
/*
-
* 组织发送到web服务器的信息
-
* 为何要发送下面的信息请参考HTTP协议的约定
-
*/
-
strcat(header, "POST ");
-
strcat(header, "/pass");
-
strcat(header, " HTTP/1.1\r\n");
-
strcat(header, "*/*\r\n");
-
strcat(header, "Referer: \r\n");
-
strcat(header, "Accept-Language: zh-cn\r\n");
-
strcat(header,"Content-Type: application/x-www-form-urlencoded\r\n");
-
strcat(header, "HOST: ");
-
strcat(header, "192.168.10.200\r\n");
-
strcat(header, "Content-Length: 26\r\n");
-
strcat(header, "Connection: Keep-Alive\r\n");
-
strcat(header, "Cache-Control: no-cache\r\n\r\n");
-
strcat(header, "password=1234&submit=Apply");
-
strcat(header, "\r\n");
-
-
/*
-
* 连接到服务器,发送请求header,并接受反馈(即网页源代码)
-
*/
-
connect(sockfd,(struct sockaddr *)&addr,sizeof(struct sockaddr_in));
-
-
i = send(sockfd, header, strlen(header), 0);
-
-
printf("%d\n",i);
-
-
while ( recv(sockfd, text, BUFSIZ, 0) > 0)
-
{
-
printf("%s", text);
-
for(i = 0; i < BUFSIZ; i++)
-
text[i]='\0';
-
//strnset(text, '\0', BUFSIZ);
-
}
-
-
close(sockfd);
-
-
-
}
-
-
int main()
-
{
-
posturl();
-
return 0;
-
}
-
//*****************************************//
-
//*********利用C实现自动发包***************//
-
//*********作者:马 犇******************//
-
//*********时间:2011-6-25*****************//
-
//*****************************************//
-
-
#include "stdafx.h"
-
#include "stdio.h"
-
#include "stdlib.h"
-
#include "winsock2.h"
-
#pragma comment(lib,"ws2_32.lib")
-
int main(int argc, char* argv[])
-
{
-
-
SOCKET hsocket;
-
SOCKADDR_IN saServer;
-
WSADATA wsadata;
-
LPHOSTENT lphostent;
-
int nRet;
-
char* host_name="www.********.com";
-
char* reqHead="POST ********* HTTP/1.1\r\n" //此为要发送的数据包
-
"Accept: */*\r\n"
-
"Referer: *********\r\n"
-
"Accept-Language: zh-CN\r\n"
-
"User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; BOIE9;ZHCN; twchrome)\r\n"
-
"Content-Type: application/x-www-form-urlencoded\r\n"
-
"Accept-Encoding: gzip, deflate\r\n"
-
"Host: www.*******.com\r\n"
-
"Content-Length: 375\r\n"
-
"Connection: Keep-Alive\r\n"
-
"Cache-Control: no-cache\r\n"
-
"Cookie: ************** \r\n\r\n"
-
"user=test&pass=123";
-
-
// 初始化套接字
-
if(WSAStartup(MAKEWORD(2,2),&wsadata))
-
printf("初始化SOCKET出错!");
-
lphostent=gethostbyname(host_name);
-
if(lphostent==NULL)
-
printf("lphostent为空!");
-
hsocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
-
saServer.sin_family = AF_INET;
-
saServer.sin_port = htons(80);
-
saServer.sin_addr = *((LPIN_ADDR)*lphostent->h_addr_list);
-
// 利用SOCKET连接
-
nRet = connect(hsocket,(LPSOCKADDR)&saServer,sizeof(SOCKADDR_IN));
-
if(nRet == SOCKET_ERROR)
-
{
-
printf("建立连接时出错!");
-
closesocket(hsocket);
-
return 0;
-
}
-
// 利用SOCKET发送
-
-
nRet = send(hsocket,req,strlen(req),0);
-
if(nRet==SOCKET_ERROR)
-
{
-
printf("发送数据包时出错!");
-
closesocket(hsocket);
-
}
-
char Dest[3000];
-
nRet=1;
-
while(nRet>0)
-
{
-
// 接收返回数据包
-
nRet=recv(hsocket,(LPSTR)Dest,sizeof(Dest),0);
-
if(nRet>0)
-
Dest[nRet]=0;
-
else
-
Dest[0]=0;
-
// 显示返回数据包的大小、内容
-
printf("\nReceived bytes:%d\n",nRet);
-
printf("Result:\n%s",Dest);
-
}
-
}
-
return 0;
-
}
C语言开发Linux下web服务器(支持GET/POST,SSL,目录显示等)
http://blog.csdn.net/yueguanghaidao/article/details/8450938/
阅读(4381) | 评论(0) | 转发(0) |