Damian Szewc
Damian Szewc

Reputation: 415

How can i download data from server (ESP8266 WiFi module)?

I want to download string from local server. To be precise from an ESP8266 wifi module. I'm posting a pure string there like"TEST". I'm trying

using (WebClient client = new WebClient())
{
    string s = client.DownloadString("http://192.168.0.13");
    MessageBox.Show(s);
}

but the Exception throws:

     System.Net.WebException: The server committed a protocol violation. section=ResponseStatusLine
   w System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
   w System.Net.WebClient.DownloadString(Uri address)
   w System.Net.WebClient.DownloadString(String address)
   w logi.Logowanie.readEsp_Click(Object sender, EventArgs e) w d:\Projects Visual Studio\Projects\logi\logi\Logowanie.cs:line 81

I've also tried to build string in html so it looked like:

   string pagestr="<html><head><title>TEST</title></head><body<h2>Testing</h2></body></html>";

but the error is the same.

Sorry, I'm a total newbie in this...

Upvotes: 0

Views: 1789

Answers (3)

Azzam
Azzam

Reputation: 1

on esp8266 code add this line before your response sending

response = "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"
response += "any text or html code"
conn.send(response)
conn.close()

Upvotes: 0

Elek Neves
Elek Neves

Reputation: 1

using System.IO;
using System.Net;
using System.Net.Http;

namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //setLabel();

            Timer timer = new Timer();
            timer.Interval = (100); // 10 secs
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        private void timer_Tick(object sender, EventArgs e)
        {

            setLabel();
        }

        public void setLabel()
        {

            var url = "http://192.168.0.105/data.txt";

            var textFromFile = (new WebClient()).DownloadString(url);
            label1.Text = textFromFile + "Kg";

        }

       
    }
}

using System.IO;
using System.Net;
using System.Net.Http;

namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //setLabel();

            Timer timer = new Timer();
            timer.Interval = (100); // 10 secs
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        private void timer_Tick(object sender, EventArgs e)
        {

            setLabel();
        }

        public void setLabel()
        {

            var url = "http://192.168.0.105/data.txt";

            var textFromFile = (new WebClient()).DownloadString(url);
            label1.Text = textFromFile + "Kg";

        }

       
    }
}

Upvotes: 0

ProgrammerV5
ProgrammerV5

Reputation: 1962

This is not the safest or wiser solution but if you want to get out of a pickle you can add this to your .config file (on your .NET project) to avoid the problem right now:

<system.net>
    <settings>
        <httpWebRequest useUnsafeHeaderParsing="true" />
    </settings>
</system.net>

But you should be aware that there is some issue with your WebServer code. Maybe posting the server code might allow us to help you solve it.

Also you can try doing it this way:

HttpWebRequest myHttpWebRequest1 =
(HttpWebRequest)WebRequest.Create("http://192.168.0.13");
myHttpWebRequest1.KeepAlive=false;
HttpWebResponse myHttpWebResponse1 = 
        (HttpWebResponse)myHttpWebRequest1.GetResponse();

This way you can set up the KeepAlive property to false.

Upvotes: 1

Related Questions