5

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?

fex
  • 3,488
  • 5
  • 30
  • 46
  • 5
    well i found a solution : P i had to call wxHTTP::Initialize() in main thread (i've done it in wxApp:onInit()) . More info can be found [here](http://www.litwindow.com/Knowhow/wxSocket/wxsocket.html) ( it's explained on wxSocket but it's same for wxHTTP ) – fex Apr 08 '12 at 22:54
  • While this is a few years old, it would be great if you could post an answer on how you solved this and then accept it. – Taryn Mar 10 '14 at 19:15
  • answer added feel free to ask – Stefano Mtangoo Sep 29 '14 at 07:39

2 Answers2

3

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

Stefano Mtangoo
  • 6,017
  • 6
  • 47
  • 93
1

Call

wxSocketBase::Initialize();

in your apps OnInit function and wxurl/wxhttp functions should work from threads.

Silver Moon
  • 1,086
  • 14
  • 19