我用下面的程序来Get到别人用java生成的一个xml文件,在IE浏览器中输入:
http://192.16.5.12:8081/test/test1/TreeServlet可以把所要的页面显示出来但我用下面的程序总是Get不到但获得其他网站的内容还是能或地到的,不知为什么. CString szServer="192.16.5.12";
CString csResponse;
INTERNET_PORT nPort=8081;
CString szObject="/test/test1/TreeServlet";
CInternetSession* pSession = NULL;
CHttpConnection* pConnection = NULL;
CHttpFile* pHttpFile = NULL;
DWORD dwHttpStatus;
try
{
pSession = new CInternetSession(NULL,1,INTERNET_OPEN_TYPE_PRECONFIG);
pConnection = pSession->GetHttpConnection(szServer,nPort,NULL,NULL);
pHttpFile = pConnection->OpenRequest(1,szObject,szServer,1,NULL,NULL,INTERNET_FLAG_EXISTING_CONNECT);
pHttpFile->SendRequest();
if (pHttpFile)
{
if (pHttpFile->QueryInfoStatusCode(dwHttpStatus)!=0) {
if (dwHttpStatus < 400)
{
int nRead = 0;
LPSTR pBuffer = new char[1024];
do
{
nRead = pHttpFile->Read(pBuffer, 1023);
if (nRead != 0)
{
pBuffer[nRead] = 0;
csResponse += pBuffer;
}
} while (nRead != 0);
MessageBox(csResponse);
if(pBuffer)
{
delete pBuffer;
pBuffer = NULL;
}
}
}
}
}
catch (CInternetException* e)
{
e->Delete();
}
catch (...)
{
}
if (pHttpFile != NULL)
{
pHttpFile->Close();
delete pHttpFile;
}
if (pConnection != NULL)
{
pConnection->Close();
delete pConnection;
}
if (pSession != NULL)
{
pSession->Close();
delete pSession;
}
http://192.16.5.12:8081/test/test1/TreeServlet可以把所要的页面显示出来但我用下面的程序总是Get不到但获得其他网站的内容还是能或地到的,不知为什么. CString szServer="192.16.5.12";
CString csResponse;
INTERNET_PORT nPort=8081;
CString szObject="/test/test1/TreeServlet";
CInternetSession* pSession = NULL;
CHttpConnection* pConnection = NULL;
CHttpFile* pHttpFile = NULL;
DWORD dwHttpStatus;
try
{
pSession = new CInternetSession(NULL,1,INTERNET_OPEN_TYPE_PRECONFIG);
pConnection = pSession->GetHttpConnection(szServer,nPort,NULL,NULL);
pHttpFile = pConnection->OpenRequest(1,szObject,szServer,1,NULL,NULL,INTERNET_FLAG_EXISTING_CONNECT);
pHttpFile->SendRequest();
if (pHttpFile)
{
if (pHttpFile->QueryInfoStatusCode(dwHttpStatus)!=0) {
if (dwHttpStatus < 400)
{
int nRead = 0;
LPSTR pBuffer = new char[1024];
do
{
nRead = pHttpFile->Read(pBuffer, 1023);
if (nRead != 0)
{
pBuffer[nRead] = 0;
csResponse += pBuffer;
}
} while (nRead != 0);
MessageBox(csResponse);
if(pBuffer)
{
delete pBuffer;
pBuffer = NULL;
}
}
}
}
}
catch (CInternetException* e)
{
e->Delete();
}
catch (...)
{
}
if (pHttpFile != NULL)
{
pHttpFile->Close();
delete pHttpFile;
}
if (pConnection != NULL)
{
pConnection->Close();
delete pConnection;
}
if (pSession != NULL)
{
pSession->Close();
delete pSession;
}
》》》pSession = new CInternetSession(NULL,1,INTERNET_OPEN_TYPE_PRECONFIG);
里面的参数INTERNET_OPEN_TYPE_PRECONFIG改为INTERNET_OPEN_TYPE_PROXY就行了,好像后一个是通过代理得到的,我现在也不是很清除,大家那位清楚这个问题可以来讨论讨论。
CString szServer="192.16.5.18"; 要发送的地址
CString csResponse;
INTERNET_PORT nPort=7001; //要发送的端口
CString szObject="/min/sh/shDatebaseServlet";
CInternetSession* cInternetSession=NULL;
cInternetSession=new CInternetSession(NULL,1,INTERNET_OPEN_TYPE_PROXY);
CHttpConnection * pHttpConnection = cInternetSession->GetHttpConnection(szServer, nPort);
CHttpFile * pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, szObject,szServer,1,NULL,NULL,INTERNET_FLAG_EXISTING_CONNECT);
pHttpFile->SendRequest(NULL,0, szStrXML.GetBuffer(0), szStrXML.GetLength());
int nRead = 0;
LPSTR pBuffer = new char[1024];
do
{
nRead = pHttpFile->Read(pBuffer, 1023);
if (nRead != 0)
{
pBuffer[nRead] = 0;
csResponse += pBuffer;
}
} while (nRead != 0);
szFanhui=csResponse; 得到返回的内容;
cInternetSession->Close();
pFile -> AddRequestHeaders("Content-Type: application/x-www-form-urlencoded");
pFile -> AddRequestHeaders("Accept: */*");在你上一个帖子里面已经说了
http://expert.csdn.net/Expert/topic/2875/2875315.xml?temp=.3907434
pFile -> AddRequestHeaders("Content-Type: application/x-www-form-urlencoded");
pFile -> AddRequestHeaders("Accept: */*");
{
CRegKey m_Key;
CString strServerIP;
CString strServerPort;
CString strLoginUserName;
CString strLoginPassword;
CString strLoginAddress;
CString strLoginDataAddress;
if(m_Key.Open(HKEY_LOCAL_MACHINE,"Software\\QQ\\Task")==ERROR_SUCCESS)
{
char strValue[1024];
DWORD pSize=1024; if(m_Key.QueryValue(strValue,"ServerIP",&pSize)!=ERROR_SUCCESS)
{m_Key.QueryValue(strValue,"ServerIP",&pSize);}
strServerIP=strValue;
if(m_Key.QueryValue(strValue,"ServerPort",&pSize)!=ERROR_SUCCESS)
{m_Key.QueryValue(strValue,"ServerPort",&pSize);}
strServerPort=strValue;
if(m_Key.QueryValue(strValue,"LoginUserName",&pSize)!=ERROR_SUCCESS)
{m_Key.QueryValue(strValue,"LoginUserName",&pSize);}
strLoginUserName=strValue;
if(m_Key.QueryValue(strValue,"LoginPassword",&pSize)!=ERROR_SUCCESS)
{m_Key.QueryValue(strValue,"LoginPassword",&pSize);}
strLoginPassword=strValue;
if(m_Key.QueryValue(strValue,"LoginAddress",&pSize)!=ERROR_SUCCESS)
{m_Key.QueryValue(strValue,"LoginAddress",&pSize);}
strLoginAddress=strValue;
if(m_Key.QueryValue(strValue,"LoginDataAddress",&pSize)!=ERROR_SUCCESS)
{m_Key.QueryValue(strValue,"LoginDataAddress",&pSize);}
strLoginDataAddress=strValue;
}
m_Key.Close();
//
int iAmount=0;
DelTempFiles();
ClearHistory();
CInternetSession m_Session("DigitalTitan");
CHttpConnection* pServer=NULL;
CHttpFile* pFile=NULL;
CHttpFile* pTaskFile=NULL;
CString strServerName=strServerIP;
INTERNET_PORT nPort=(INTERNET_PORT)atoi(strServerPort);
CString strURL=strLoginAddress;
try
{
pServer=m_Session.GetHttpConnection(strServerName,nPort);
pFile=pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,strURL,NULL,1,NULL,NULL,INTERNET_FLAG_EXISTING_CONNECT);
CString strHeaders=_T("Content-Type: application/x-www-form-urlencoded");
CString strFormData;
strFormData.Format("UserName=%s&Password=%s&action=submit",strLoginUserName,strLoginPassword);
if(pFile->SendRequest(strHeaders,(LPVOID)(LPCTSTR)strFormData,strFormData.GetLength())==0)
{
//AfxMessageBox("网络异常...",MB_ICONINFORMATION);
pFile->Close();
delete pFile;
pServer->Close();
delete pServer;
m_Session.Close();
}
else
{
pFile->Close();
delete pFile;
pServer->Close();
delete pServer;
pTaskFile=(CHttpFile*)m_Session.OpenURL(strLoginDataAddress);
CString strBuf;
CString strText;
while(pTaskFile->ReadString(strText))
{
strBuf+="\r\n";
strBuf+=strText;
}
pTaskFile->Close();
delete pTaskFile;
m_Session.Close();
//AfxMessageBox(strBuf,MB_ICONINFORMATION);
int iBegin,iEnd;
iBegin=0;iEnd=0;
CString strTemp;
strTemp="";
iBegin=strBuf.Find("待办方案",0);
if(iBegin!=-1)
{
iEnd=strBuf.Find("更多方案",iBegin);
strTemp=strBuf.Mid(iBegin+9,iEnd-(iBegin+9));
int iResult=strTemp.Find("<td",0);
if(iResult==-1)
{
//
}
else if(iResult<strTemp.GetLength())
{
while(iResult!=-1)
{
iAmount++;
iResult=strTemp.Find("<td",iResult+3);
}
iAmount=iAmount-1;
}
}
}
}
catch(CInternetException* e)
{
char strErrorBuf[255];
e->GetErrorMessage(strErrorBuf,255,NULL);
AfxMessageBox(strErrorBuf,MB_ICONINFORMATION);
pFile=NULL;
delete pFile;
pTaskFile=NULL;
delete pTaskFile;
pServer=NULL;
delete pServer;
m_Session.Close();
return 0;
}
return iAmount;
}
CString setformat="<?xml version="1.0" encoding="gbk"?><articlelist><command>1</command><\articlelist>";这样把要发送的xml文件放入字符串中发过去就行了.至于这个http头是不是必须要有呀,没有就会出错.
pFile -> AddRequestHeaders("Content-Type: application/x-www-form-urlencoded");
pFile -> AddRequestHeaders("Accept: */*");
在这也谢谢大家的支持,明天或后天就结贴,
我得这个贴在今天就结了.
http://expert.csdn.net/Expert/topic/2875/2875315.xml?temp=.2171595
本帖地址;
http://expert.csdn.net/Expert/topic/2921/2921732.xml?temp=.2517511