码迷,mamicode.com
首页 > Web开发 > 详细

CInternetSession的简单使用

时间:2015-06-01 22:53:22      阅读:384      评论:0      收藏:0      [点我收藏+]

标签:服务器   数据   session   cinternetsession   

1. CInternetSession的简单使用

CInternetSession session;

CHttpFile *file = NULL;

CString strURL = " https://www.baidu.com"; //最新百度请求页面

CString strHtml = "”;   //存放打开的页面返回数据

 

try{

       file = (CHttpFile*)session.OpenURL(strURL);

}catch(CInternetException * m_pException){       

      CString strInfo; strInfo.Format(_T("CInternetException ErrorCode = %ld"),m_pException->m_dwError);

       file = NULL;            

       m_pException->Delete();

       session.Close();  

       MessageBox(strInfo);

}

CString strLine;

if(file != NULL){

       while(file->ReadString(strLine) != NULL){

       strHtml += strLine;

       }

 

}else{

       MessageBox("file is null!");

}

  MessageBox(strHtml ); //弹出看我们请求百度倒底返回的是什么哈

session.Close();

file->Close();

delete file;

file = NULL;

 

2. CInternetSession的代理与超时使用

CInternetSession session;

CHttpFile *file = NULL;  

 

INTERNET_PROXY_INFO proxyinfo;

proxyinfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;

proxyinfo.lpszProxy ="10.88.1.101:8080";

proxyinfo.lpszProxyBypass = NULL;

session.SetOption(INTERNET_OPTION_PROXY,(LPVOID)&proxyinfo,

sizeof(INTERNET_PROXY_INFO));

 

session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 5000);      // 5秒的连接超时

session.SetOption(INTERNET_OPTION_SEND_TIMEOUT, 1000);           // 1秒的发送超时

session.SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 7000);        // 7秒的接收超时

session.SetOption(INTERNET_OPTION_DATA_SEND_TIMEOUT, 1000);     // 1秒的发送超时

session.SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, 7000);       // 7秒的接收超时

session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);          // 1次重试

 

try{

       file = (CHttpFile*)session.OpenURL("http://10.88.1.101:8080/kmcs",1,

INTERNET_FLAG_TRANSFER_ASCII|INTERNET_FLAG_RELOAD|INTERNET_FLAG_DONT_CACHE);

}catch(CInternetException * m_pException){

       CString strInfo; strInfo.Format(_T("CInternetException ErrorCode = %ld"),m_pException->m_dwError);

       file = NULL;

       m_pException->Delete();

       session.Close();

       MessageBox(strInfo);

       return;

}

CString strLine;

if(file != NULL){

       while(file->ReadString(strLine) != NULL){

              MessageBox(strLine);

       }

}else{

       MessageBox("file is null!");

}

file->Close();

session.Close();

 delete file;

file = NULL;

3. CInternetSessionPOST使用

CString sSendUrl;
sSendUrl.Format(_T("&operate=save&layers=%s&addType=1&fields=%s&fieldValues=%s%d&geometry=%s"),sTXTabName,sFieldComoBox,CArrayYWDATAInfo.at(nIndex-1),nIndex,sValueZB);
CLog::Out("转码前的请求操作:" + sSendUrl +"\r\n");

CInternetSession m_InetSession(_T("session"),
0,
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
INTERNET_FLAG_DONT_CACHE);//设置不缓冲
CHttpConnection* pServer = NULL;//取得HTTP服务连接
CHttpFile* pFile = NULL;//取得读取HTTP文件连接等
CString strHtml = "";//GET返回的数据
CString strRequest = sSendUrl+"\r\n";//POST过去的数据.
CString strHeaders = "Content-Type:application/x-www-form-urlencoded";//发送服务器头数据.


//将发送的数据进行转码操作,有点操蛋……
string str;
strCoding::GB2312ToUTF_8(str,strRequest.GetBuffer(0),strRequest.GetLength());
strRequest = str.c_str();
CLog::Out("转码后的请求操作:" + strRequest + "\r\n");


