MFC中用CHttpClient:HttpPost请求,需要身份验证,在哪个步骤设置登录名和密码?
#pragma once
#ifndef HTTPCLIENT_H
#define HTTPCLIENT_H
#include <afxinet.h>
#include <string>
using namespace std;
#define IE_AGENT _T("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)")
// 操作成功
#define SUCCESS 0
// 操作失败
#define FAILURE 1
// 操作超时
#define OUTTIME 2
class CHttpClient
{
public:
CHttpClient(LPCTSTR strAgent = IE_AGENT);
virtual ~CHttpClient(void);
int HttpGet(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse);
int HttpPost(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse);
private:
int ExecuteRequest(LPCTSTR strMethod, LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse);
void Clear();
private:
CInternetSession *m_pSession;
CHttpConnection *m_pConnection;
CHttpFile *m_pFile;
};
以上是在网上找的别人封装好的类,访问没有身份验证的url的时候能够正常获取json数据,但是对于一些需要身份验证的url就访问失败,以下是我的调用代码:
CString strUrl,strPostData;
std::string strResponse;
strUrl=_T("http://gc.ditu.aliyun.com/geocoding?a=苏州市");
int nRet=m_httpClient.HttpPost((LPCTSTR)strUrl,(LPCTSTR)strPostData,strResponse);