A http client sample program.

Dependencies:   NyFileSystems libMiMic mbed-rtos mbed

Fork of TcpSocketClientSamlpe by Ryo Iizuka

Revision:
26:f58dc24e2c1b
Parent:
25:1a4f620b7af6
Child:
28:dd350a9a5221
--- a/main.cpp	Wed Oct 02 08:34:02 2013 +0000
+++ b/main.cpp	Thu Oct 03 04:54:29 2013 +0000
@@ -12,7 +12,7 @@
 #include "utils/PlatformInfo.h"
 #include "fsdata.h"
 
-
+LocalFileSystem2 lf("local");
 NetConfig cfg; //create network configulation
 Net* net;
 
@@ -30,52 +30,51 @@
     cfg.setIpAddr(192,168,128,39);
     cfg.setNetMask(255,255,255,0);
     cfg.setGateway(192,168,128,254);    
-
+    cfg.setSrvUPnP(false);
+    cfg.setSrvMdns(false);
 
-    // Create tcp socket with 512 bytes RX buffer.
+    // Create http client.
     // Socket must create between "net.start" with "new Net()"
-    TcpSocket socket(512);
+    HttpClient http;
     
     //Start network
     net->start(cfg);
 
-
-    led1=1;    
-    for(;;){
-        //connect to server
-        if(!socket.connect(IpAddr(192,168,128,195),1234)){
-            Thread::wait(1000);
-        }
-        //connected!
-        led2=1;
-        for(;;){
-            led4=0;
-            led3=1;
-            //wait for data...
-            const void* rx;
-            //get read pointer
-            int l=socket.precv(rx);
-            if(l<0){
-                break;
+    if(http.connect(IpAddr(192,168,128,195),80)){
+        if(http.sendMethod(HttpClient::HTTP_GET,"/mimic/")){            
+            FILE *fp=fopen("/local/out.txt", "w");
+            if(fp!=NULL){            
+                int s=http.getStatus();
+                fprintf(fp, "Status:%d\n",s);
+                if(s==200){
+                    for(;;){
+                        short l;
+                        char b[32];
+                        if(!http.read(b,32,l)){
+                            //Error
+                            led1=1;
+                            break;
+                        }
+                        if(fwrite(b,1,l,fp)<l){
+                            //EOS
+                            led2=1;
+                            break;
+                        }
+                        if(l==0){
+                            //EOS
+                            led3=1;
+                            break;
+                        }
+                    }
+                }            
+                fclose(fp);
             }
-            if(l==0){
-                //timeout
-            }else{
-                //ok,echo back data.
-                led4=1;
-                //send data
-                if(!socket.send(rx,l)){
-                    break;
-                }
-                //move read pointer.
-                socket.pseek(l);
-            }
-            led3=0;
         }
-        led2=0;
-        led3=0;
-        led4=0;
-        socket.close(); //close the socket.
+        http.close();
+    }
+    for(int c=0;;c=(c+1)%2){
+        led4=c;
+        Thread::wait(500);
     }
     return 0;
 }