Get访问HTTP服务器

来源:互联网 发布:淘宝在哪里修改配送地 编辑:程序博客网 时间:2024/06/05 02:56
#include <cc++/common.h>
#include 
<iostream>
#include 
<cstdlib>

#ifdef  CCXX_NAMESPACES
using namespace std;
using namespace ost;
#endif

class myURLStream : public URLStream
{
    
private:
        
void httpHeader(const char *header, const char *value) 
        {       
            cout 
<< "HEADER " << header << "=" << value << endl;
        }       
};
int main(int argc, char **argv)
{
    myURLStream url;
    
char cbuf[1024];
    URLStream::Error status;
    
int len;

    
const char* addr = "http://www.webservicex.net/globalweather.asmx/GetWeather?CityName=beijing&CountryName=china";
#ifdef  CCXX_EXCEPTIONS
    
try
    {
#endif
        status 
= url.get(addr);
        
if(status)
        {
            cout 
<< "failed; reason=" << status << endl;
            url.close();
        }
        
else{
            cout 
<< "loading..." << endl;
            
while(!url.eof())
            {
                url.read(cbuf, sizeof(cbuf));
                len 
= url.gcount();
                
if(len > 0)
                    cout.write(cbuf, len);
            }
            url.close();
            cout 
<< ends;
        }
#ifdef  CCXX_EXCEPTIONS
    }
    
catch(...)
    {
        cerr 
<< "url " << addr << " failed" << endl;
    }
#endif
    
return 0;
}
 
原创粉丝点击