2009-07-14 3 views
11

Win32でネイティブC/C++用の「ハイレベル」HTTPライブラリはありませんか、間違った場所を探していますか?Win32のネイティブC/C++用のハイレベルHTTPクライアントライブラリ

「高レベル」とは、.NETフレームワークとほぼ同じ抽象度のC++でHTTP Webリクエスト/レスポンスを可能にするAPIを意味します(ただし、C++/CLIを使用することはオプションではありません私のために)。

.NETを使用せずにWin32でC/C++でこれを(ほぼ同じ量のコードで)実行する方法は?参考までに、私はC#でどのようにしたらよいかを示すためのコードサンプルが含まれています。

byte[] fileBytes = null; 
bool successfulDownload = false; 
using (WebClient client = new WebClient()) 
{ 
    WebProxy proxy = WebProxy.GetDefaultProxy(); 
    client.Proxy = proxy; 
tryAgain: 
    try 
    { 
     fileBytes = client.DownloadData(fileUrl); 
     successfulDownload = true; 
    } 
    catch (WebException wEx) 
    { 
     if (wEx.Response != null && wEx.Response is HttpWebResponse) 
     { 
      string username = null, password = null; 
      bool userCanceled = false; 
      HttpStatusCode statusCode = ((HttpWebResponse)wEx.Response).StatusCode; 
      switch (statusCode) 
      { 
       case HttpStatusCode.ProxyAuthenticationRequired: 
        // This is just a convenience function defined elsewhere 
        GetAuthenticationCredentials(fileUrl, true, 
         out username, out password, out userCanceled); 
        if (!userCanceled) 
        { 
         client.Proxy.Credentials = new NetworkCredential(username, password); 
         goto tryAgain; 
        } 
        break; 
       case HttpStatusCode.Unauthorized: 
        // This is just a convenience function defined elsewhere 
        GetAuthenticationCredentials(fileUrl, false, 
         out username, out password, out userCanceled); 
        if (!userCanceled) 
        { 
         client.Credentials = new NetworkCredential(username, password); 
         goto tryAgain; 
        } 
        break; 
      } 
     } 
    } 
} 
+0

重複:http://stackoverflow.com/questions/822581/what-c​​-library-for-http-client –

答えて

8

Win32はInternet*の機能を提供します。

http://msdn.microsoft.com/en-us/library/aa385473(VS.85).aspx

あなたは(IIRC、私は10年以上にわたり、これらのAPIを触れていない)InternetOpenURLInternetReadFileを行う必要があります。

+0

これは私がやろうと思っているものに近いものの、それでもかなりはありません。問題は、「エキゾチックな」プロキシシナリオ(自動プロキシ構成スクリプト、Webプロキシ自動検出プロトコル)用のかなりのコードを書く必要があることです。 –

+0

おそらくIMは怪しいですが、自動プロキシ設定スクリプトの場合は、InternetOpenに1つのフラグを変更するだけではありませんか?しかし、WPADの助けを捧げることはできません... – Goz

+0

何かを完全に無関係に探している間、私はこのページをMSDNで見つけましたか? http://msdn.microsoft.com/en-us/library/aa383910.aspx 余分なコードが必要ですが、特に複雑ではありません:) – Goz

7

私はlibcurlと考えています。そしてその後、いくつかの。

This exampleは、HTTPページを取得してメモリに格納する方法を示しています。これはあなたの例より少しコードですが、それはC言語です。

+0

これは良い答えです - libcurlは本当に素晴らしいライブラリであり、私がこれまで使用してきたこと。しかし、プロキシ領域では不足しています。たとえば、(.NETクラスのように)自動プロキシ設定スクリプトをサポートしていません。 –

1

あなたは間違った場所を探しています。それはただの悲しい現実です。だから、libcurl用のC++ラッパーがcurlppと呼ばれています。

以下は、ウェブページを検索してstdoutストリームに印刷する方法のexampleです。

#include <curlpp/curlpp.hpp> 
#include <curlpp/Easy.hpp> 
#include <curlpp/Options.hpp> 


using namespace curlpp::options; 

int main(int, char **) 
{ 
    try 
    { 
    // That's all that is needed to do cleanup of used resources (RAII style). 
    curlpp::Cleanup myCleanup; 

    // Our request to be sent. 
    curlpp::Easy myRequest; 

    // Set the URL. 
    myRequest.setOpt<Url>("http://example.com"); 

    // Send request and get a result. 
    // By default the result goes to standard output. 
    myRequest.perform(); 
    } 

    catch(curlpp::RuntimeError & e) 
    { 
    std::cout << e.what() << std::endl; 
    } 

    catch(curlpp::LogicError & e) 
    { 
    std::cout << e.what() << std::endl; 
    } 

    return 0; 
} 
4

POCOには、クロスプラットフォームのネットワーキングコンポーネントもあります。

例はこのような何か(これはエラーチェックを綿毛なしで)としてFTPプログラムを与える

Poco::Net::FTPStreamFactory::registerFactory(); 
std::ofstream localFile(inputFile, std::ios_base::out | std::ios_base::binary); 
Poco::URI uri(inputURL); 
std::auto_ptr<std::istream> ptrFtpStream(Poco::Net::URIStreamOpener::defaultOpener().open(uri)); 
Poco::StreamCopier::copyStream(*ptrFtpStream.get(), localFile); 
1

Qtライブラリの一部であるQtNetworkも可能です。

7

libcurl/curlpp(これは柔軟で強力ですが、私は非常に... clunkyです)から離れて、私は私の目を守っている2つのC++ライブラリがあります。両方とも全く新しいBoost :: ASIOに基づいています。しかしでも(私が知る限り)プロキシをサポートしていません。

cpp-netlibblogは)おそらくもっと成熟している(私はそれはいくつかの現実世界のテストを持っていたことを知っている)が、現在(私はそれに取り組んでいます!)タイムアウトを欠いています。例:

network::http::request request("http://google.com"); 
network::http::client client; 
network::http::response response; 

response = client.get(request); 
if (response.status() == 200) 
{ 
    std::cout << body(response)); 
} 

Urdldocumentation)ASIO作成者によって書かれているとタイムアウトを持っている(ただし、先月のみ発表されました)。これは、ストリームで動作するように選ぶ、別のモデルを使用しています:

urdl::istream is("http://google.com"); 
std::string line; 
while (std::getline(is, line)) 
{ 
    std::cout << line << std::endl; 
} 

私はC++がHTTPのための素晴らしいサポートを持っていませんが、これらのライブラリの両方が、約束の多くを示していることに同意するものとします。

関連する問題