mfc-vc++_模拟网页get,post数据教程例子

关于vc的网络编程的一个概览,请移步至:

http://www.youthtribe.com/archives/1323

先上一个vc++ post数据的例子:


	// http 链接变量
	CHttpConnection * m_http = NULL ;
	//m_http通过下边的方法取值
	CString strURL = "www.youthtribe.com"	//不能有http://
	CInternetSession CIS;
	m_http = CIS.GetHttpConnection(strURL) ;
	//chttpfile
	CHttpFile * pHttpFile = NULL;
	pHttpFile = pdlg->m_http->OpenRequest(CHttpConnection::HTTP_VERB_GET,	//GET or POST
				"?p=p",//"test/test.php",
				NULL,
				1,
				NULL,
				NULL,
				INTERNET_FLAG_EXISTING_CONNECT);
	//发送header
	pHttpFile->AddRequestHeaders("Content-Type:application/x-www-form-urlencoded"); 
	pHttpFile->AddRequestHeaders("Accept:*/*");
	//SendRequest,因为是get,所以值都为0
	pHttpFile->SendRequest(NULL,0,NULL,0)
	/*
	这个是post的
	CString strPost;
	strPost = "p=p";	//post的数据
	pHttpFile->SendRequest(NULL,0,(LPVOID)(LPCTSTR)strSend,strSend.GetLength()); 	
	*/
	//获取返回状态码
	DWORD   retcode; 
	pHttpFile->QueryInfoStatusCode(retcode);
	//可以这样读字符串
	//一行一行的读
	//一行一行的读出来
	CString strReturn , strLine;
	while(pHttpFile->ReadString(strLine))
	{
		strReturn = strLine + "\n";
	}

One comment

Leave a Reply