1

我正在尝试为 Windows phone 8 移植 cocos2dx 应用程序。我正在尝试使用 iXMLHTTPRequest 类在 C++ 中执行网络调用。

我正在尝试使用此下载 zip 文件,但不知道我做错了什么以及在哪里做错了。这是我正在使用的代码,请帮助我找出问题以及我应该怎么做才能使其正常工作。

无效的 HTTPRequest::sendRequest(){

m_cancelHttpRequestSource = cancellation_token_source();
// Set up the GET request parameters.
std::string s_str = std::string(urlString);
std::wstring wid_str = std::wstring(s_str.begin(), s_str.end());
const wchar_t* w_char = wid_str.c_str();
auto uri = ref new Uri( ref new String(w_char));
String ^temp = uri->AbsoluteUri;
auto token = m_cancelHttpRequestSource.get_token();
// Send the request and then update the UI.
onHttpRequestCompleted(m_httpRequest.GetAsync(uri, token));

}

无效 HTTPRequest::onHttpRequestCompleted(并发::task httpRequest) {

httpRequest.then([this](task previousTask) { try { wstring response = previousTask.get(); if (m_httpRequest.GetStatusCode() == 200) { size_t strSize; FILE* fileHandle;

            auto local = Windows::Storage::ApplicationData::Current->LocalFolder;
            auto localFileNamePlatformString = local->Path + "\\test1.zip";

            // Create an the xml file in text and Unicode encoding mode.
            if ((fileHandle = _wfopen(localFileNamePlatformString->Data(), L"wb")) == NULL) // C4996
                // Note: _wfopen is deprecated; consider using _wfopen_s instead
            {
                wprintf(L"_wfopen failed!\n");
                return(0);
            }

            // Write a string into the file.
            strSize = wcslen(response.c_str());
            if (fwrite(response.c_str(), sizeof(wchar_t), strSize, fileHandle) != strSize)
            {
                wprintf(L"fwrite failed!\n");
            }

            // Close the file.
            if (fclose(fileHandle))
            {
                wprintf(L"fclose failed!\n");
            }
        }
        else
        {
            // The request failed. Show the status code and reason.
            wstringstream ss;
            ss << L"The server returned "
               << m_httpRequest.GetStatusCode()
               << L" ("
               << m_httpRequest.GetReasonPhrase()
               << L')';
            //String ^responseText = ref new String(ss.str().c_str());
            m_delegate->parserError(requestType->getCString(), "Print Status Code later");
        }
    }
    catch (const task_canceled&)
    {
        // Indicate that the operation was canceled.
        //String ^responseText = "The operation was canceled";
        m_delegate->parserError(requestType->getCString(), "Operation has canceled");
    }
    catch (Exception^ e)
    {
        // Indicate that the operation failed.
        //String ^responseText = "The operation failed";
        m_delegate->parserError(requestType->getCString(), "The operation failed");
        // TODO: Handle the error further.
        (void)e;
    }

}, task_continuation_context::use_current());

}

4

0 回答 0