try{
CString sPort = "";
CString sIP   = "";
CString sUrl  = "";
GetGraphServicesConfig(sIP,sPort,sUrl);


CLog::Out("请求的URL: http://" + sIP + ":" + sPort +"/" + sUrl);

                
INTERNET_PORT nPort=atol(sPort.GetBuffer(0)); //端口.
pServer = m_InetSession.GetHttpConnection(sIP, nPort);
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,sUrl);
pFile->AddRequestHeaders(strHeaders);
strHeaders = "CharSet:utf-8";
pFile->AddRequestHeaders(strHeaders);


pFile->SendRequestEx(strRequest.GetLength());
pFile->WriteString(strRequest);
pFile->EndRequest();
DWORD dwRet;
pFile->QueryInfoStatusCode(dwRet);


if (dwRet == HTTP_STATUS_OK)
{
CString strLine;
while (pFile->ReadString(strLine))
strHtml += strLine;


//入库成功返回的结果JSON样式:{"message":"入库成功","ID":81,"command":"success"}
//入库失败返回的结果JSON样式:{"message":"sr图形参数为空或异常。","command":"error"}
//入库结果转码,将UTF8转成GB2312.
                   
try
{
UrlCode url;
char destBuffer[256]={0};
url.URL_UTF8_Decode(strHtml.GetBuffer(0),destBuffer);
strHtml=destBuffer;
strHtml.TrimLeft("{");
strHtml.TrimRight("}");
strHtml.Replace("\"","");
}
catch(...)
{
CLog::Out("转码异常了,可能是图形服务端返回值乱码了\r\n");
}


CLog::Out("返回结果:" + strHtml + "\r\n");


//入库成功
if((strHtml.Find(_T("command:success")) != -1) && (strHtml.Find(_T("message:入库成功")) != -1))
{
sPlit.SetString(strHtml,_T(","));
for (int n = 0 ; n < sPlit.GetCount() ; ++n)
{
CSplitString splitStr;
splitStr.SetString(sPlit.GetString(n),":");
CString id = splitStr.GetString(0);
if(id.CompareNoCase("ID") ==0)
{
//存储已经成功能入图形库的地块的OBJECTID
CArrayLandID.push_back(make_pair(splitStr.GetString(1),it->second));


//将已成功入图形库的OBJECTID更到地块表d_kc的OBJECTID字段上,将业务库地块数据与图形库入库表建立对应关系
miniAdo* pAdo = lpReqInfo->GetAdoConn();
if (pAdo != NULL)
{
CString sSql;
sSql.Format(_T("update d_kc set objectid=%d where kcdjcg2=‘%s‘"),atol(splitStr.GetString(1)),it->second);
CLog::Out(sSql+"\r\n");
try
{
pAdo->BeginTrans();
pAdo->Execute(sSql);
pAdo->CommitTrans();
}
catch(...)
{
pAdo->RollbackTrans();
CLog::Out("执行:"+sSql+"异常!");
}


lpReqInfo->RelAdoConn(pAdo);
pAdo = NULL;
}
break;
}
}
}
else if(strHtml.Find(_T("command:error")) != -1) //图形入库失败
{
bFlag = TRUE;
CLog::Out(_T("---------------------执行图形入库操作失败,将执行回滚操作!--------------------------"));
for (vector<pr>::iterator it_ = CArrayLandID.begin() ; it_ != CArrayLandID.end() ; ++it_)
{
GraphDelete(sZBPKValue,it_->second,sTXTabName,it_->first);
}
}
else
{
CLog::Out("........................................................");
CLog::Out("--------------------执行到了,不可能执行到的地方------------------");
CLog::Out("........................................................");
bFlag=TRUE;
}
}
else
{
lpReqInfo->sRespond.Format(_T("调用图形接口入库异常,异常信息是==>HTTP:%ld\r\n"),dwRet),bFlag = TRUE;
CLog::Out(lpReqInfo->sRespond);
}
delete pFile;pFile = NULL;
delete pServer;pServer = NULL;
}
catch (CInternetException* e){
e->m_dwContext;
CLog::Out("执行HTTP Request进异常!");
bFlag=TRUE;
}

CInternetSession的简单使用

标签:服务器   数据   session   cinternetsession   

原文地址:http://blog.csdn.net/weikangc/article/details/46313621

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!