0

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...

Damian Szewc
  • 305
  • 2
  • 10

3 Answers3

1

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.

ProgrammerV5
  • 1,915
  • 2
  • 12
  • 22
  • I've tried this. Doesn't work either. Actually the problem is with my WebServer code.It has no protocol at all it only posts a String. I've got the solution but got banned so can't write the code. Eventually I used System.Net.Sockets; library and it helped a lot – Damian Szewc Jan 08 '16 at 17:21
0

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";

        }

       
    }
}
0

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()
Azzam
  • 1
  • Thank you for your response. It was a question I've asked so long ago, when i was in university. Was a total newbie back then, my ESP server was rubbish it could not work at all because I didn't understand how simple server works :D Now I'm a bit wiser and see how my question was pointless. Thank you for answering and remembering me that nostalgic time! Cheers – Damian Szewc Aug 09 '23 at 17:27