I have some problems with using wxHTTP inside a Thread. I have created below class which derive from wxThread to use wxHTTP.
class Thread : public wxThread {
private:
wxHTTP get;
public:
Thread()
{
}
~Thread()
{
}
virtual ExitCode Entry()
{
get.SetHeader(wxT("Content-Type"), wxT("text/html; charset=utf-8"));
get.Connect(wxT("www.mysite.com"));
get.SetTimeout(1);
wxInputStream *httpStream = get.GetInputStream(wxT("/script.php?name=aaa&text=blabla"));
wxDELETE(httpStream);
get.Close();
return 0;
}
};
I create this thread and run it (threads are created, ran and everything is fine with them). Unfortunately wxHTTP seems to doesn't work properly with threads (even my firewall doesn't ask me about connection). Is there any way to create wxHTTP connection inside a thread?
Here is the answer (as requested by @bluefeet) wxHTTP inherits from wxSocketBase and in wxSocketBase we have this quote
When using wxSocket from multiple threads, even implicitly (e.g. by using wxFTP or wxHTTP in another thread) you must initialize the sockets from the main thread by calling Initialize() before creating the other ones.
See here for more explanation
Call
wxSocketBase::Initialize();
in your apps OnInit function and wxurl/wxhttp functions should work from threads.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With