Header easyhttp.h
#ifndef EASYHTTP_H_INCLUDED
#define EASYHTTP_H_INCLUDED
#include <windows.h>
#include <wininet.h>
#include <string.h>
typedef struct _HTTP
{
HINTERNET http;
HINTERNET www;
HINTERNET req;
}HTTP;
HTTP* http_fopen(int* reterr,char* server,char* file,char* user,char* psw);
int http_ok(HTTP* h);
long http_fseek(HTTP* h,long offset,int origin);
long http_ftell(HTTP* h);
size_t http_fread(void* ptr,size_t size,size_t nobj,HTTP* h);
size_t http_fwrite(void* ptr,size_t size,size_t nobj,HTTP* h);
void http_free(HTTP* h);
#endif // EASYHTTP_H_INCLUDED
easyhttp.c
#include "easyhttp.h"
HTTP* http_fopen(int* reterr,char* server,char* file,char* user,char* psw)
{
DWORD istate;
if ( InternetGetConnectedState(&istate,0) == FALSE ) {*reterr = -7; return NULL;}
DWORD retstatus;
DWORD szstatus = sizeof(retstatus);
HTTP* http = (HTTP*) malloc(sizeof(HTTP));
if (http == NULL) return NULL;
http->http = NULL;
http->www = NULL;
http->req = NULL;
http->http = InternetOpen("easyhttp",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (http->http == NULL) {*reterr = -1;http_free(http);return NULL;}
http->www = InternetConnect(http->http,server,INTERNET_INVALID_PORT_NUMBER,NULL,NULL,INTERNET_SERVICE_HTTP,0,0);
if (http->www == NULL) { *reterr = -2;http_free(http);return NULL;}
http->req = HttpOpenRequest(http->www, "GET",file, NULL, NULL, NULL,INTERNET_FLAG_KEEP_CONNECTION ,0);
if (http->req == NULL) { *reterr = -3;http_free(http);return NULL;}
int res = 0;
RESEND:
HttpSendRequest(http->req, NULL, 0, NULL, 0);
HttpQueryInfo(http->req, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE, &retstatus, &szstatus, NULL);
switch (retstatus)
{
DWORD cchUserLength, cchPasswordLength;
case HTTP_STATUS_PROXY_AUTH_REQ:
if (res)
{
*reterr = -4;
http_free(http);
return NULL;
}
else if (user == NULL || psw == NULL)
{
*reterr = -5;
http_free(http);
return NULL;
}
res = 1;
cchUserLength = strlen(user) + 1;
cchPasswordLength = strlen(psw) + 1;
InternetSetOption(http->req,INTERNET_OPTION_PROXY_USERNAME,user,cchUserLength);
InternetSetOption(http->req,INTERNET_OPTION_PROXY_PASSWORD,psw, cchPasswordLength);
goto RESEND;
break;
case HTTP_STATUS_DENIED:
if (res)
{
*reterr = -6;
http_free(http);
return NULL;
}
else if (user == NULL || psw == NULL)
{
*reterr = -5;
http_free(http);
return NULL;
}
res = 1;
cchUserLength = strlen(user) + 1;
cchPasswordLength = strlen(psw) + 1;
InternetSetOption(http->req,INTERNET_OPTION_USERNAME,user,cchUserLength);
InternetSetOption(http->req,INTERNET_OPTION_PASSWORD,psw, cchPasswordLength);
goto RESEND;
break;
}
*reterr = 0;
return http;
}
int http_ok(HTTP* h)
{
if (h == NULL) return 0;
if (h->http == NULL || h->www == NULL || h->req == NULL ) return 0;
return 1;
}
long http_fseek(HTTP* h,long offset,int origin)
{
if (!http_ok(h)) return -1;
if (origin == SEEK_CUR)
origin = FILE_CURRENT;
else if (origin == SEEK_END)
origin = FILE_END;
else
origin = FILE_BEGIN;
long ret = InternetSetFilePointer(h->req,offset,NULL,origin,0);
if (ret < 0 || ret == INVALID_SET_FILE_POINTER)
return -2;
return ret;
}
long http_ftell(HTTP* h)
{
return http_fseek(h,0,SEEK_CUR);
}
size_t http_fread(void* ptr,size_t size,size_t nobj,HTTP* h)
{
if (!http_ok(h)) return 0;
DWORD nread;
DWORD toread = size * nobj;
if (InternetReadFile(h->req,ptr,toread,&nread) == FALSE ) return 0;
nread /= size;
return nread;
}
size_t http_fwrite(void* ptr,size_t size,size_t nobj,HTTP* h)
{
if (!http_ok(h)) return 0;
DWORD nwrite;
DWORD towrite = size * nobj;
if (InternetWriteFile(h->req,ptr,towrite,&nwrite) == FALSE ) return 0;
nwrite /= size;
return nwrite;
}
void http_free(HTTP* h)
{
if (h == NULL) return ;
if (h->req != NULL)
InternetCloseHandle(h->req);
if (h->www != NULL)
InternetCloseHandle(h->www);
if (h->http != NULL)
InternetCloseHandle(h->http);
free(h);
}
per usarlo
main.c
#include <stdio.h>
#include <stdlib.h>
#include "easyhttp.h"
int main()
{
printf("Http...\n");
printf("Test download file\n");
char* server = "*************";
char* file = "/************f";
char* filedest = "d:\\mydownload.pdf";
int rerr;
printf("Open:\"%s%s\"...",server,file);
HTTP* h = http_fopen(&rerr,server,file,NULL,NULL);
if (h == NULL)
{
switch (rerr)
{
case -1:
printf("err internet\n");
break;
case -2:
printf("err connection on server\n");
break;
case -3:
printf("err connection openfile\n");
break;
case -4:
printf("err proxy autentication\n");
break;
case -5:
printf("err request username and password\n");
break;
case -6:
printf("err autentication\n");
break;
case -7:
printf("err no internet connection\n");
break;
}
return rerr;
}
printf("ok\n");
printf("Local file:");
FILE* d = fopen(filedest,"wb");
if (d == NULL)
{
http_free(h);
printf("err open file\n");
return -7;
}
printf("ok\n");
double rsz =(double) http_fseek(h,0,SEEK_END) / 1024.0;
printf("remote file size:%f\n",rsz);
http_fseek(h,0,SEEK_SET);
char buffer[1024];
DWORD read=0;
int total=0;
printf("download file:\n");
while ( (read=http_fread(buffer,sizeof(char),1024,h)) > 0 )
{
fwrite(buffer,sizeof(char),read,d);
total+=read;
}
http_free(h);
fclose(d);
printf("end download:\n");
return 0;
}
O P E N S O U R C E