WinInet方式实现Get Post 有的网站能取到源码,有的却只能取到一部分,甚至返回bad request ,但不知道哪里出了问题
出问题的时候,HttpSendRequest 调用失败,错误码是12150
char *CWinInetOperate::WebVisit(CString strUrl, CString strVisitType, char *pcSubmitData, CString strSubmitCookies, CString strSubjoinProtocolHeader,
BOOL bNoRedirect, CString strProxyAddress )
{
CStringOperate c_StrOperate;
CString strTempUrl = strUrl;
BOOL bHttps = FALSE;
strTempUrl.MakeLower();
if (strTempUrl.Left(8) == L"https://")
bHttps = TRUE;
CString strUserAgent;
if (strSubjoinProtocolHeader.Find(L"User-Agent: ") != -1)
strUserAgent = c_StrOperate.GetStringMiddle(strSubjoinProtocolHeader, L"User-Agent: ", L"\r\n");
else
strUserAgent = L"Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)";
HINTERNET hInternet;
if (strProxyAddress.IsEmpty())
hInternet = InternetOpen(strUserAgent, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
else
{
if (bHttps)
hInternet = InternetOpen(strUserAgent, INTERNET_OPEN_TYPE_PROXY, strProxyAddress, NULL, 0);
else
hInternet = InternetOpen(strUserAgent, INTERNET_OPEN_TYPE_DIRECT, L"http=" + strProxyAddress, NULL, 0);
}
if (!hInternet)
return NULL;
HINTERNET hInternetConnect;
hInternetConnect = InternetConnect(hInternet, GetDomain(strUrl), GetPort(strUrl), NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if (!hInternetConnect)
{
InternetCloseHandle(hInternetConnect);
InternetCloseHandle(hInternet);
return NULL;
}
int nRequestSign = INTERNET_FLAG_RELOAD | INTERNET_COOKIE_THIRD_PARTY;
if (strSubmitCookies.IsEmpty() == FALSE)
nRequestSign |= INTERNET_FLAG_NO_COOKIES;
if (bNoRedirect)
nRequestSign |= INTERNET_FLAG_NO_AUTO_REDIRECT;
if (bHttps)
nRequestSign |= INTERNET_FLAG_SECURE;
else
nRequestSign |= INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS;
PCTSTR rgpszAcceptTypes[] = {L"text/*",NULL};
HINTERNET hHttpRequest = HttpOpenRequest(hInternetConnect, strVisitType, GetPageAddress(strUrl), L"HTTP/1.1", NULL,NULL, nRequestSign, 0);
if (!hHttpRequest)
{
InternetCloseHandle(hInternet);
InternetCloseHandle(hInternetConnect);
InternetCloseHandle(hHttpRequest);
return NULL;
}
if (strSubjoinProtocolHeader.IsEmpty())
strSubjoinProtocolHeader = L"Accept: */*";
else
{
if (strSubjoinProtocolHeader.Find(L"Accept:") == -1)
strSubjoinProtocolHeader = strSubjoinProtocolHeader + L"\r\n" + L"Accept: */*";
}
if (strSubjoinProtocolHeader.Find(L"Referer:") == -1)
strSubjoinProtocolHeader = strSubjoinProtocolHeader + L"\r\n" + L"Referer: " + strUrl;
if (strSubjoinProtocolHeader.Find(L"Accept-Language:") == -1)
strSubjoinProtocolHeader = strSubjoinProtocolHeader +L"\r\n" +L"Accept-Language: zh-cn";
if (strSubjoinProtocolHeader.Find(L"Content-Type:") == -1)
strSubjoinProtocolHeader = strSubjoinProtocolHeader +L"\r\n"+ L"Content-Type: application/x-www-form-urlencoded";
if (strSubmitCookies.IsEmpty() == FALSE)
strSubjoinProtocolHeader = strSubjoinProtocolHeader + L"\r\n" + L"Cookie: " + strSubmitCookies;
CString strTempVisitType = strVisitType;
strTempVisitType.MakeLower();
if (strTempVisitType == L"get")
HttpSendRequest(hHttpRequest, strSubjoinProtocolHeader, strSubjoinProtocolHeader.GetLength(), NULL, 0);
else
{
DWORD dwLength;
if (pcSubmitData == NULL)
dwLength = 0;
else
dwLength = strlen(pcSubmitData) + 1;
HttpSendRequest(hHttpRequest, strSubjoinProtocolHeader, strSubjoinProtocolHeader.GetLength(), pcSubmitData, dwLength);
}
DWORD nCount;
byte *pbtBuf = new byte[1024];
CByteArray btaryWebCode;
memset(pbtBuf, 0, 1024);
BOOL bFlag;
do
{
bFlag = InternetReadFile(hHttpRequest, pbtBuf, 1024, &nCount);
for (DWORD i = 0; i < nCount; i++)
btaryWebCode.Add(pbtBuf[i]);
} while (nCount);
nCount = btaryWebCode.GetCount();
char *pcWebCode = new char[nCount+1];
memset(pcWebCode, 0, nCount + 1);
for (DWORD i = 0; i < nCount; i++)
pcWebCode[i] = btaryWebCode.GetAt(i);
/* CFile file;
file.Open(L"C:\\Users\\Administrator.USER-20150718LB\\Desktop\\1.txt",CFile::modeReadWrite);
file.Write(pcWebCode,nCount);
file.Close();*/
InternetCloseHandle(hInternet);
InternetCloseHandle(hInternetConnect);
InternetCloseHandle(hHttpRequest);
return pcWebCode;
}
相关推荐
VC通过Http协议Get或Post方式与WebService通信,解析返回的Json
本资源是一个封装类,采用异步方式解决了Wininet不能设置超时的问题,当前异步采用C++11线程库及同步方式,可修改为MFC工作线程后在VS2012以下版本使用。JSON解析库采用JSONCPP。调用方式参考: CWininetHttp MyHttp; bool bSucess=false; iMyHttp.RequestJsonInfo((LPCTSTR)m_strUrl,Hr_Post,"Content-Type:application/x-www-form-urlencoded",strData,m_nTimeOut); if (!MyHttp.ParseJsonInfo()) { return OutPutFailInfo(MyHttp.GetErrInfo()); } MFC程序中通过HttpGet和HttpPost方式向WebService发送请求,WebService以Json的方式返回数据,MFC程序解析Json,得到指定数据。
api实现http请求,支持post和get方法
涉及范围,delphi6基本控件界面控件,线程,wininet api库文件为7z格式,号称宇宙中最强大的压缩格式,请自行去www.7-zip.org下载解压软件
易语言-wininet实现GET和POST访问网页
wininet实现GET和POST访问网页 易语言在线自学
wininet实现GET和POST访问网页
资源介绍:。wininet实现GET和POST访问网页。资源作者:。易语言在线自学。资源界面:。资源下载:。
C++ HTTP GET,POST的简单实例
1.一个简单的关于 HTTP GET,POST的简单实例 第一个方法是get 第二个是POST


网络编程
