如何从服务器(ESP8266 WiFi 模块)下载数据?
Posted
技术标签:
【中文标题】如何从服务器(ESP8266 WiFi 模块)下载数据?【英文标题】:How can i download data from server (ESP8266 WiFi module)? 【发布时间】:2016-01-06 20:53:21 【问题描述】:我想从本地服务器下载字符串。准确地说是 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>";
但错误是一样的。
对不起,我是这方面的新手......
【问题讨论】:
ESP8266 上是否运行网络服务器?在浏览器中打开网址能到达吗? 异常提示服务器没有返回有效数据。您是否查看过服务器响应的网络捕获? 是的,我什至可以使用 Ctrl + Click 来访问 Visual Studio 会打开写着“测试”的网页 向我们展示您的 ESP8266 代码。 尝试使用 TCP 套接字连接。 【参考方案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。
【讨论】:
我试过这个。也不行。实际上问题出在我的 WebServer 代码上。它根本没有协议,它只发布一个字符串。我有解决方案,但被禁止,所以无法编写代码。最终我使用了 System.Net.Sockets;图书馆,它帮助了很多【参考方案2】: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";
【讨论】:
以上是关于如何从服务器(ESP8266 WiFi 模块)下载数据?的主要内容,如果未能解决你的问题,请参考以下文章
如何使 AT 命令在 arduino 中为 ESP8266 wifi 模块以编程方式工作