VC模拟POST提交

来源:互联网 发布:单片机项目私活 编辑:程序博客网 时间:2024/05/17 09:07

VC模拟POST提交

源码(部分注释我已经删掉了,还是很容易看懂的):

#include "stdafx.h"#include <stdio.h>#include <afxinet.h>/***模拟POST提交*///////////////////////////////////////////////////////////////////////////void main(){CString strHost = _T("127.0.0.1");CString strUrl = _T("/action.asp");INTERNET_PORT m_Port = 80;CInternetSession m_Session(_T("HttpClient"));CHttpConnection * m_Server = NULL;CHttpFile * m_File = NULL;CString strRequest = _T("xsxh=20104833");//提交的数据CString strHeader =_T("Content-Type: application/x-www-form-urlencoded\r\n");try{m_Server = m_Session.GetHttpConnection(strHost,m_Port);if(m_Server == NULL)printf("CHttpConnection failed!");m_File = m_Server->OpenRequest(CHttpConnection::HTTP_VERB_POST,strUrl);if (m_File == NULL)printf("CHttpFile is NULL!");m_File->AddRequestHeaders(strHeader);m_File->AddRequestHeaders(_T("Accept-Language: zh-cn"));//特别这个头的部分,如果请求包头错误,那么肯定不会成功。建议:自己网页提交时抓包m_File->AddRequestHeaders(_T("Accept-Charset:GB2312"));m_File->SendRequest(strHeader,(LPVOID)(LPCTSTR)strRequest,strRequest.GetLength());DWORD httpStatus;m_File->QueryInfoStatusCode(httpStatus);printf("httpStatus:%d\n",httpStatus);if (httpStatus == HTTP_STATUS_OK){freopen("out.htm","w",stdout);CString strLine;int m_read;while((m_read = m_File->ReadString(strLine)) > 0)printf("%s\n",strLine);fclose(stdout);}elseprintf("HTTP_STATUS_FAILED\n");}catch (CInternetException* e){printf("Error:%d\n",e->m_dwContext);}if(m_File)delete m_File;if (m_Server)delete m_Server;m_Session.Close();}//////////////////////////////////////////////////////////////////////////

stdafx.h文件代码就不贴了,随便建一个MFC工程里面都会有,贴过来就OK。

原创粉丝点击