File: httpclient.cc

package info (click to toggle)
gpsshogi 0.7.0-3.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 111,280 kB
  • sloc: cpp: 80,962; perl: 12,610; ruby: 3,929; javascript: 1,631; makefile: 1,202; sh: 473; tcl: 166; ansic: 67
file content (37 lines) | stat: -rw-r--r-- 980 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "httpclient.h"
#ifndef MINIMAL_GPSSHELL
#  include "Poco/URIStreamOpener.h"
#  include "Poco/StreamCopier.h"
#  include "Poco/URI.h"
#  include "Poco/Exception.h"
#  include "Poco/Net/HTTPStreamFactory.h"
#endif
#include <memory>
#include <iostream>
#include <fstream>
#include <string>

int gpsshell::getFileOverHttp(const std::string& url, const std::string& tempfile_name)
{
#ifndef MINIMAL_GPSSHELL
  try
  {
    std::ofstream localFile(tempfile_name.c_str(), std::ios_base::out | std::ios_base::binary);
    if (!localFile)
    {
    	std::cerr << "Failed to open local file for writing: " << tempfile_name << std::endl;
    	return 1;
    }
    Poco::URI uri(url);
    std::unique_ptr<std::istream> ptrHttpStream(Poco::URIStreamOpener::defaultOpener().open(uri));
    Poco::StreamCopier::copyStream(*ptrHttpStream.get(), localFile);
  }
  catch (Poco::Exception& exc)
  {
    std::cerr << exc.displayText() << std::endl;
    return 1;    
  }
#endif
  return 0;
}