Support Board
Date/Time: Wed, 15 Jan 2025 12:43:18 +0000
[Programming Help] - How to repeat HTTP Requests within a study?
View Count: 1077
[2017-08-11 08:59:23] |
Blue Brazilian - Posts: 42 |
I am trying to access data from various pages on a finance web site using the study shown below. The study accepts the URL for a specific page on the web site and then applies some rudimentary parsing of the response to extract price data. This is successful on a single request, and the remote side then gracefully closes the socket. However, if I then enter a new URL to the study, in order to access a new page, it doesn’t repeat the access request. I have tried re-setting the RequestState to REQUEST_NOT_SENT after the first response is received, but it still does not make a new request. Is there a way to execute repeated sc.MakeHTTPRequest calls from within the same study? And if so, could I then simply use a Sleep function to execute the HTTP requests at, say, one hour intervals? #include <iostream> #include <string> #include <stdlib.h> #include <algorithm> #include "sierrachart.h" using namespace std; SCDLLName("htmlext5a") SCSFExport scsf_htmlext5a(SCStudyGraphRef sc) { SCInputRef URLtext = sc.Input[0]; if (sc.SetDefaults) { URLtext.Name = "URL of website"; sc.Input[0].SetString("URL"); sc.FreeDLL = 1; return; } int& RequestState = sc.GetPersistentInt(1); SCString URLinput; URLinput = URLtext.GetString(); enum { REQUEST_NOT_SENT = 0, REQUEST_SENT, REQUEST_RECEIVED }; if (sc.Index == 0) { if (RequestState == REQUEST_NOT_SENT) { // Make a request for a text file on the server. When the request is complete and all of the data //has been downloaded, this study function will be called with the file placed into the sc.HTTPResponse character string array. if (!sc.MakeHTTPRequest(URLinput)) { sc.AddMessageToLog("Error making HTTP request.", 1); // Indicate that the request was not sent. // Therefore, it can be made again when sc.Index equals 0. RequestState = REQUEST_NOT_SENT; } else RequestState = REQUEST_SENT; } } if (RequestState == REQUEST_SENT && sc.HTTPResponse != "") { RequestState = REQUEST_RECEIVED; std::string SResp; SResp = sc.HTTPResponse; int str_len = SResp.size(); std::string TextString; std::string Subtext; Subtext = "alertValue"; if (SResp.find(Subtext) != std::string::npos) { int posn = SResp.find(Subtext); if (str_len == 0) sc.AddMessageToLog("size of str_len is zero", 1); else { SCString Buffer; Buffer = " Length of str is "; SCString Num2Str; Num2Str.Format("%d ", str_len); Buffer += Num2Str; Buffer += " value of posn "; SCString NumStr; NumStr.Format("%d ", posn); Buffer += NumStr; Buffer += "\n"; std::string TextStr = SResp.substr(posn + 11, 30); std::string price; std::string tester = "\""; int findPos = TextStr.find(tester); //finds first quote mark int findPos2 = TextStr.find(tester, findPos + 1); //finds second quote mark price = TextStr.substr(findPos + 1, (findPos2 - findPos - 1)); SCString Buffer1; Buffer1 = "...Price is "; Buffer1 += price.c_str(); price.erase(std::remove(price.begin(), price.end(), ','), price.end()); float pricevalue; pricevalue = strtof(price.c_str(), 0); SCString Buffer2; Buffer2 = " price "; SCString Num4Str; Num4Str.Format("%0.06f ", pricevalue); Buffer2 += Num4Str; Buffer2 += "\n"; sc.AddMessageToLog(Buffer2, 1); } } } else if (RequestState == REQUEST_SENT && sc.HTTPResponse == "") { sc.AddMessageToLog("Request did not complete...", 1); //The request has not completed, therefore there is nothing to do so we will return return; } } |
[2017-08-14 13:44:18] |
Sierra Chart Engineering - Posts: 104368 |
Is there a way to execute repeated sc.MakeHTTPRequest calls from within the same study? You for sure can do this. If this is not happening, then there must not be another call to sc.MakeHTTPRequest. You need to perform step-by-step debugging to see what is going wrong:https://www.sierrachart.com/index.php?page=doc/DebuggingDLLs.html You definitely cannot use Sleep. You just need to keep track of the time that a request was last made, compare it to the current time and make one every hour. You could store that value in a persistent variable. Refer to: sc.GetPersistentInt() Sierra Chart Support - Engineering Level Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy: https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service: Sierra Chart Teton Futures Order Routing Date Time Of Last Edit: 2017-08-14 13:45:18
|
To post a message in this thread, you need to log in with your Sierra Chart account: