void RouteRequestManager::setRequest(Request* req,bool addToResults_){
	addToResults= addToResults_;
	__pRequest = req;
	String hostAddr = L"http://api.irail.be";

	result r = E_SUCCESS;
	__pSession = new HttpSession();
	r = __pSession->Construct(NET_HTTP_SESSION_MODE_NORMAL, null ,hostAddr,null);
	if (IsFailed(r))
		AppLogDebug("Construct Session failed. (%s)\n", GetErrorMessage(r));


	__pTransaction = __pSession->OpenTransactionN();
	if (null == __pTransaction)
		AppLogDebug("Construct Session failed. \n");

	r = __pTransaction->AddHttpTransactionListener(*this);
	if (IsFailed(r))
		AppLogDebug("AddHttpTransactionListener Session failed.\n");

	__pHttpRequest = __pTransaction->GetRequest();
	if(__pHttpRequest == null)
		AppLogDebug("GetRequest failed. \n");

	String hostAddr2(L"http://api.irail.be/connections/?to=");
	hostAddr2.Append(*req->getToStation()->getNameN());
	hostAddr2.Append(L"&from=");
	hostAddr2.Append(*req->getFromStation()->getNameN());
	hostAddr2.Replace(L" ","-");
	hostAddr2.Append(L"&typeOfTransport=train");
	DateTime* dt = req->getDateTime();
	if(dt){
		//LocaleManager localeManager;
		//localeManager.Construct();
		//TimeZone timeZone = localeManager.GetSystemTimeZone();
		//DateTime utcDateTime = timeZone.WallTimeToUtcTime(*req->getDateTime());
		hostAddr2.Append(L"&date=");
		if(dt->GetDay()<10)
			hostAddr2.Append(L"0");
		hostAddr2.Append(Integer::ToString(dt->GetDay()));
		if(dt->GetMonth()<10)
			hostAddr2.Append(L"0");
		hostAddr2.Append(Integer::ToString(dt->GetMonth()));
		String year;
		Integer::ToString(dt->GetYear()).SubString(2,year);
		hostAddr2.Append(year);

		hostAddr2.Append(L"&time=");
		if(dt->GetHour()<10)
			hostAddr2.Append(L"0");
		hostAddr2.Append(Integer::ToString(dt->GetHour()));
		if(dt->GetMinute()<10)
			hostAddr2.Append(L"0");
		hostAddr2.Append(Integer::ToString(dt->GetMinute()));
	}

	//AppLog("request link: %S",hostAddr2.GetPointer());

	r = __pHttpRequest->SetUri(hostAddr2);
	if(IsFailed(r))
		AppLogDebug("SetUri failed. (%s)\n", GetErrorMessage(r));

	r = __pHttpRequest->SetMethod(NET_HTTP_METHOD_GET);
	if(IsFailed(r))
		AppLogDebug("SetMethod failed. (%s)\n", GetErrorMessage(r));

	// Header Setting
	HttpHeader* pHttpHeader = null;
	pHttpHeader = __pHttpRequest->GetHeader();
	if (null == pHttpHeader){
		r = GetLastResult();
		AppLogDebug("Uri Setting failed. (%s)\n", GetErrorMessage(r));
		r = pHttpHeader->AddField(L"Content-Length", L"1024");
		if (IsFailed(r))AppLogDebug("Header Setting failed. (%s)\n", GetErrorMessage(r));
	}

	r = __pTransaction->Submit();
	if(IsFailed(r))
		AppLogDebug("Submit failed. (%s)\n", GetErrorMessage(r));
	//AppLog("request sended");

	//TODO delete __pHttpRequest
	//TODO delete pHttpHeader
}
Exemple #2
0
HttpThread::HttpThread(std::string const & url,
                       downloader::IHttpThreadCallback & callback,
                       int64_t beg,
                       int64_t end,
                       int64_t size,
                       string const & pb)
  : m_callback(callback),
    m_begRange(beg), m_endRange(end),
    m_downloadedBytes(0), m_expectedSize(size),
    m_url(url), m_pb(pb),
    m_pSession(0),
    m_pTransaction(0)
{
  result r = E_SUCCESS;

  String * pProxyAddr = 0;
  String hostAddr(m_url.c_str());
  HttpHeader * header = 0;

  LOG(LDEBUG, ("Creating HttpSession", m_url));
  m_pSession = new HttpSession();
  r = m_pSession->Construct(NET_HTTP_SESSION_MODE_NORMAL, pProxyAddr, hostAddr, header);
  if (r != E_SUCCESS)
  {
    LOG(LERROR, ("HttpSession Construction error:", r));
    return;
  }

  // Open a new HttpTransaction.
  m_pTransaction = m_pSession->OpenTransactionN();
  if ((r = GetLastResult()) != E_SUCCESS)
  {
    LOG(LERROR, ("OpenTransactionN", GetLastResult()));
    return;
  }

  m_pTransaction->AddHttpTransactionListener(*this);
  m_pTransaction->SetHttpProgressListener(*this);

  HttpRequest * pRequest = m_pTransaction->GetRequest();
  pRequest->SetUri(m_url.c_str());

  HttpHeader * pHeader = pRequest->GetHeader();

  // use Range header only if we don't download whole file from start
  if (!(m_begRange == 0 && m_endRange < 0))
  {
    if (m_endRange > 0)
    {
      LOG(LDEBUG, (m_url, "downloading range [", m_begRange, ",", m_endRange, "]"));
      String range("bytes=");
      range.Append(m_begRange);
      range.Append('-');
      range.Append(m_endRange);
      pHeader->AddField(L"Range", range);
    }
    else
    {
      LOG(LDEBUG, (m_url, "resuming download from position", m_begRange));
      String range("bytes=");
      range.Append(m_begRange);
      range.Append('-');
      pHeader->AddField("Range", range);
    }
  }

  // set user-agent with unique client id only for mapswithme requests
  if (m_url.find("mapswithme.com") != string::npos)
  {
    static string const uid = GetPlatform().UniqueClientId();
    pHeader->AddField("User-Agent", uid.c_str());
  }

  if (m_pb.empty())
  {
    pRequest->SetMethod(NET_HTTP_METHOD_GET);
  }
  else
  {
    pRequest->SetMethod(NET_HTTP_METHOD_POST);
    pHeader->AddField("Content-Type", "application/json");
    int64_t const sz = m_pb.size();
    String length;
    length.Append(sz);
    pHeader->AddField("Content-Length", length);
    ByteBuffer body;
    body.Construct((const byte *)m_pb.c_str(), 0, sz, sz);
    pRequest->WriteBody(body);
  }
  LOG(LDEBUG, ("Connecting to", m_url, "[", m_begRange, ",", m_endRange, "]", "size=", m_expectedSize));
  m_pTransaction->Submit();
}