0

i need help about getting a fist line from a web page, found a little script working with ipchicken.com. but i connot run it with me web script , posting the original code becouse i was mad about all things ... for that! i search for that here but i dont find and i post a question ..

but it need to be edit correctly to works with mine php script .. php > file.php?get=ver or ip on 192.168.1.1 port 88

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

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

here is the original c++ code >

    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;
    }

and .. yea how to add a port select like this ->

char *getwebpage(char *hostname, char *uri, char *port, unsigned long *total)
HINTERNET connect = InternetConnect(session, hostname, port, "", "", INTERNET_SERVICE_HTTP, 0, 0);
01001110
  • 1
  • 1
  • then i try to get info from my file is some string like asci byte in txt mode, it works without int getmyipaddress(char *buffer) function and ` 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); } ` – 01001110 Mar 06 '12 at 22:01
  • replaced with > [code] BYTE Buffer[512]; DWORD BytesRead; while(InternetReadFile(http, Buffer, 3*6, &BytesRead) && BytesRead != 0) { printf("Version:[%s]",Buffer); }[/code] – 01001110 Mar 06 '12 at 22:02

2 Answers2

1

You say your service is at port 88, but you call:

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

so maybe change to:

HINTERNET connect = InternetConnect(session, hostname, 88, ....
marcinj
  • 48,511
  • 9
  • 79
  • 100
  • no, the port is not problem. problem comes from copy and other string where is in while process. – 01001110 Mar 06 '12 at 22:24
  • while in getwebpage looks ok, maybe you are just getting results that you expect to be different? but this is the problem on server, like maybe you return utf16 but you expect in C++ to receive utf8 – marcinj Mar 07 '12 at 08:45
0

Try to do InternetReadFile(http, &buffer, 1024, &read). I am not sure if this will sove your problem but I am quite sure there has to be a pointer to the buffer in the function InternetReadFile() call

Ionut J. Bejan
  • 734
  • 11
  • 28