5,695,118 members and growing! (16,340 online)
Email Password   helpLost your password?
General Programming » Internet / Network » HTTP     Intermediate License: The GNU Lesser General Public License

A Simple Windows HTTP Wrapper Using C++

By shicheng

Gets the content of a web page into a project without the WinHttp APIs mixed in my source code using a WinHttp wrapper.
C++, Windows, ATL, COM, Dev

Posted: 31 Jul 2008
Updated: 31 Jul 2008
Views: 3,160
Bookmarked: 14 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
4 votes for this Article.
Popularity: 1.40 Rating: 2.33 out of 5
1 vote, 25.0%
1
1 vote, 25.0%
2
1 vote, 25.0%
3
0 votes, 0.0%
4
1 vote, 25.0%
5

Introduction

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 the Code

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();

Additional

There are several things that may interest you.

  1. The method 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.
  2. You can parse and enumerate the URLs in the string returned by the method 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.
  3. URLs with default index pages can be handled well. Fox example:
    • http://www.codeproject.com/
    • http://www.codeproject.com/Zones/IBM/
    • http://www.codeproject.com/info/submit.aspx
  4. I don’t really know how the HTTP verb affects the result. So a user can select use the GET or POST verb to send the HTTP request through the parameter of SendHttpRequest. The default verb is GET.

Points of Interest

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.

Finally

This is my first time writing a technical article. I hope I explained everything clearly.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License

About the Author

shicheng


Cheng Shi is a software developer in China. He is interested in COM, ATL, Direct3D, etc. He is now working for Sonic Solutions.

Cheng Shi loves Formula1 and watchs every Grand Prix. He is dreaming to be a racing car driver. Hope his dream can come true.
Occupation: Software Developer
Company: N/A
Location: China China

Other popular Internet / Network articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 31 Jul 2008
Editor: Sean Ewington
Copyright 2008 by shicheng
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project