0
回答
模拟HTTP请求, POST方法(附源码)
滴滴云服务器,限时包月0.9元,为开发者而生>>>   

Fiddler是一款免费的Http抓包工具,功能强大,可与商业软件媲美。下载地址:

http://www.fiddlertool.com/fiddler/version.asp

 

 

 

#include <afxwin.h>
#include <stdio.h>
#include <windows.h>
#include "Wininet.h"
#include <WinSock2.h>

#pragma comment(lib,"Wininet.lib")
#pragma comment(lib,"nafxcwd.lib")
#pragma comment(lib, "ws2_32.lib")

//模拟浏览器发送HTTP请求函数
CString HttpRequest(TCHAR * lpHostName,short sPort,TCHAR * lpUrl,TCHAR * lpMethod,TCHAR * lpPostData,int nPostDataLen)
{
 HINTERNET hInternet,hConnect,hRequest;
 BOOL bRet;
 CString strResponse;
 FILE * fp ;
 static TCHAR *accept = _T("Accept: */*");
 TCHAR hdrs[] = L"Content-Type: application/x-www-form-urlencoded"
  L"Accept-Language: zh-cn"
  L"Accept-Encoding: gzip, deflate"
  L"Pragma: no-cache";

 hInternet = InternetOpen(L"User-Agent",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
 if(!hInternet)
  goto Ret0;

 hConnect = InternetConnect(hInternet,lpHostName, INTERNET_DEFAULT_HTTP_PORT,NULL, NULL, INTERNET_SERVICE_HTTP,0,1);
 if(!hConnect)
  goto Ret0;

 hRequest = HttpOpenRequest(hConnect, lpMethod, lpUrl, L"HTTP/1.1", NULL, NULL, INTERNET_FLAG_RELOAD, 1);
 DWORD erro = GetLastError();
 if(!hRequest)
  goto Ret0;

 bRet = HttpSendRequest(hRequest,hdrs, wcslen(hdrs), "key=love&go=go&y=1",strlen("key=love&go=go&y=1"));


 fp = fopen("C://a.html","w");
 while(TRUE)
 {
  char cReadBuffer[4096];
  unsigned long lNumberOfBytesRead;
  bRet = InternetReadFile(hRequest,cReadBuffer,sizeof(cReadBuffer) - 1,&lNumberOfBytesRead);
  if(!bRet || !lNumberOfBytesRead)
   break;
  cReadBuffer[lNumberOfBytesRead] = 0;
  strResponse = strResponse + cReadBuffer;
  fwrite(cReadBuffer,lNumberOfBytesRead,1,fp);
 }
 fclose(fp);

Ret0:
 if(hRequest)
  InternetCloseHandle(hRequest);
 if(hConnect)
  InternetCloseHandle(hConnect);
 if(hInternet)
  InternetCloseHandle(hInternet);

 return strResponse;
}

 

void main(int argc, char *argv[])

 
 CString strResponse = HttpRequest(L"lrc.bzmtv.com",80,L"/So.asp", L"POST", L"key=love&go=go&y=1",wcslen(L"key=love&go=go&y=1"));

}

 

 

自己慢慢看吧,GET方法更简单,在此不表。

 

还有一个socket api版本,代码如下:

 

#include <stdio.h>
#include   "winsock.h"  
#pragma   comment(lib,"ws2_32.lib")  
#define   winsock_version   0x0101 

void   main()  
{  
 //I   create     C:/Inetpub/wwwroot/test/test.asp   ,start   the   web   service  
 //start   my   program,   the   result   is   OK.  
 //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="lrc.bzmtv.com";  
 char*   req="POST /So.asp HTTP/1.1/r/n"  
  //"From:   local/r/n"  
  "Accept: */* /r/n"
  "Content-Type: application/x-www-form-urlencoded/r/n"
  "Accept-Language: zh-cn/r/n"
  "Accept-Encoding: gzip, deflate/r/n"
  "User-Agent: post_test/1.1/r/n" 
  "Content-Length: 18/r/n"
  "Host: lrc.bzmtv.com/r/n"   
  "Pragma: no-cache/r/n/r/n"
  "key=love&go=go&y=1/r/n";  
 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; 
 FILE *fp;
 fp = fopen("C://a.html", "w");
 while(nRet>0)  
 {  
  nRet=recv(hsocket,(LPSTR)dest,sizeof(dest),0);  
  if(nRet>0)  
   dest[nRet]=0;  
  else  
   dest[0]=0; 

  fwrite(dest, nRet, 1, fp);
  printf("/nReceived   bytes:%d/n",nRet);  
  printf("Result:/n%s",dest);  
 }  

 fclose(fp);
}


原文链接:http://blog.csdn.net/favormm/article/details/4649628
<无标签>
举报
长平狐
发帖于5年前 0回/1K+阅
顶部