1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| void UPLOAD()
{
WSADATA WSAData;
int erreur = WSAStartup(MAKEWORD(2,0), &WSAData);
SOCKET sock;
SOCKADDR_IN sin;
char buffer[1024] = "";
/* Si les sockets Windows fonctionnent */
if(!erreur)
{
/* Création de la socket */
sock = socket(AF_INET, SOCK_STREAM, 0);
/* Configuration de la connexion */
struct hostent *ipserver;
ipserver = gethostbyname(url_server);
memcpy(&sin.sin_addr,ipserver->h_addr,ipserver->h_length);
sin.sin_family = AF_INET;
sin.sin_port = htons(80);
/* Si l'on a réussi à se connecter */
if(connect(sock, (SOCKADDR *)&sin, sizeof(sin)) != SOCKET_ERROR)
{
FILE* fichier = NULL;
fichier = fopen("test.jpg", "rb");
char * commande;
char * bufferFichier;
char * variables;
long lSize;
// obtain file size:
fseek (fichier , 0 , SEEK_END);
lSize = ftell(fichier);
rewind (fichier);
// allocate memory to contain the whole file:
bufferFichier = (char*) malloc (sizeof(char)*lSize);
commande = (char*) malloc (sizeof(char)*lSize+8000);
variables = (char*) malloc (sizeof(char)*lSize+15000);
sprintf(variables,"--frontier\r\nContent-Disposition: form-data; name=\"envoyer\"\r\n\r\noui\r\n--frontier\r\nContent-Disposition: form-data; name=\"fichier\"; filename=\"test.jpg\"\r\nContent-Type: image/jpeg\r\nContent-Transfer-Encoding: binary\r\n\r\n");
if (fichier != NULL)
{
fread(bufferFichier,1,lSize,fichier);
}
strcat(variables,bufferFichier);
strcat(variables,"\r\n--frontier--\r\n");
sprintf(commande,"POST %s HTTP/1.1\r\nHost: %s\r\nAccept: */*\r\nContent-Type: multipart/form-data; boundary=\"frontier\"\r\nMIME-version: 1.0\r\nContent-Length:%d\r\n\r\n%s",http_req_path,url_server,(strlen(variables)+lSize),variables);
send(sock,commande,strlen(commande)+1,0);
printf("%s", commande);
free(commande);
free(variables);
free(bufferFichier);
char response [1024] ="";
while (recv(sock,buffer, 1024, 0) > 0)
{
strcat(response, buffer);
}
char *resultatfinal = strstr(response, "\r\n\r\n")+4;
printf("%s", resultatfinal);
}
/* sinon, on affiche "Impossible de se connecter" */
else
{
printf("Impossible de se connecter\n");
}
/* On ferme la socket */
closesocket(sock);
WSACleanup();
}
} |
Partager