示例#1
0
void GD_API SendHttpRequest(const gd::String & host, const gd::String & uri, const gd::String & body,
    const gd::String & method, const gd::String & contentType, gd::Variable & responseVar)
{
    // Separate the host and the port number
    auto hostInfo = host.Split(U':');

    if(hostInfo.size() < 2)
        return; //Invalid address (there should be two elements: "http" and "//the.domain.com")

    // Create Http
    sf::Http http;
    http.setHost(hostInfo[0].ToUTF8() + ":" + hostInfo[1].ToUTF8(), hostInfo.size() > 2 ? hostInfo[2].To<unsigned short>() : 0);

    // Create request
    sf::Http::Request request;
    request.setMethod(method == "POST" ? sf::Http::Request::Post : sf::Http::Request::Get);
    request.setField("Content-Type", contentType.empty() ? "application/x-www-form-urlencoded" : contentType.ToUTF8());
    request.setUri(uri.ToUTF8());
    request.setBody(body.ToUTF8());

    // Send request & Get response
    sf::Http::Response response = http.sendRequest(request);

    if (response.getStatus() == sf::Http::Response::Ok)
    {
        responseVar.SetString(gd::String::FromUTF8(response.getBody()));
    }
    //else request failed.
}
示例#2
0
void GD_API DownloadFile( const gd::String & host, const gd::String & uri, const gd::String & outputfilename )
{
    // Separate the host and the port number
    auto hostInfo = host.Split(U':');

    if(hostInfo.size() < 2)
        return; //Invalid address (there should be two elements: "http" and "//the.domain.com")

    // Create Http
    sf::Http http;
    http.setHost(hostInfo[0].ToUTF8() + ":" + hostInfo[1].ToUTF8(), hostInfo.size() > 2 ? hostInfo[2].To<unsigned short>() : 0);

    // Create request
    sf::Http::Request Request;
    Request.setMethod(sf::Http::Request::Get);
    Request.setUri(uri.ToUTF8());

    // Send request & Get response
    sf::Http::Response datas = http.sendRequest(Request);

    ofstream ofile(outputfilename.ToLocale().c_str(), ios_base::binary);
    if ( ofile.is_open() )
    {
        ofile.write(datas.getBody().c_str(),datas.getBody().size());
        ofile.close();

        return;
    }

    cout << "Downloading file : Unable to open output file " << outputfilename;
    return;
}
bool NativeFileSystem::WriteToFile(const gd::String & filename, const gd::String & content)
{
    std::ofstream file( filename.ToLocale().c_str() );
    if ( file.is_open() ) {
        file << content.ToUTF8();
        file.close();
        return true;
    }

    return false;
}