Recently I needed to get the content of a web page into a project and I didn't want to have WinHttp APIs mixed in my source code so I created a simple WinHttp wrapper named WinHttpClient.
WinHttpClient takes a URL as a parameter, sends the HTTP request and gets the response. The method that sends the HTTP request is a synchronous method so it should NOT be called in the UI thread. WinHttpClient is thread safe which means it can be used in many threads at one time.
Using WinHttpClient is pretty simple. The class diagram is as follows.
For example, the code snap to get the content of http://www.codeproject.com/ is as follows.
WinHttpClient client(L"http://www.codeproject.com/");
client.SendHttpRequest();
wstring httpResponseHeader = client.GetHttpResponseHeader();
wstring httpResponse = client.GetHttpResponse();
There are several things that may interest you.
SendHttpRequest is a synchronous method and it will retry several times until it succeeds which causes this method to take a long time to finish. So do not use this in a UI thread. It is recommended to create some worker threads and use WinHttpClient in them since WinHttpClient is a thread safe class.GetHttpResponse and get the contents of the them and do it over and over again. Then you will get the contents of a whole website.SendHttpRequest. The default verb is GET.When using MultiByteToWideChar to convert to the wide characters, deleting the buffer I created to hold the output wide leads to a crash if I set the last character to 0. It is strange and I didn't figure it out.
This is my first time writing a technical article. I hope I explained everything clearly.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||