Advertisement
glauco486

Untitled

Mar 16th, 2011
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. #define _WIN32_WINNT 0x0500
  2. #include <windows.h>
  3. #include <wininet.h>
  4. #include <tchar.h>
  5. #include <iostream>
  6. #include <string.h>
  7. #include <fstream>
  8. #include <cstdlib>
  9. #pragma comment(lib,"wininet.lib")
  10.  
  11.  
  12.  
  13. using namespace std;
  14.  
  15. void envia(){
  16.      int length;
  17.      char * buffer;
  18.      ifstream is;
  19.      is.open ("imagem.jpg", ios::binary );
  20.      is.seekg (0, ios::end);
  21.      length = is.tellg();
  22.      is.seekg (0, ios::beg);
  23.      buffer = new char [length];
  24.      is.read (buffer,length);
  25.      is.close();
  26.  
  27.     string conteudo;
  28.     conteudo ="-----------------------------7d82751e2bc0858\nContent-Disposition: form-data; name=\"file\"; filename=\"imagem.jpg\"\nContent-Type: text/plain\n\n";
  29.     conteudo += buffer ;
  30.     conteudo += "\n-----------------------------7d82751e2bc0858--" ;
  31.    
  32.     TCHAR *param=new TCHAR[conteudo.size()+1];
  33.     param[conteudo.size()]=0;
  34.     std::copy(conteudo.begin(),conteudo.end(),param);
  35.    
  36.    
  37.     static TCHAR hdrs[]    = "Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858";
  38.  
  39.     HINTERNET hSession = InternetOpen("MyAgent",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
  40.     if (!hSession)
  41.     {
  42.         printf(" (!) hSession failed!\n");
  43.     }
  44.    
  45.    
  46.    
  47.    
  48.     HINTERNET hConnect = InternetConnect(hSession,("meusite.com.br"),INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
  49.       if (!hConnect)
  50.     {
  51.         printf(" (!) hConnect failed!\n");
  52.     }
  53.  
  54.  
  55.  
  56.    
  57.     HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",_T("erros/glauco/upload.php"), NULL, NULL, (const char**)"*/*\0", 0, 1);
  58.      if (!hRequest)
  59.     {
  60.         printf(" (!) Ocorreu um erro de numero  %d!\n",GetLastError());
  61.        
  62.         InternetCloseHandle(hSession);
  63.         InternetCloseHandle(hConnect);
  64.         InternetCloseHandle(hRequest);
  65.    
  66.     }
  67.  
  68.  
  69.  
  70.   BOOL sent= HttpSendRequest(hRequest,
  71.   hdrs,
  72.   strlen(hdrs),
  73.   param,
  74.   strlen(param));
  75.     printf(" >>> Enviado Com sucesso!! \n");
  76.     InternetCloseHandle(hSession);
  77.     InternetCloseHandle(hConnect);
  78.     InternetCloseHandle(hRequest);
  79.  
  80.     }
  81.    
  82.  
  83. void ReadFile(char *name)
  84. {
  85.     FILE *file;
  86.     char *buffer;
  87.     unsigned long fileLen;
  88.  
  89.     //Open file
  90.     file = fopen(name, "rb");
  91.     if (!file)
  92.     {
  93.         fprintf(stderr, "Unable to open file %s", name);
  94.         return;
  95.     }
  96.    
  97.     //Get file length
  98.     fseek(file, 0, SEEK_END);
  99.     fileLen=ftell(file);
  100.     fseek(file, 0, SEEK_SET);
  101.  
  102.     //Allocate memory
  103.     buffer=(char *)malloc(fileLen+1);
  104.     if (!buffer)
  105.     {
  106.         fprintf(stderr, "Memory error!");
  107.                                 fclose(file);
  108.         return;
  109.     }
  110.  
  111.     //Read file contents into buffer
  112.     fread(buffer, fileLen, 1, file);
  113.     fclose(file);
  114.  
  115.     //Do what ever with buffer
  116.  
  117.     free(buffer);
  118. }
  119.  
  120. int main(int argc, char *argv[])
  121. {
  122.  
  123.      
  124.  
  125.  
  126.  
  127.  
  128.     //envia();
  129.     system("PAUSE");
  130.     return EXIT_SUCCESS;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement