| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 671 人关注过本帖
标题:httpGet程序运行时连不上是怎么回事,能帮我看下吗?谢啦
只看楼主 加入收藏
pingguo08
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2008-7-21
收藏
 问题点数:0 回复次数:0 
httpGet程序运行时连不上是怎么回事,能帮我看下吗?谢啦
#ifndef MWEBCLIENT_H
#define MWEBCLIENT_H

#include<string>
#include <sys/socket.h>
#include<unistd.h>

class MWebClient
{
public:
    MWebClient();

    int doGet(const char* getUrl,std::string &responseBuf);
   
    void setTimeOut(int seconds);

    int doRequest(char* ip ,std::string &request, std::string &responseBuf);
private:
    int m_port;
    int m_outtime_second;
};

#endif // MWEBCLIENT_H

#include "../include/MWebclient.h"
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/utsname.h>
#include <stdio.h>
#include <string>
#include <string.h>
#include<stdlib.h>
#include <iostream>
extern void *memset(void *buffer, int c, int count);

MWebClient::MWebClient()
{
    m_port =8000;
    m_outtime_second =10;
}

void MWebClient::setTimeOut(int seconds)
{
    m_outtime_second = seconds;
}

int MWebClient::doRequest(char* ip, std::string &request, std::string &responseBuf)
{

    int search_socket =-1;
    struct sockaddr_in search_server;
    int retval =0, tmp_val =0;
    struct timeval tv;
    search_socket =socket(AF_INET, SOCK_STREAM, 0);

    if(search_socket==-1)
    {
       return -1;
    }

    // 设置地址可重用
    tmp_val =1;
    retval =setsockopt(search_socket, SOL_SOCKET, SO_REUSEADDR,
        (char *)&tmp_val, sizeof(tmp_val));

    if( retval !=0 )
    {
      close(search_socket);
      search_socket =-1;
      return -1;
    }

    // 设置超时参数
    int tSec = m_outtime_second;
    int tUsec =0;
    if(tUsec<0)
    {
       tUsec =0;
    }
    tv.tv_sec = tSec;
    tv.tv_usec = tUsec;

    retval =setsockopt( search_socket, SOL_SOCKET, SO_RCVTIMEO,
        (char *)&tv, sizeof(tv));
    if( retval !=0 )
    {
    close( search_socket );
    search_socket =-1;
    return -1;
    }
    retval =setsockopt( search_socket, SOL_SOCKET, SO_SNDTIMEO,(char *)&tv, sizeof(tv));
    if( retval !=0 )
    {
      close( search_socket );
      search_socket =-1;
      return -1;
    }

    memset((char*) &search_server, 0, sizeof(search_server));
    search_server.sin_family = AF_INET;
    search_server.sin_port =htons(m_port);
    std::cout<<ip<<std::endl;

    search_server.sin_addr.s_addr =inet_addr(ip);
    retval =connect( search_socket, (struct sockaddr *) &search_server, sizeof(search_server) );
    if( retval !=0 )
    {
       printf("retval wrong");
       close( search_socket );
       search_socket =-1;
       return -1;
    }


    retval =write(search_socket,request.c_str(),request.length());

    if(retval==-1)
    {
        close( search_socket );
        search_socket =-1;
        return-1;
    }

    char strBuf[1024];
    size_t neft=sizeof(strBuf);
    while(true){
       retval =read(search_socket,strBuf,neft);

       if(retval>0)
       {
           responseBuf.append(strBuf,retval);
       }
       else
       {
           break;
       }

       if(retval<(int)sizeof(strBuf))
       {
           break;
       }
    }
    close( search_socket );
    search_socket =-1;

    return 0;
}

int MWebClient::doGet(const char* getUrl,std::string &responseBuf)
{
    char hostname[64];
    char getstr[512];
    char *ip="";


    char tmpGetUrl[512];
    strncpy(tmpGetUrl,getUrl,sizeof(tmpGetUrl)-1);
    char* tmpP;
    tmpP =strstr(tmpGetUrl,"http://");
    if(tmpP!=NULL)
    {
       tmpP = tmpP+7;
    }
    else
    {
       tmpP = tmpGetUrl;
    }
    ip=tmpP;
    char* tmpP2;
    tmpP2 =strstr(tmpP,"/");
    if(tmpP2!=NULL)
    {
       if(tmpP2-tmpP>=(int)sizeof(hostname))
       {
          // return -1;
       }
       memcpy(hostname,tmpP,tmpP2-tmpP);
       hostname[tmpP2-tmpP] ='\0';
       strncpy(getstr,tmpP2,sizeof(getstr)-1);
    }
    else
    {
       strncpy(hostname,tmpP,sizeof(hostname)-1);
       getstr[0] ='/';
       getstr[1] ='\0';
    }

    //struct hostent * hostPtr;
    //hostPtr =gethostbyname(hostname);
    //if(hostPtr==NULL)
  // {
      // return -1;
    //}
    //struct in_addr * addrPtr;
   // addrPtr =(struct in_addr *) *(hostPtr->h_addr_list);
    //strncpy(ip,inet_ntoa(*addrPtr),sizeof(ip)-1);

    std::string request;
    request.append("GET ").append(getstr).append(" HTTP/1.1").append("\r\n");
    request.append("Accept: */*").append("\r\n");
    request.append("Referer: ").append(getUrl).append("\r\n");
    request.append("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; InfoPath.1)").append("\r\n");
    request.append("Host: ").append(hostname).append("\r\n");
    request.append("Connection: Keep-Alive").append("\r\n");
    request.append("\r\n");

    return doRequest(ip,request,responseBuf);
}

#include <iostream>
#include"include/MWebclient.h"
#include <string>

using namespace std;

int main()
{

    MWebClient obj=MWebClient::MWebClient();
    std::string responseBuf="";
    //obj.doPost("192.168.0.137:8000","fsssss",responseBuf);
    obj.doGet("192.168.0.137:8000",responseBuf);
    std::cout<<responseBuf<<std::endl;


    return 0;
}
搜索更多相关主题的帖子: httpGet 运行 
2008-11-26 21:51
快速回复:httpGet程序运行时连不上是怎么回事,能帮我看下吗?谢啦
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025777 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved