使用iXMLHttpRequest2下载zip文件

时间:2013-08-26 09:32:30

标签: visual-c++ windows-phone-8

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

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

void 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));

}

void HTTPRequest :: onHttpRequestCompleted(concurrency :: task httpRequest) {

httpRequest.then([this](task previousTask)     {         尝试         {             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());

}

0 个答案:

没有答案