0
点赞
收藏
分享

微信扫一扫

MFC学习笔记10 HTTP请求

http get

代码示例

#include <afxinet.h>
//通过 http GET 协议来获取并保存文件
CInternetSession session;
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000 * 20);
session.SetOption(INTERNET_OPTION_CONNECT_BACKOFF, 1000);
session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);

CHttpConnection* pConnection = session.GetHttpConnection(TEXT("localhost"),(INTERNET_PORT)8080);
CHttpFile* pFile = pConnection->OpenRequest( CHttpConnection::HTTP_VERB_GET,
TEXT("/Practice/index.jsp"));

CString szHeaders = L"Accept: audio/x-aiff, audio/basic, audio/midi,\
audio/mpeg, audio/wav, image/jpeg, image/gif, image/jpg, image/png,\
image/mng, image/bmp, text/plain, text/html, text/htm\r\n";

pFile->AddRequestHeaders(szHeaders);

pFile->SendRequest();

DWORD dwRet;
pFile->QueryInfoStatusCode(dwRet);

if(dwRet != HTTP_STATUS_OK)
{
CString errText;
errText.Format(L"POST出错,错误码:%d", dwRet);
AfxMessageBox(errText);
}
else
{
int len = pFile->GetLength();
char buf[2000];
int numread;
CString filepath;
CString strFile = L"response.txt";
filepath.Format(L".\\%s", strFile);
CFile myfile( filepath,
CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
while ((numread = pFile->Read(buf,sizeof(buf)-1)) > 0)
{
buf[numread] = '\0';
strFile += buf;
myfile.Write(buf, numread);
}
myfile.Close();
}

session.Close();
pFile->Close();
delete pFile;

HTTP 状态码

Group

Meaning

200-299

Success

300-399

Information

400-499

Request error

500-599

Server error

常用状态码

Status code

Meaning

200

URL located, transmission follows

400

Unintelligible request

404

Requested URL not found

405

Server does not support requested method

500

Unknown server error

503

Server capacity reached

http post

CInternetSession session;
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000 * 20);
session.SetOption(INTERNET_OPTION_CONNECT_BACKOFF, 1000);
session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);

CHttpConnection* pConnection = session.GetHttpConnection( TEXT("localhost"),
(INTERNET_PORT)8080);
CHttpFile* pFile = pConnection->OpenRequest( CHttpConnection::HTTP_VERB_POST,
TEXT("/Practice/RequestObjectInJSP.jsp"),
NULL,
1,
NULL,
TEXT("HTTP/1.1"),
INTERNET_FLAG_RELOAD);

//需要提交的数据
CString szHeaders = L"Content-Type: application/x-www-form-urlencoded;";

//下面这段编码,则是可以让服务器正常处理
CHAR* strFormData = "username=WaterLin&password=TestPost";
pFile->SendRequest( szHeaders,
szHeaders.GetLength(),
(LPVOID)strFormData,
strlen(strFormData));

DWORD dwRet;
pFile->QueryInfoStatusCode(dwRet);

if(dwRet != HTTP_STATUS_OK)
{
CString errText;
errText.Format(L"POST出错,错误码:%d", dwRet);
AfxMessageBox(errText);
}
else
{
int len = pFile->GetLength();
char buf[2000];
int numread;
CString filepath;
CString strFile = L"result.html";
filepath.Format(L".\\%s", strFile);
CFile myfile(filepath,
CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
while ((numread = pFile->Read(buf,sizeof(buf)-1)) > 0)
{
buf[numread] = '\0';
strFile += buf;
myfile.Write(buf, numread);
}
myfile.Close();
}

session.Close();
pFile->Close();
delete pFile;

字符编码设置

CString szHeaders   = L"Content-Type: application/x-www-form-urlencoded;charset=UTF-8";

参考:
​​​http://cn.waterlin.org/Microsoft/MFC-HTTP-request.html​​


举报

相关推荐

0 条评论