http://xxxx.xxx
idcode
actcode
./log idcode actcode data
post http://xxxx.xxxx
if(result=ok){
read logs from '/Skoo/Box/log/log.txt' one by one
post it
if(result=ok){
delete it
}
}
else{
white it to 'log.txt'
}
上面是偽代碼,我想知道如何實現post,最好有代碼。
C語言和shell吧盡量,謝謝
C可以用curl
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#define POSTURL "http://www.xiami.com/member/login"
#define POSTFIELDS "[email protected]&password=mypassword&autologin=1&submit=登 錄&type="
#define FILENAME "curlposttest.log"
size_t write_data(void* buffer,size_t size,size_t nmemb,void *stream)
{
FILE *fptr = (FILE*)stream;
fwrite(buffer,size,nmemb,fptr);
return size*nmemb;
}
int main(int argc,char *argv[])
{
CURL *curl;
CURLcode res;
FILE* fptr;
struct curl_slist *http_header = NULL;
if ((fptr = fopen(FILENAME,"w")) == NULL)
{
fprintf(stderr,"fopen file error:%s\n",FILENAME);
return -1;
}
curl = curl_easy_init();
if (!curl)
{
fprintf(stderr,"curl init failed\n");
return -1;
}
curl_easy_setopt(curl,CURLOPT_URL,POSTURL); //url地址
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,POSTFIELDS); //post參數
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_data); //對返回的數據進行操作的函數地址
curl_easy_setopt(curl,CURLOPT_WRITEDATA,fptr); //這是write_data的第四個參數值
curl_easy_setopt(curl,CURLOPT_POST,1); //設置問非0表示本次操作為post
curl_easy_setopt(curl,CURLOPT_VERBOSE,1); //打印調試信息
curl_easy_setopt(curl,CURLOPT_HEADER,1); //將響應頭信息和相應體一起傳給write_data
curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1); //設置為非0,響應頭信息location
curl_easy_setopt(curl,CURLOPT_COOKIEFILE,"/Users/zhu/CProjects/curlposttest.cookie");
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
switch(res)
{
case CURLE_UNSUPPORTED_PROTOCOL:
fprintf(stderr,"不支持的協議,由URL的頭部指定\n");
case CURLE_COULDNT_CONNECT:
fprintf(stderr,"不能連接到remote主機或者代理\n");
case CURLE_HTTP_RETURNED_ERROR:
fprintf(stderr,"http返回錯誤\n");
case CURLE_READ_ERROR:
fprintf(stderr,"讀本地文件錯誤\n");
default:
fprintf(stderr,"返回值:%d\n",res);
}
return -1;
}
curl_easy_cleanup(curl);
}