//发送消息到服务器 public string HttpConnectToServer(string ServerPage) { byte[] dataArray = Encoding.Default.GetBytes("data=test"); //创建请求 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ServerPage); request.Method = "POST"; request.ContentLength = dataArray.Length; request.ContentType = "application/x-www-form-urlencoded"; //创建输入流 Stream dataStream = null; try { dataStream = request.GetRequestStream(); } catch (Exception) { return null;//连接服务器失败 } //发送请求 dataStream.Write(dataArray, 0, dataArray.Length); dataStream.Close(); //读取返回消息 string res = string.Empty; try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); res = reader.ReadToEnd(); reader.Close(); } catch (Exception) { return null;//连接服务器失败 } return res; }