Note that there are some explanatory texts on larger screens.

plurals
  1. POSend CGI request from C++ program?
    primarykey
    data
    text
    <p>I am currently working on integrating a prototype product I made into a larger program. What I did in the prototype was create a HTTP server on a microcontroller, and then wrote some HTML to call .cgi functions on the web-server. This was so I could control I/O on the micro controller via an ethernet. </p> <p>My html seems very straight forward, to call a cgi function on the microcontroller I have made buttons with the following syntax:</p> <pre><code>&lt;script type="text/javascript"&gt; function my_confirm(a){ var r=confirm("Are you sure you'd like to run "+a+"?"); if (r==true) location.href = a; } &lt;/script&gt; &lt;button type = "button" onclick=" my_confirm('started.cgi')" &gt;Started&lt;/button&gt; </code></pre> <p>additionally, if i were to type in "169.254.129.12/started.cgi" into a web browser, the microcontroller would respond appropriately. Now that the prototype is completed ahead of schedule, my new task is to integrate my controls into a C++/CLI program. GREAT. I have never written in C++... </p> <p>Rather than re-doing all of the backend stuff, I would like to add a menu to send similiar requests. Over the last 4-5 days I have gotten a menu in VS2005 and have had limited success, with the code below:</p> <pre><code>int sendHttpRequestAndRespond(System::Uri^ url){ if(url-&gt;Scheme == System::Uri::UriSchemeHttp){ HttpWebRequest^ request = dynamic_cast&lt;HttpWebRequest^&gt;(WebRequest::Create(url)); request-&gt;Method = System::Net::WebRequestMethods::Http::Get; return 0; } //HttpWebResponse^ response = dynamic_cast&lt;HttpWebResponse^&gt; (request-&gt;GetResponse()); //RequestState^ state = gcnew RequestState(); //state-&gt;request = request; //try{ // IAsyncResult^ response = dynamic_cast&lt;IAsyncResult^&gt; (request-&gt;BeginGetResponse(gcnew AsyncCallback( responseReceived),state)); //} //catch(WebException^ e){} else return -1; } </code></pre> <p>With the code written as shown above(with comments present) the I/O control of the microcontroller does not operate properly. Oddly, if I put in the HttpWebResponse line, things (sometimes) go properly, and other times, nothing seems to happen. Lastly, if I use the AsyncResponse, things are similiar to if I use the HttpWebResponse portion.</p> <p>Now my thought/understanding was that merely putting in the request via the GET method I am using in the code shown above would initiate the I/O control on the microcontroller, but it seems the response plays a role in it as well.</p> <p>Do any of you have a better route to go with this? At this point I am highly confused/frustrated and open to any advice! Thank you,</p> <p><strong>EDIT 1: UPDATED CODE:</strong></p> <pre><code>int sendHttpRequestAndRespond(System::Uri^ url){ if(url-&gt;Scheme == System::Uri::UriSchemeHttp){ HttpWebRequest^ request = dynamic_cast&lt;HttpWebRequest^&gt;(WebRequest::Create(url)); request-&gt;Method = System::Net::WebRequestMethods::Http::Get; RequestState^ state = gcnew RequestState(); state-&gt;request = request; try{ IAsyncResult^ response = dynamic_cast&lt;IAsyncResult^&gt; (request-&gt;BeginGetResponse(gcnew AsyncCallback( responseReceived),state)); } catch(WebException^ e){} return 0; } </code></pre> <p>With this code I get into my callback function (responseReceived) the first two times I call the function, but never on the 3rd or further time. I am testing this with <a href="http://www.facebook.com" rel="nofollow">http://www.facebook.com</a>, as this seems to work for my CGI calling, but only the first two times as well. Thanks,</p> <p><strong>EDIT 2: Resolution and new problems!</strong> Okay, I figured out my problem was due mostly to not ending the requests. In the callBack function I wasn't ending the request, and HTTP protocol has a 2 connections/ip restriction, making the first two calls work, but future ones wouldn't. </p> <p>Now my new issue is that on the asyc call I a using, it first does synchronous DNS resolution. Since I am sending these commands to an embedded computer with an IP, there's no DNS inbetween, and the function blocks for a long time. Once my computer realizes there's been a DNS resolution error, it fires off the request any ways, and it works! Subsequent requests work instantly, as there's a cache that remembers the failure. </p> <p>So im about 99% of the way there, but I need to fix this first time lockup some how. My current ideas are:</p> <p>Somehow disable dns resolution before the request</p> <p>Shorten the dns resolution timeout from 60+ seconds to 1ish seconds</p> <p>trick my comptuer/the computer the program runs on into thinking that <a href="http://169.254.129.12" rel="nofollow">http://169.254.129.12</a> has an ip address of 169.254.129.12 (local host file??)</p> <p>I am not sure how to accomplish those, or if there are better ideas??</p> <p>Thank you,</p> <p>Any better ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload