代码如下:TcpClient tcpClient = new TcpClient();
Uri URI = new Uri("http://192.168.18.66/getData");
tcpClient.Connect(URI.Host, URI.Port);
NetworkStream netStream = tcpClient.GetStream();
if (netStream.CanWrite)
{
string msg = "name=aabb11";
StringBuilder sb = new StringBuilder();
sb.Append("POST " + URI.PathAndQuery + " HTTP/1.0\r\n");
sb.Append("Host: " + URI.Host + "\r\n");
sb.Append("Content-Type: application/x-www-form-urlencoded\r\n");
sb.Append("Content-Length: " + Encoding.UTF8.GetByteCount(msg) + "\r\n");
sb.Append("\r\n");
sb.Append(msg + "\r\n");
Byte[] sendBytes = Encoding.UTF8.GetBytes(sb.ToString());
netStream.Write(sendBytes, 0, sendBytes.Length);
netStream.Flush();
} if (netStream.CanRead)
{
Thread.Sleep(10); //这个地方加上就能获取到数据,如果去掉大部分时候都不能获取到,只有少数时候能取得
byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
netStream.Read(bytes, 0, (int)tcpClient.ReceiveBufferSize);
string returndata = Encoding.UTF8.GetString(bytes);
returndata = returndata.Substring(0,returndata.IndexOf('\0')); Console.WriteLine("returns: " + returndata);
}
tcpClient.Close();
netStream.Close();Thread.Sleep(10); //这个地方加上就能获取到数据,如果去掉大部分时候都不能获取到,只有少数时候能取得,求解
Uri URI = new Uri("http://192.168.18.66/getData");
tcpClient.Connect(URI.Host, URI.Port);
NetworkStream netStream = tcpClient.GetStream();
if (netStream.CanWrite)
{
string msg = "name=aabb11";
StringBuilder sb = new StringBuilder();
sb.Append("POST " + URI.PathAndQuery + " HTTP/1.0\r\n");
sb.Append("Host: " + URI.Host + "\r\n");
sb.Append("Content-Type: application/x-www-form-urlencoded\r\n");
sb.Append("Content-Length: " + Encoding.UTF8.GetByteCount(msg) + "\r\n");
sb.Append("\r\n");
sb.Append(msg + "\r\n");
Byte[] sendBytes = Encoding.UTF8.GetBytes(sb.ToString());
netStream.Write(sendBytes, 0, sendBytes.Length);
netStream.Flush();
} if (netStream.CanRead)
{
Thread.Sleep(10); //这个地方加上就能获取到数据,如果去掉大部分时候都不能获取到,只有少数时候能取得
byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
netStream.Read(bytes, 0, (int)tcpClient.ReceiveBufferSize);
string returndata = Encoding.UTF8.GetString(bytes);
returndata = returndata.Substring(0,returndata.IndexOf('\0')); Console.WriteLine("returns: " + returndata);
}
tcpClient.Close();
netStream.Close();Thread.Sleep(10); //这个地方加上就能获取到数据,如果去掉大部分时候都不能获取到,只有少数时候能取得,求解
解决方案 »
- rowMergeView显示两个数据
- C#读二进制文件,如何读取2个字节,但不提升字节位置。
- datagridview 行选择改变是哪个事件?
- C# 时间
- 多线程的问题,急啊!就要交差了!
- adsafe,pp助手之类的软件是用什么做的?
- DataGrid中数据下载问题
- 大家帮忙看看这段代码,我想你一定也感兴趣!
- PrintDocument怎样设纸张大小?自定义纸张。(我查了一下,以前有人问过,可惜升级后,很多都没有了:)
- 请问如何在一个datagrid 中显示多个表的内容,也就是将这两个表通过共有字段连接后显示
- C#的comboBox 控件怎么实现按下回车键就弹出下拉框呢,我实现了弹出下拉框,但是弹出来一瞬间就又弹回去了
- C# Ftp上传文件报553错误
streamReader.ReadLine()