2012-03-06 37 views
0

我需要帮助,从网页获取第一行,找到一个小脚本与ipchicken.com合作。 但我无法与我一起运行它的网页脚本,张贴原始代码,因为我对所有事情都很生气......为此! 我搜索,在这里,但我不觉得我张贴的问题..InternetReadFile获取专用线

,但它需要正确地编辑与我的PHP脚本.. PHP> file.php?GET =版本或IP 192.168 .1.1口88

switch($_GET['get']) 
    { 
case "ip" : 
echo ($_SERVER['REMOTE_ADDR']); 
    break; 

case "ver" : 
print "0.1.1"; 
    break; 
default : 
break; 
    } 

原来这里是C++代码>

char *getwebpage(char *hostname, char *uri, unsigned long *total) 
{ 
    if(!hostname || !uri || !total) return (char *)0; 
    *total = 0; 

    char *headers1 = "Accept: text/html, */*\nAccept-Language: en-GB\nAccept-Encoding: none\nHost: "; 
    char *headers2 = (char *)malloc(strlen(headers1) + strlen(hostname) + 2); 
    sprintf(headers2, "%s%s\n", headers1, hostname); 
    HINTERNET session = InternetOpen("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); 
    HINTERNET connect = InternetConnect(session, hostname, 80, "", "", INTERNET_SERVICE_HTTP, 0, 0); 
    HINTERNET http = HttpOpenRequest(connect, "GET", uri, HTTP_VERSION, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0); 
    HttpSendRequest(http, headers2, strlen(headers2), NULL, 0); 
    free(headers2); 

    unsigned long read; 
    char buffer[1024]; 
    char *final = (char *)malloc(1024); 
    memset(buffer, 0, 1024); 
    while(InternetReadFile(http, buffer, 1024, &read) && (read != 0)){ 
     CopyMemory((final + *total), buffer, read); 
     *total += read; 
     final = (char *)realloc(final, (*total + 1024)); 
     memset((final + *total), 0, 1024); 
    } 

    InternetCloseHandle(http); 
    InternetCloseHandle(connect); 
    InternetCloseHandle(session); 

    return final; 
} 

    int getmyipaddress(char *buffer) 
    { 
     unsigned long length; 
     char *webpage = getwebpage("www.ipchicken.com", "/", &length); 
     if(!webpage || length == 0) return 0; 
     int result = 0; 
     char *start = strstr(webpage, "<b>"); 
     if(start){ 
      start += 3; 
      while(*start <= ' ') start++; 
      char *end = start; 
      while(*end > ' ') end++; 
      *end = 0; 
      strcpy(buffer, start); 
      result = 1; 
     } 
     free(webpage); 
     return result; 
    } 

和..酵母如何添加端口选择这样的 - >

char *getwebpage(char *hostname, char *uri, char *port, unsigned long *total) 
HINTERNET connect = InternetConnect(session, hostname, port, "", "", INTERNET_SERVICE_HTTP, 0, 0); 
+0

并且问题是? – marcinj 2012-03-06 21:47:15

+0

然后我尝试从我的文件中获取信息是一些字符串,如txt模式下的asci字节,它的工作方式没有int getmyipaddress(char * buffer)函数和 'unsigned long read; char buffer [1024]; char * final =(char *)malloc(1024); memset(buffer,0,1024); (InternetReadFile(http,buffer,1024,&read)&&(read!= 0))CopyMemory((final + * total),buffer,read); * total + = read; final =(char *)realloc(final,(* total + 1024)); memset((final + * total),0,1024); } ' – 01001110 2012-03-06 22:01:38

+0

替换为> [code] \t BYTE Buffer [512]; DWORD BytesRead; \t而(的InternetReadFile(HTTP,缓冲液,3 * 6,&BytesRead)&& BytesRead!= 0){ \t \t的printf( “版本:[%S]”,缓冲液); \t} [/ code] – 01001110 2012-03-06 22:02:03

回答

1

你说你的服务是在端口88,但你拨打:

HINTERNET connect = InternetConnect(session, hostname, 80, .... 

所以也许更改为:

HINTERNET connect = InternetConnect(session, hostname, 88, .... 
+0

不行,端口不是问题。问题来自拷贝和其他处于进程中的字符串。 – 01001110 2012-03-06 22:24:52

+0

虽然在getwebpage看起来不错,也许你只是得到你期望会有所不同的结果?但这是服务器上的问题,就像你可能返回utf16一样,但你期望在C++中接收utf8 – marcinj 2012-03-07 08:45:25

0

尝试做的InternetReadFile(HTTP,&缓冲,1024,&读) 。我不确定这是否会解决您的问题,但我相当肯定必须在函数InternetReadFile()中调用缓冲区指针