-->
Page 1 of 1

Can't Download Web Page Text Programmatically

PostPosted: Mon Feb 15, 2016 12:08 pm
by SimoneLuconi
Hi, i have an ESP-01 with a Lua Web Server, but i can't download the content of the Web Server via code, i have tried in C# and in Java (with android) but i get the same result.

This is the content of init.lua:
Code: Select allwifi.setmode(wifi.STATION)
wifi.sta.config("Gandalf","simonecleomartina")
uart.setup(0,9600,8,0,1)
print(wifi.sta.getip())
print ("uart.setup(0,9600,8,0,1) finished")
print ("waiting for serial data....")
tempst = "N.D.";
srv=net.createServer(net.TCP)
srv:listen(46,function(conn)
    conn:on("receive", function(client,request)
        local buf = "";
        local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
        if(method == nil)then
            _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
        end
        local _GET = {}
        if (vars ~= nil)then
            for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
                _GET[k] = v
            end
        end
        buf = buf.."<h1> ESP8266 Web Server</h1>";
        buf = buf.."<p>Temperatura "..tempst;
        local _on,_off = "",""
        if(_GET.pin == "Cancello")then
              print("1");
        elseif(_GET.pin == "Garage")then
              print("2");
        end
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)

function temp(value,decimal)
  print("Temperatura("..value.."."..decimal..") ricevuta")
  tempst = value.."."..decimal;
end


This is the code that i use in C# for downloading the web page content:
Code: Select all            int timeout = 5000;
            SetAllowUnsafeHeaderParsing20();
            HttpWebRequest myHttpWebRequest1 = (HttpWebRequest)WebRequest.Create(action);
            myHttpWebRequest1.KeepAlive = false;
            myHttpWebRequest1.ServicePoint.Expect100Continue = false;
            myHttpWebRequest1.Timeout = timeout;
            myHttpWebRequest1.ReadWriteTimeout = timeout;
            myHttpWebRequest1.ServicePoint.MaxIdleTime = timeout;
            myHttpWebRequest1.ServicePoint.ConnectionLeaseTimeout = timeout;
            HttpWebResponse myHttpWebResponse1 = (HttpWebResponse)myHttpWebRequest1.GetResponse();
            Stream receiveStream = myHttpWebResponse1.GetResponseStream();
            Encoding enc = System.Text.Encoding.UTF8;
            StreamReader loResponseStream = new StreamReader(receiveStream, enc);


And i get:
Code: Select allSystem.Net.WebException non รจ stata gestita
  HResult=-2146233079
  Message=Connessione sottostante chiusa: Chiusura imprevista della connessione..
  Source=System
  StackTrace:
       in System.Net.HttpWebRequest.GetResponse()
       in Arduino.Form1.aggiornadati(String action) in C:\Users\PC-Simone\Desktop\New\Arduino\Form1.cs:riga 115
       in Arduino.Form1.buttonCancello_Click(Object sender, EventArgs e) in C:\Users\PC-Simone\Desktop\New\Arduino\Form1.cs:riga 53
       in System.Windows.Forms.Control.OnClick(EventArgs e)
       in System.Windows.Forms.Button.OnClick(EventArgs e)
       in System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       in System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       in System.Windows.Forms.Control.WndProc(Message& m)
       in System.Windows.Forms.ButtonBase.WndProc(Message& m)
       in System.Windows.Forms.Button.WndProc(Message& m)
       in System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       in System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       in System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       in System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       in System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       in System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       in System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       in System.Windows.Forms.Application.Run(Form mainForm)
       in Arduino.Program.Main() in C:\Users\PC-Simone\Desktop\New\Arduino\Program.cs:riga 18
       in System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       in System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       in System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
       in System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
       in System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
       in System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
       in System.Activator.CreateInstance(ActivationContext activationContext)
       in Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
       in System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       in System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       in System.Threading.ThreadHelper.ThreadStart()
  InnerException:


My Visual Studio is in italian, the translation of the message is: The underlying connection was closed: unexpected connection close.

This is the code that i use in Android (i use the OkHttp Library):
Code: Select allOkHttpClient client = new OkHttpClient();

                Request request = new Request.Builder()
                        .url(params[0])
                        .build();

                Response response = client.newCall(request).execute();
                String result = response.body().string();


And i get:
java.io.IOException: unexpected end of stream on Connection

With other url the code works perfectly, but with the web page of the ESP don't work.
Any solution? Thanks!