如何用VC++中创建一个URL链接,并通过此链接向服务器发送信息,再取得返回信息?谢谢

bbjj 2001-11-28 10:16:54
...全文
291 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
bbjj 2001-12-07
  • 打赏
  • 举报
回复
给分了
bbjj 2001-11-30
  • 打赏
  • 举报
回复
就象用“+”代替参数中的空格,用“%2b”代替参数中的“+”,类似于java中的URLENcoder.encode(strName);
heilong 2001-11-30
  • 打赏
  • 举报
回复
你说的那种编码,是针对非字符的,即如果你要上传的name是汉字,则字串应该是%第一个字节的ascii码%第二个字节的ascii码.....,你只要取得你的非字符的字串每个字节的ascii码,前面加上%即可
bbjj 2001-11-30
  • 打赏
  • 举报
回复
如何对参数进行URL编码,得到行例如id=1&name=2&pw=3%2b 的格式
heilong 2001-11-29
  • 打赏
  • 举报
回复
还是贴出来吧,一个一个发可发不过来,函数如下,调用方法嘛,
例:HttpSend("www.csdn.net","test.asp?","id=233&name=heilong",TRUE);
最後一个参数IsAuto是我程序重要用到的,对大家来说没什么大用,大家可以去掉,主要是看看用mfc的Wininet类建立一个internet程序的步骤,这些msdn中都有,不过写这个函数可花了我一天的时间,没办法,偶以前没做过,现在共享以下,欢迎大家指教,我的email是qincunxiu@263.net
/////////////////////////////////以下为函数源码
BOOL CDicosDlg::HttpSend(CString strSever, CString Object,CString strValue,BOOL bIsAuto)
{
//CString temp = Object + strValue;
//temp = strSever + temp;
//MessageBox(temp);
CWnd* butTran = GetDlgItem (IDC_BUTTON_TRANSCOM);
if (bIsAuto)//说明正在Ontimer中直接调用该函数,应该屏蔽掉其他的发送
{
butTran->EnableWindow (FALSE);
bIsTranscom = TRUE;
}
CHttpConnection* m_Connection = NULL;
CHttpFile* m_File = NULL;
CString m_Ret;//用来接收返回信息
CInternetSession m_Session(AfxGetAppName (),0,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
TRY
{
//DWORD dwValue = 0;
//m_Session.QueryOption (INTERNET_OPTION_CONNECT_RETRIES,dwValue);
m_Session.SetOption (INTERNET_OPTION_CONNECT_RETRIES,1);
m_Session.SetOption (INTERNET_OPTION_CONNECT_TIMEOUT,20000);
m_Session.SetOption (INTERNET_OPTION_RECEIVE_TIMEOUT,20000);
INTERNET_PORT nPort = 80;
CString strServer = strSever;
m_Connection = m_Session.GetHttpConnection(strServer,nPort,NULL,NULL );
if (m_Connection == NULL)
{
DisplayStat ("An error occurred connecting to the server");
Sleep (2000);
goto end;
}
LPCTSTR ppszAcceptTypes[2];
ppszAcceptTypes[0] = _T("*/*");
ppszAcceptTypes[1] = NULL;
CString strObject = Object;
strObject = Object + strValue;
m_File = m_Connection->OpenRequest(NULL, strObject, NULL,1,ppszAcceptTypes, NULL, INTERNET_FLAG_RELOAD |
INTERNET_FLAG_DONT_CACHE);
if (m_File == NULL)
{
DisplayStat ("打开服务器上的文件出错");
/*CStdioFile debugFile;
debugFile.Open ("debug.txt",CFile::modeWrite|CFile::modeCreate|CFile::modeNoTruncate|CFile::shareDenyWrite|CFile::typeText);
debugFile.SeekToEnd ();
CString strInf("打开服务器上的文件出错");
strInf = Object + strInf + "\n";
debugFile.Write (strInf.GetBuffer (strInf.GetLength ()),strInf.GetLength ());
strInf.ReleaseBuffer ();*/
goto end;
}
if (!m_File->SendRequest (NULL,0,NULL,0))
{
DisplayStat ("An error occurred connecting to the server");
Sleep (2000);
goto end;
}
TCHAR szStatusCode[32];
DWORD dwInfoSize = 32;
if (m_File->QueryInfo (HTTP_QUERY_STATUS_CODE,szStatusCode,&dwInfoSize))
{
long nStatusCode = _ttol(szStatusCode);

//Handle any authentication errors
if (nStatusCode == HTTP_STATUS_REQUEST_TIMEOUT)
{
DisplayStat ("连接超时,请重试");
/*CStdioFile debugFile;
debugFile.Open ("debug.txt",CFile::modeWrite|CFile::modeCreate|CFile::modeNoTruncate|CFile::shareDenyWrite|CFile::typeText);
debugFile.SeekToEnd ();
CString strInf ("连接超时,请重试");
strInf = Object + strInf + "\n";
debugFile.Write (strInf.GetBuffer (strInf.GetLength ()),strInf.GetLength ());
strInf.ReleaseBuffer ();*/
goto end;
// We have to read all outstanding data on the Internet handle
// before we can resubmit request. Just discard the data.
}
else if (nStatusCode != HTTP_STATUS_OK)
{
DisplayStat("服务器端错误");
/*CStdioFile debugFile;
debugFile.Open ("debug.txt",CFile::modeWrite|CFile::modeCreate|CFile::modeNoTruncate|CFile::shareDenyWrite|CFile::typeText);
debugFile.SeekToEnd ();
CString strInf("服务器端错误");
strInf = Object + strInf + szStatusCode + "\n";
debugFile.Write (strInf.GetBuffer (strInf.GetLength ()),strInf.GetLength ());
strInf.ReleaseBuffer ();*/
goto end;
}
//状态正确,开始读取信息
}
else
{
DisplayStat("无法取得服务器返回信息");
/*CStdioFile debugFile;
debugFile.Open ("debug.txt",CFile::modeWrite|CFile::modeCreate|CFile::modeNoTruncate|CFile::shareDenyWrite|CFile::typeText);
debugFile.SeekToEnd ();
CString strInf("无法取得服务器返回信息");
strInf = Object + strInf + "\n";
debugFile.Write (strInf.GetBuffer (strInf.GetLength ()),strInf.GetLength ());
strInf.ReleaseBuffer ();*/
goto end;
}
char szReadBuf[1024];
DWORD dwBytesToRead = 1024;
DWORD m_ActualSize = 0;
do
{
ZeroMemory (szReadBuf,1024);
DWORD m_ActualSize = m_File->Read (szReadBuf,dwBytesToRead);
m_Ret = m_Ret + szReadBuf;
}while(!(m_ActualSize < dwBytesToRead));
TRACE("%s",m_Ret);
int iFirst = m_Ret.Find ("<返回值>",0);
CString strTemp = m_Ret.Right (m_Ret.GetLength () - iFirst - 8);
int iLast = strTemp.Find ("</返回值>",0);
strTemp = strTemp.Left (iLast);
if (strTemp == "操作成功")
{
DisplayStat("数据上传成功");
/*CStdioFile debugFile;
debugFile.Open ("debug.txt",CFile::modeWrite|CFile::modeCreate|CFile::modeNoTruncate|CFile::shareDenyWrite|CFile::typeText);
debugFile.SeekToEnd ();
CString strInf ("数据上传成功");
strInf = Object + strInf + "\n";
debugFile.Write (strInf.GetBuffer (strInf.GetLength ()),strInf.GetLength ());
strInf.ReleaseBuffer ();*/
if (m_File)
{
m_File->Close ();
delete m_File;
}
if (m_Connection)
{
m_Connection->Close ();
delete m_Connection;
}
m_Session.Close ();
if (bIsAuto)
{
butTran->EnableWindow ();
bIsTranscom = FALSE;
}
DisplayStat ("空闲。。。");
return TRUE;
}
else
{
DisplayStat("数据上传失败,请稍候重试");
/*CStdioFile debugFile;
debugFile.Open ("debug.txt",CFile::modeWrite|CFile::modeCreate|CFile::modeNoTruncate|CFile::shareDenyWrite|CFile::typeText);
debugFile.SeekToEnd ();
CString strInf ("数据上传失败,请稍候重试");
strInf = Object + strInf + "\n";
debugFile.Write (strInf.GetBuffer (strInf.GetLength ()),strInf.GetLength ());
strInf.ReleaseBuffer ();*/
goto end;
}
//int i = m_Content.Replace ("\r\n","\n");
}
CATCH (CInternetException, e)
{
char errStr[100];
e->GetErrorMessage (errStr,100);
DisplayStat (errStr);
/*CStdioFile debugFile;
debugFile.Open ("debug.txt",CFile::modeWrite|CFile::modeCreate|CFile::modeNoTruncate|CFile::shareDenyWrite|CFile::typeText);
debugFile.SeekToEnd ();
CString strInf("InternetException");
strInf = strInf + errStr + "\n";
debugFile.Write (strInf.GetBuffer (strInf.GetLength ()),strInf.GetLength ());
strInf.ReleaseBuffer ();*/
if (m_File)
{
m_File->Close ();
delete m_File;
m_File = NULL;
}
if (m_Connection)
{
m_Connection->Close ();
delete m_Connection;
m_Connection = NULL;
}
m_Session.Close ();
if (bIsAuto)
{
butTran->EnableWindow ();
bIsTranscom = FALSE;
}
DisplayStat ("空闲。。。");
return FALSE;
//return FALSE;
}
//CATCH_ALL (e)
//{
//}
END_CATCH
end:
if (m_File)
{
m_File->Close ();
delete m_File;
m_File = NULL;
}
if (m_Connection)
{
m_Connection->Close ();
delete m_Connection;
m_Connection = NULL;
}
m_Session.Close ();
if (bIsAuto)
{
butTran->EnableWindow ();
bIsTranscom = FALSE;
}
DisplayStat ("空闲。。。");
return FALSE;
}
bbjj 2001-11-29
  • 打赏
  • 举报
回复
如何把串id=1&name=2&pw=3 发送到URL
Novelty 2001-11-28
  • 打赏
  • 举报
回复
找本<<Visual C++网络高级编程>>看看就知道啦.

记住是机械出版社的.
heilong 2001-11-28
  • 打赏
  • 举报
回复
用http协议吗?我这有用http协议访问网页的源程序,
bbjj 2001-11-28
  • 打赏
  • 举报
回复
up一下
unix123 2001-11-28
  • 打赏
  • 举报
回复
CInternetSession可以实现,挺好用的

CInternetSession session;
CInternetFile * file = NULL;

try{
file = (CInternetFile *)session.OpenURL (mReqStr,1, INTERNET_FLAG_TRANSFER_ASCII | INTERNET_FLAG_RELOAD);
}
catch(CInternetException * pEx){
file = NULL;
pEx->Delete ();
}
if (file){
for (int i=0;i<30 && file->ReadString(line);i++)
Rstmp = Rstmp + line; file->Close();
delete file;
}
else{ }
bbjj 2001-11-28
  • 打赏
  • 举报
回复
能详细讲讲吗?或mailto: sjb11@sina.com.cn 谢谢
BABIZHU 2001-11-28
  • 打赏
  • 举报
回复
很简单啊,用winet就ok了,我做过一个通过不断提交form数据,破戒邮件密码的程序
bbjj 2001-11-28
  • 打赏
  • 举报
回复
建立URL连接后,POST串如:id=1&name=2&pw=3,然后取得从服务器返回的信息
bbjj 2001-11-28
  • 打赏
  • 举报
回复
java下已经实现了,vc++下如何通过URL向服务器post数据,并取得返回信息
killingfield 2001-11-28
  • 打赏
  • 举报
回复
也给我一份吧,先谢了!
xiaohuimr@etang.com
bbjj 2001-11-28
  • 打赏
  • 举报
回复
我的mail:
sjb11@sina.com.cn
谢谢
ILMLCZH 2001-11-28
  • 打赏
  • 举报
回复
to:heilong()
thanks!
ilml@elong.com

16,550

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