C#写的一段与HTTP响应的接收和发送消息的代码没法跑通求教原因
然后是我下面写的WINFORM代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Threading;
using System.Net.Sockets;
using System.Diagnostics;
namespace myInitialProject
{
public partial class Form2 : Form
{
Thread serverThread;
Thread clientThread;
Socket serverSocket;
private byte[] result = new byte[1024];
//Socket clientSocket;
public Form2()
{
InitializeComponent();
}
public void ServerStart()
{
listBoxInfo.Items.Add("Server Start...");
string host = "127.0.0.1"; // Form1就通过IPAddress.Any去定义
int nPort = 10000;
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(IPAddress.Parse(host), nPort));
serverSocket.Listen(10);
serverThread = new Thread(ListenClientConnect); // 监听客户端
serverThread.Start();
}
/// <summary>
/// 监听客户端连接
/// </summary>
public void ListenClientConnect()
{
while (true)
{
Socket clientSocket = serverSocket.Accept();
//clientSocket.Send(Encoding.UTF8.GetBytes("已连接服务器"));
Thread receiveThread = new Thread(ReceiveMessage);
//设置为后台线程,随着主线程退出而退出
receiveThread.IsBackground = true;
receiveThread.Start(clientSocket);
}
}
/// <summary>
/// 接收信息
/// </summary>
/// <param name="clientSocket"></param>
public void ReceiveMessage(object clientSocket)
{
Socket myClientSocket = (Socket)clientSocket;
byte[] data = new byte[1024];
int nReceiveNumber;
byte[] tempData = new byte[1024 * 2]; // 浏览器发来的数据
int reciverNumber = myClientSocket.Receive(tempData);
string tempStr = Encoding.UTF8.GetString(tempData, 0, reciverNumber);
string resdata = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " server request time:" + 0;
byte[] body = Encoding.UTF8.GetBytes(resdata);
byte[] head = Encoding.UTF8.GetBytes(@"HTTP/1.1 200 OK
Content-Length: " + body.Length + @"
Content-Type: text/plain
Date: " + string.Format("{0:R}", DateTime.Now) + @"
Server: Cftea Web Server" + resdata);
StringBuilder sb = new StringBuilder();
sb.Append("HTTP/1.1 200 OK\r\n");
sb.Append("Content-Type: text/plain\r\n");
sb.Append("\r\n");
sb.Append("测试返回的数据\r\n");
byte[] responseData = Encoding.UTF8.GetBytes(sb.ToString());
myClientSocket.Send(responseData);
myClientSocket.Close();
/*while (true)
{
try
{
// 接收数据
nReceiveNumber = myClientSocket.Receive(result);
if (nReceiveNumber == 0)
{
return;
}
string strMessage = Encoding.UTF8.GetString(result, 0, nReceiveNumber);
// 这里打印出客户端发送回来的消息
Trace.WriteLine("接收到的返回信息:" + myClientSocket.RemoteEndPoint.ToString() + " " + strMessage);
// 发送回客户端
//string strWelcome = "已连接上服务器";
// 取得字符串的字节序列
//data = Encoding.UTF8.GetBytes(strWelcome);
//myClientSocket.Send(data, data.Length, SocketFlags.None);
// 短连接 发送完关闭SOCKET
//myClientSocket.Close();
if (myClientSocket.Connected)
{
// 判断客户端是否已关闭
// 此方法不能检测某些类型的连接问题,
// 例如,网络电缆中断或远程主机意外关闭。您必须尝试发送或接收数据以检测这些类型的错误。
if (myClientSocket.Poll(0, SelectMode.SelectRead))
{
{
//if (nRead == 0)
{
myClientSocket.Close();
MessageBox.Show("客户端已关闭");
break;
}
}
}
else
{
MessageBox.Show("客户端未关闭");
}
}
}
catch (System.Exception ex)
{
MessageBox.Show("server listening Error: " + ex.Message);
// 禁止发送和上传
//myClientSocket.Shutdown(SocketShutdown.Both);
// if(myClientSocket.Poll(-1, SelectMode.SelectRead))
// {
// byte[] judgeData = new byte[1];
// int nRead = myClientSocket.Receive(judgeData, SocketFlags.Peek);
// if(nRead != 0)
// myClientSocket.Close();
// }
break;
}
}*/
}
/// <summary>
/// 监听按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnListen_Click(object sender, EventArgs e)
{
ServerStart();
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
try
{
if (serverSocket != null)
{
serverSocket.Close();
}
if (serverThread != null)
{
// Thread对象有个IsBackground属性,设置为true即可,在主线程关闭时,子线程随之关闭
serverThread.IsBackground = true;
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
这段代码用这个在线测试工具是没通过,总是这样的结果

但我用POSTMAN做本地测试是可以通过的啊

也试过放到别人服务器上去测试,也是不行,

按道理来说不是本地测试通过,就行了吗? 为什么真正放服务器上跑就不行呢?
这到底是什么原因? 请各位赐教!

相关推荐
c#版的微信开发接收和发送消息示例
微信的官方文档,接收和发送消息处理示例
Winform高效实现HTTP POST提交JSON与接收响应:从HttpClient到异步UI不卡顿
HTTP POST与JSON是现代接口通信的基石:前者将数据置于请求体中传递,适合提交结构化数据;后者作为轻量级数据交换格式,成为前后端协作的事实标准。在实际工程中,正确设置Content-Type、编码与超时参数,是保障接口联调顺利的关键技术细节。借助HttpClient与async/await异步模型,开发者可以避免Winform界面卡死,提升程序响应速度与用户体验。此类技术广泛应用于Winform工具对接Web API、调用第三方平台、接入AI服务等场景。围绕这些基础概念,文章逐步拆解了在Winfor
C# WinForms群消息推送机器人:异步队列与界面优化实践
在自动化运维与业务通知场景中,群消息推送机器人是企业内外部信息流转的重要载体。C#凭借完善的异步编程模型与成熟的WinForms桌面框架,为构建高可用的群通知工具提供了坚实基础。其核心思路是利用HttpClient封装轻量级HTTP接口,通过System.Threading.Channels构建有界队列实现消息削峰与节流,再借助PeriodicTimer驱动定时任务,并基于Windows消息机制安全地刷新界面层。这种架构清晰分离了网络通信、任务调度与UI展示,有效规避了线程竞争和界面卡顿风险,同时为错误码分
C#微信企业号接收和发送消息
用C#写的,微信企业号接收和发送消息,例子是把用户发送的消息回复给用户,内容可以自己修改,其中接收和发送消息可以单独使用。很珍贵的哦。
C#最精简的微信企业号发送消息
注意:JsonHelper需要引用System.Runtime.Serialization;需要有xml文件记录token和expiretime;只有发送消息;接收消息待上传。。。


