- #include <stdio.h>
- #include <curl/curl.h>
- #include <string.h>
- #include <ctype.h>
- #include <iconv.h>
- #define TMP_FILE "tmp.html"
- #define HTML_BUFFER_SIZE 1024*800
-
- void split(char **arr, char *str, const char *del) {
- char *s = strtok(str, del);
- while (s != NULL) {
- *arr++ = s;
- s = strtok(NULL, del);
- }
- }
-
- void replaceFirst(char *str1, char *str2, char *str3) {
- char str4[strlen(str1) + 1];
- char *p;
- strcpy(str4, str1);
- if ((p = strstr(str1, str2)) != NULL)
- while (str1 != p && str1 != NULL)
- str1++;
- }
- str1[0] = '/0';
- strcat(str1, str3);
- strcat(str1, strstr(str4, str2) + strlen(str2));
- }
- }
-
- void replace(char *str1, char *str2, char *str3) {
- while (strstr(str1, str2) != NULL) {
- replaceFirst(str1, str2, str3);
- }
- }
-
- void substring(char *dest, char *src, int start, int end) {
- char *p = src;
- int i = start;
- if (start > strlen(src))return;
- if (end > strlen(src))
- end = strlen(src);
- while (i < end) {
- dest[i - start] = src[i];
- i++;
- }
- dest[i - start] = '/0';
- return;
- }
-
- char charAt(char *src, int index) {
- char *p = src;
- int i = 0;
- if (index < 0 || index > strlen(src))
- return 0;
- while (i < index)i++;
- return p[i];
- }
-
- int indexOf(char *str1, char *str2) {
- char *p = str1;
- int i = 0;
- p = strstr(str1, str2);
- if (p == NULL)
- return -1;
- else {
- while (str1 != p) {
- str1++;
- i++;
- }
- }
- return i;
- }
-
- int lastIndexOf(char *str1, char *str2) {
- char *p = str1;
- int i = 0, len = strlen(str2);
- p = strstr(str1, str2);
- if (p == NULL)return -1;
- while (p != NULL) {
- for (; str1 != p; str1++)i++;
- p = p + len;
- p = strstr(p, str2);
- }
- return i;
- }
-
- void ltrim(char *str) {
- int i = 0, j, len = strlen(str);
- while (str[i] != '/0') {
- if (str[i] != 32 && str[i] != 9)break;
- i++;
- }
- if (i != 0)
- for (j = 0; j <= len - i; j++) {
- str[j] = str[j + i];
- }
- }
-
- void rtrim(char *str) {
- char *p = str;
- int i = strlen(str) - 1;
- while (i >= 0) {
- if (p[i] != 32 && p[i] != 9)break;
- i--;
- }
- str[++i] = '/0';
- }
-
- void trim(char *str) {
- ltrim(str);
- rtrim(str);
- }
-
-
- static size_t write_callback(void *ptr, size_t size, size_t nmemb, void *stream) {
- int len = size * nmemb;
- int written = len;
- FILE *fp = NULL;
- if (access((char*) stream, 0) == -1) {
- fp = fopen((char*) stream, "wb");
- } else {
- fp = fopen((char*) stream, "ab");
- }
- if (fp) {
- fwrite(ptr, size, nmemb, fp);
- }
-
- fclose(fp);
- return written;
- }
-
- void test_post(char* url,char* data) {
- CURL *curl;
- CURLcode res;
- curl = curl_easy_init();
- if (curl) {
-
- curl_easy_setopt(curl, CURLOPT_URL, url);
- curl_easy_setopt(curl, CURLOPT_POST, 1L);
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
- res = curl_easy_perform(curl);
- curl_easy_cleanup(curl);
- }
- }
-
- int file_exists(char *filename) {
- return (access(filename, 0) == 0);
- }
- int GetCharset(char *src_html,char *charCode) {
- char tmp_html[HTML_BUFFER_SIZE]={0};
- int pos = indexOf(src_html, "text/html; charset=");
- if (pos > 0) {
- strncpy(tmp_html, src_html + pos + strlen("text/html; charset="), strlen(src_html) - pos);
- pos = indexOf(tmp_html, "\"");
- if (pos > 0) {
- strncpy(charCode, tmp_html, pos);
- }
- }
- return 0;
-
- }
-
- void test_get(char* url) {
- CURL *curl;
- CURLcode res;
- curl = curl_easy_init();
- if (curl) {
- if (file_exists(TMP_FILE))
- remove(TMP_FILE);
- curl_easy_setopt(curl, CURLOPT_URL, url);
-
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
-
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, TMP_FILE);
- res = curl_easy_perform(curl);
- char tocode[64] = "UTF-8";
- if (CURLE_OK == res) {
- char *ct;
- res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
- if ((CURLE_OK == res) && ct)
- printf("We received Content-Type: %s\n", ct);
-
-
- int index = indexOf(ct, "=");
- char* arr[3];
- if (index > 0) {
- split(arr, ct, "=");
-
- strcpy(tocode, arr[1]);
- }
- FILE *fp = NULL;
- fp = fopen(TMP_FILE, "r");
- char src_html[HTML_BUFFER_SIZE]={0};
- char output_html[HTML_BUFFER_SIZE]={0};
- char tmp_html[HTML_BUFFER_SIZE]={0};
- if (fp) {
- fread(src_html, HTML_BUFFER_SIZE, 1, fp);
- strcpy(tmp_html,src_html);
- if(index <0) {
- GetCharset(tmp_html,tocode);
- printf("%s\n",tocode);
- }
- int iRet;
-
- iconv_t hIconv = iconv_open(tocode,"iso-8859-1");
- if (-1 == (int) hIconv) {
- return -1;
- }
- printf("%s\n",src_html);
-
- iRet = iconv(hIconv, (char**) (&src_html), strlen(src_html), (char**) (&output_html), strlen(src_html));
- printf("%s\n", output_html);
- printf("ok");
- if(strcmp(output_html,"")==0)
- {
- printf("%s\n",src_html);
- }
-
- iconv_close(hIconv);
-
- }
-
- }
- curl_easy_cleanup(curl);
- }
- }
-
- int main(int argc, char* argv) {
-
-
- test_post("http://192.168.1.6:8080/TestServer/index.html","wd=hava&hehe=123456");
-
- printf("\nok");
- return 0;
- }
-
|
请发表评论