Chinaunix首页 | 论坛 | 博客
  • 博客访问: 201799
  • 博文数量: 48
  • 博客积分: 1935
  • 博客等级: 上尉
  • 技术积分: 491
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-29 00:59
文章分类

全部博文(48)

文章存档

2011年(1)

2010年(47)

我的朋友

分类: WINDOWS

2010-11-01 00:56:11

C下socket post 表单。
 
// post.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "winsock2.h"  
#pragma   comment(lib,"ws2_32.lib")  
#define   winsock_version   0x0101  
void   main()  
{  
//If   it   works,it   is   written   by   masterz,otherwise   I   don't   know   who   write   it.  
SOCKADDR_IN   saServer;  
LPHOSTENT   lphostent;  
WSADATA   wsadata;  
SOCKET   hsocket;  
int   nRet;  
const   char*   host_name="";  
char*   req="POST   /member/logon.asp   HTTP/1.0\r\n"  
    "From:   local\r\n"  
    "User-Agent:   post_test/1.0\r\n"  
    "Content-Type:   application/x-www-form-urlencoded\r\n"  
    "Content-Length:   34\r\n\r\n"  
    "name=XXXXXX&pass=XXXXXXXXX&type=1";   //if   you   change   xxx,   you   may   have   to   change   content-length   correspondingly  
if(WSAStartup(winsock_version,&wsadata))  
    printf("can't   initial   socket");  
lphostent=gethostbyname(host_name);  
if(lphostent==NULL)  
    printf("lphostent   is   null");  
hsocket   =   socket(AF_INET,   SOCK_STREAM,   IPPROTO_TCP);  
saServer.sin_family   =   AF_INET;  
//   Use   def.   now,   need   to   handle   general   case  
saServer.sin_port   =   htons(80);  
saServer.sin_addr   =   *((LPIN_ADDR)*lphostent->h_addr_list);  
nRet   =   connect(hsocket,   (LPSOCKADDR)&saServer,   sizeof(SOCKADDR_IN));  
if   (nRet   ==   SOCKET_ERROR)  
{  
    printf("can't   connect");  
    closesocket(hsocket);  
    return;  
}  
else  
    printf("connected   with   %s\n",host_name);  
nRet   =   send(hsocket,   req,   strlen(req),   0);  
if   (nRet   ==   SOCKET_ERROR)  
{  
    printf("send()   failed");  
    closesocket(hsocket);  
   
}  
else  
    printf("send()   OK\n");  
char   dest[1000];  
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);  
}  
}  
阅读(2264) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-11-01 17:44:32

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com