VC模拟POST提交

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);
		}
		else
			printf("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。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值