读书人

VC用Post提交表单的出现错误

发布时间: 2012-05-29 12:16:00 作者: rapoo

VC用Post提交表单的出现异常?
我尝试实现一个VC自动提交表单的功能,因为是在内网,所以无法提供网址测试。主要代码如下

C/C++ code
void CMyIpDlg::OnButton1() {    // TODO: Add your control notification handler code here         //获取本机IP    WORD wVersionRequested;    WSADATA wsaData;    char name[255];    CString ip;    PHOSTENT hostinfo;    wVersionRequested = MAKEWORD( 2, 0 );        if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )    {                if( gethostname ( name, sizeof(name)) == 0)        {            if((hostinfo = gethostbyname(name)) != NULL)            {                ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);            }        }                WSACleanup( );    }    CInternetSession m_InetSession(_T("MySession"),0,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,INTERNET_FLAG_DONT_CACHE);    CHttpConnection *pServer = NULL;    CHttpFile *pFile = NULL;        CString strHtml = "";    CString strUrl = "";    CString strRequest = "userid=********&passwd=*********";    CString strHeader =  "POST /portalAuthAction.do HTTP/1.1/r/n";            strHeader += "Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg,";            strHeader += "application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*/r/n";            strHeader += "Referer: http://219.136.125.131/portalReceiveAction.do?wlanacname=gdcc-a&wlanuserip=";            strHeader += ip;            strHeader += "&wlanacip=183.56.21.196/r/n";            strHeader += "Accept-Language: zh-CN/r/n";            strHeader += "Content-Type: application/x-www-form-urlencoded/r/n";            strHeader += "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0;GTB6.6; chromeframe/10.0.648.119;";            strHeader += "SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; Tablet PC 2.0; CIBA)/r/n";    try{            strUrl = "http://219.136.125.131/portalReceiveAction.do?wlanacname=gdcc-a&wlanuserip=";            strUrl += ip;            strUrl += "&wlanacip=183.56.21.196";            INTERNET_PORT nPort = 80;            pServer = m_InetSession.GetHttpConnection(strUrl,nPort,NULL,NULL);            pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,"/");            pFile->AddRequestHeaders(strHeader);            Sleep(3000);            pFile->SendRequestEx((DWORD)strRequest.GetLength());                pFile->WriteString(strRequest);            pFile->EndRequest();            DWORD dwRet;            pFile->QueryInfoStatusCode(dwRet);            if(dwRet == HTTP_STATUS_OK)            {                CString strLine;                while (  pFile->ReadString(strLine) > 0)                {                    strHtml += strLine;                }            }            delete pFile;            delete pServer;    }    catch(CInternetException e)    {            e.m_dwContext;        e.Delete();    }}

我测试过如果不要
C/C++ code
pFile->SendRequestEx((DWORD)strRequest.GetLength());                pFile->WriteString(strRequest);            pFile->EndRequest();

这三行代码,是不会抛出异常的。
保留三行代码时,抛出的异常,弹出一个窗口说URL无效。
e.m_dwError的代号为12005,但我找不到这个代号的说明。
希望高手能指点一下。

[解决办法]
改成这样看看
strUrl = "/portalReceiveAction.do?wlanacname=gdcc-a&wlanuserip=";
strUrl += ip;
strUrl += "&wlanacip=183.56.21.196";
INTERNET_PORT nPort = 80;
pServer = m_InetSession.GetHttpConnection("http://219.136.125.131",nPort,NULL,NULL);


pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,strUrl);

[解决办法]
回车是\r\n
不是/r/n

读书人网 >VC/MFC

热点推荐