如何从服务器(ESP8266 WiFi模块)下载数据?

问题描述 投票:0回答:3

我想从本地服务器下载字符串。准确来说是 ESP8266 wifi 模块。我在那里发布了一个纯字符串,例如“TEST”。 我正在努力

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

但是异常抛出:

     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

我还尝试在 html 中构建字符串,所以它看起来像:

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

但错误是一样的。

抱歉,我在这方面完全是新手......

c# webclient system.net.webexception esp8266
3个回答
1
投票

这不是最安全或更明智的解决方案,但如果您想摆脱困境,您可以将其添加到您的

.config
文件(在您的 .NET 项目上)以避免立即出现问题:

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

但是您应该意识到您的 WebServer 代码存在一些问题。也许发布服务器代码可能会让我们帮助您解决问题。

你也可以尝试这样做:

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

这样您就可以将 KeepAlive 属性设置为 false。


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
投票

在 esp8266 代码上,在发送响应之前添加此行

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()
© www.soinside.com 2019 - 2024. All rights reserved.