C++ 以POST方式向网页提交数据.有错误。。求大神解救就这点财富值了,见谅。图片是错误。

#include"stdafx.h"#include<iostream>#include<string>#include<afxinet.h>//定义了MFCCInter... #include "stdafx.h"#include <iostream>#include <string>#include <afxinet.h> //定义了MFC CInternetSession类等bool PostHttpPage(const std::string& hostName, const std::string& pathName, const std::string& postData){ using namespace std; CInternetSession session(_T("session"),0,INTERNET_OPEN_TYPE_PRECONFIG,NULL, NULL,INTERNET_FLAG_DONT_CACHE); //设置不缓冲

INTERNET_PORT nPort = 80; DWORD dwRet = 0; CHttpConnection* pServer = session.GetHttpConnection( (LPCTSTR)hostName.c_str(),nPort); CHttpFile* pFile = pServer->OpenRequest((LPCTSTR)CHttpConnection:: HTTP_VERB_POST,(LPCTSTR)pathName.c_str()); CString strHeaders = "Content-Type: application/x-www-form-urlencoded"; //请求头//开始发送请求 pFile->SendRequest(strHeaders,(LPVOID)postData.c_str(), postData.size()); pFile->QueryInfoStatusCode(dwRet); if (dwRet == HTTP_STATUS_OK) { CString result, newline; while(pFile->ReadString(newline)) {//循环读取每行内容 result += newline+"\r\n"; } std::cout<<result<<std::endl;//显示返回内容 } else { return false; } delete pFile; delete pServer; cout<<"成功"<<endl; session.Close(); return true;}int _tmain(int argc, _TCHAR* argv[]){ using namespace std; int a; //向本机Web目录下面的welcome.php页面发送发送 PostHttpPage("localhost","welcome.php","name=rain&age=21"); cin>>a; return 0;}
展开
 我来答
匿名用户
2014-09-03
展开全部

只帮你解决崩溃的问题哈


就说两个问题:

1、VS2005或以上的IDE默认是用UNICODE的,你代码里混用ANSI和UNICODE,又做强制转换,导致了一些参数出错


2、OpenRequest接受两种不同的参数的,既然你用的是HTTP_VERB_POST,为什么要强制转换成LPCTSTR呢,这样会导致它调用的是另外一个函数(C++支持同名函数,不同参数)


bool PostHttpPage(const std::wstring& hostName,
  const std::wstring& pathName,
  const std::wstring& postData)
{
using namespace std;
CInternetSession session(_T("session"),0,INTERNET_OPEN_TYPE_PRECONFIG,NULL,
NULL,INTERNET_FLAG_DONT_CACHE);     //设置不缓冲

INTERNET_PORT nPort = 80;
DWORD dwRet = 0;

CHttpConnection* pServer = session.GetHttpConnection(
(LPCTSTR)hostName.c_str(),nPort);
CHttpFile* pFile = pServer->OpenRequest(CHttpConnection::
HTTP_VERB_POST,(LPCTSTR)pathName.c_str());

CString strHeaders = L"Content-Type: application/x-www-form-urlencoded"; //请求头
//开始发送请求

pFile->SendRequest(strHeaders,(LPVOID)postData.c_str(),
postData.size());
pFile->QueryInfoStatusCode(dwRet);

if (dwRet == HTTP_STATUS_OK)
{
CString result, newline;

while(pFile->ReadString(newline))
{//循环读取每行内容
result += newline+L"\r\n";
}

std::cout<<result<<std::endl;//显示返回内容
}
else
{
return false;
}
delete pFile;
delete pServer;
cout<<"成功"<<endl;

session.Close();

return true;
}

//调用时用下面的代替
PostHttpPage(L"localhost",L"welcome.php",L"name=rain&age=21");
追问
那怎么改呢。我是个新手。这些代码大都是我从网上找的,只明白大概意思。对于这亏我还不熟悉。我把OpenRequest的强制转换去掉后。有这个。    1    IntelliSense: 没有与参数列表匹配的 重载函数 "CHttpConnection::OpenRequest" 实例。还有上面的session.GetHttpConnection也是。我怎么写这两个函数呢,?
追答
我不是贴了改好的代码给你了么……?

VS2005默认用的是UNICODE,即宽字节
歌梦复兴
2014-09-03 · TA获得超过211个赞
知道小有建树答主
回答量:177
采纳率:0%
帮助的人:132万
展开全部
能定位到异常是在哪里抛出的吗?
追问
好像是session.GetHttpConnection      pServer->OpenRequest(CHttpConnection HTTP_VERB_POST,(LPCTSTR)pathName.c_str());不对。您能看着改改么。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式