2 diao1057 diao1057 于 2017.09.09 16:19 提问

cocos2dx 3.10 c++ 使用http网络请求,在 1C

下面就是源码
HttpRequest* request = new HttpRequest();//创建请求对象
std::string str1 = "127.0.0.1:80/index.php?";
std::string str2 = p_User_EditBox->getText();
std::string str3 = p_Psw_EditBox->getText();
std::string struser = "username=";
std::string strpsw = "&password=";
str1 = str1 + struser + str2 + strpsw + str3;

    request->setUrl(str1.c_str());//设置请求的url,username和password已经包含在url中
    request->setRequestType(HttpRequest::Type::GET);//设置为Get模式
    request->setResponseCallback(this, httpresponse_selector(HelloWorld::onHttpRequestCompleted));//设置响应的回调

    request->setTag("GET test");
    HttpClient::getInstance()->send(request);//发送请求
    request->release();//释放请求


////////////////////////////////////////////////////在回调函数里面的内容
if (!response->isSucceed())//如果响应失败,输出错误信息
{
    CCString strError;
    strError.initWithFormat("Receive Error! \n%s\n", response->getErrorBuffer());
    m_labelStatusCode->setString(strError.getCString());
    return;
}

std::vector<char> *buffer = response->getResponseData();//接收响应信息
std::string recieveData;
for (unsigned int i = 0; i < buffer->size(); i++)
{
    recieveData += (*buffer)[i];
}
size_t begin = recieveData.find("<body>") + 6;//这里简单处理,获取<body>标签内数据,即是响应内容  +6是从<body>后开始截取
size_t end = recieveData.find("</body>");
std::string result(recieveData, begin, end - begin);
m_labelStatusCode->setString(result.c_str());
运行win32项目![图片说明](http://img.ask.csdn.net/upload/201709/09/1504945082_543483.png)
在模拟器上运行![图片说明](http://img.ask.csdn.net/upload/201709/09/1504945109_345208.png)

3个回答

diao1057
diao1057   2017.09.09 16:21

win32运行的图片说明

diao1057
diao1057   2017.09.09 16:21

模拟器上运行的图片说明

diao1057
diao1057   2017.09.10 20:13

额 解决了,url没加http://

Csdn user default icon
上传中...
上传图片
插入图片