linux中如何访问一个URL,并将自己的IP传过去
linux中怎么访问一个URL,并将自己的IP传过去。
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <wininet.h>
#define MAXSIZE 1024
#pragma comment(lib, "Wininet.lib")
#pragma comment( lib, "ws2_32.lib" )
using namespace std;
char * GetIpList()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
cout<<"WSAStartup failed !"<<endl;
return false;
}
char szhn[256];
int nStatus = gethostname(szhn, sizeof(szhn));
if (nStatus == SOCKET_ERROR )
{
cout<<"gethostname failed, Error code: "<<WSAGetLastError()<<endl;
return false;
}
HOSTENT *host = gethostbyname(szhn);
char * ipaddress =NULL;
if (host != NULL)
{
ipaddress = inet_ntoa( *(IN_ADDR*)host->h_addr_list[0]);
}
WSACleanup();
return ipaddress;
}
int main(){
//Get ip address
char * ip_address = NULL;
ip_address = GetIpList();
cout<<ip_address<<endl;
//Request and send ip_address
char url[100] = "http://www.baidu.com?ip=";
strcat(url, ip_address);
HINTERNET hSession = InternetOpen("UrlTest", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(hSession != NULL)