7 #define INTERNET_FLAGS (INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_NO_UI | INTERNET_FLAG_NO_COOKIES )
9 static char *http_receive(HINTERNET h_req
, u_long
*d_size
)
11 u_long bytes
= sizeof(u_long
);
17 if (HttpQueryInfo(h_req
, HTTP_QUERY_CONTENT_LENGTH
| HTTP_QUERY_FLAG_NUMBER
, &qsize
, &bytes
, NULL
) != 0) {
18 data
= malloc(qsize
+ 1);
23 if (InternetReadFile(h_req
, buff
, sizeof(buff
), &bytes
) == 0) {
26 if ( (readed
+ bytes
) > qsize
) {
27 data
= realloc(data
, readed
+ bytes
+ 1);
28 if (data
== NULL
) break;
31 memcpy(data
+ readed
, buff
, bytes
); readed
+= bytes
;
34 if ( (data
!= NULL
) && (readed
!= qsize
) ) {
35 free(data
); data
= NULL
;
37 if (d_size
!= NULL
) *d_size
= readed
;
43 void *http_get(wchar_t *url
, u_long
*d_size
)
45 HINTERNET h_inet
= NULL
;
46 HINTERNET h_req
= NULL
;
51 h_inet
= InternetOpen(NULL
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
52 if (h_inet
== NULL
) break;
54 h_req
= InternetOpenUrl(h_inet
, url
, NULL
, 0, INTERNET_FLAGS
, 0);
55 if (h_req
== NULL
) break;
57 replay
= http_receive(h_req
, d_size
);
61 InternetCloseHandle(h_req
);
64 InternetCloseHandle(h_inet
);
69 void *http_post(wchar_t *url
, void *data
, int size
, u_long
*d_size
)
71 URL_COMPONENTS url_cm
= {0};
72 HINTERNET h_inet
= NULL
;
73 HINTERNET h_conn
= NULL
;
74 HINTERNET h_req
= NULL
;
77 wchar_t host
[MAX_PATH
];
78 wchar_t path
[MAX_PATH
];
84 if ( (q_data
= malloc(size
* 3 + 10)) == NULL
) {
87 strcpy(q_data
, "data="); p
= q_data
+ 5;
90 p
+= sprintf(p
, "%%%.2x", (u_int
)*d
++);
93 url_cm
.dwStructSize
= sizeof(url_cm
);
94 url_cm
.lpszHostName
= host
;
95 url_cm
.dwHostNameLength
= sizeof(host
);
96 url_cm
.lpszUrlPath
= path
;
97 url_cm
.dwUrlPathLength
= sizeof(path
);
99 if (InternetCrackUrl(url
, 0, 0, &url_cm
) == 0) {
103 h_inet
= InternetOpen(NULL
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
104 if (h_inet
== NULL
) break;
106 h_conn
= InternetConnect(h_inet
, host
, url_cm
.nPort
, NULL
, NULL
, INTERNET_SERVICE_HTTP
, 0, 0);
107 if (h_conn
== NULL
) break;
109 h_req
= HttpOpenRequest(h_conn
, L
"POST", path
, NULL
, NULL
, NULL
, INTERNET_FLAGS
, 0);
110 if (h_req
== NULL
) break;
112 HttpAddRequestHeaders(
113 h_req
, L
"Content-Type: application/x-www-form-urlencoded", 47*2, HTTP_ADDREQ_FLAG_ADD
);
115 if (HttpSendRequest(h_req
, NULL
, 0, q_data
, strlen(q_data
)) == 0) {
118 replay
= http_receive(h_req
, d_size
);
122 InternetCloseHandle(h_req
);
124 if (h_conn
!= NULL
) {
125 InternetCloseHandle(h_conn
);
127 if (h_inet
!= NULL
) {
128 InternetCloseHandle(h_inet
);
130 if (q_data
!= NULL
) free(q_data
);