extern "C" int MAMain() { InitConsole(); gConsoleLogging = 1; static const char data[] = "userid=joe&password=guessme"; int size = sizeof(data) - 1; char buffer[64]; printf("HTTP POST test\n"); Handle http = maHttpCreate("http://msdev.mine.nu:8080/testing/posttest.php", HTTP_POST); CT(http); maHttpSetRequestHeader(http, "X-MoSync-test", "terue"); _itoa(size, buffer, 10); maHttpSetRequestHeader(http, "Content-Length", buffer); maHttpSetRequestHeader(http, "Content-Type", "application/x-www-form-urlencoded"); printf("write\n"); maConnWrite(http, data, size); if(waitConn(http) < 0) Freeze(0); printf("finish\n"); maHttpFinish(http); if(waitConn(http) < 0) Freeze(0); int res = maHttpGetResponseHeader(http, "Content-Length", buffer, sizeof(buffer)); if(res <= 0 || res >= (int)sizeof(buffer)) { printf("CLerr %i\n", res); Freeze(0); } printf("Content-Length: %s\n", buffer); res = 0; while(true) { maConnRead(http, buffer, sizeof(buffer)-1); size = waitConn(http); if(size < 0) break; res += size; buffer[size] = 0; printf(buffer); } printf("Bytes read: %i\n", res); Freeze(0); return 0; }
int MAMain() { Handle conn; char ping[] = "ping\n"; char buffer[1024]; int res; #ifdef CONPRINT InitConsole(); gConsoleLogging = 1; #endif //println("Socket test"); retry: printlnf("Connecting..."); conn = maConnect("socket://130.237.3.104:6666"); //test DNS resolution res = AnimatedConnWait(); if(res <= 0) { printlnf("err %i", res); goto exit; } /*conn = maConnect("socket://217.25.35.146:81"); //test IP address reading res = AnimatedConnWait(); if(res <= 0) { printlnf("err %i", res); goto exit; }*/ println("Writing..."); maConnWrite(conn, ping, sizeof(ping)); res = AnimatedConnWait(); if(res <= 0) { printlnf("err %i", res); goto exit; } println("Reading..."); res = ConnReadAtLeast(conn, 4, sizeof(buffer), buffer); if(res <= 0) { printlnf("err %i", res); goto exit; } maConnClose(conn); printlnf("Got %i bytes", res); buffer[res] = 0; println(buffer); maUpdateScreen(); exit: println("Press a key"); for(;;) { EVENT event; while(maGetEvent(&event)) { if(event.type == EVENT_TYPE_CLOSE || (event.type == EVENT_TYPE_KEY_PRESSED && event.key == MAK_0)) { maExit(0); } else if(event.type == EVENT_TYPE_KEY_PRESSED) { goto retry; } } maWait(0); } }