目前我使用.net里面的HttpWebRequest和HttpWebResponse向服务器发送xml文件, 发送过去服务器一直没有响应, 我非常想了解服务器端是如何实现接收的? 我们知道tcp/ip里面的socket编程, 服务器是实时侦听一个端口, 客户端有请求过来就可以响应得到.  http协议里面服务器端是如何响应的呢?

解决方案 »

  1.   

    我也在研究post和get方法共同学习
      

  2.   

    要想向服务器url 发送数据, 客户端发送的http 标头有什么严格的定义吗?  标头定义不对是不是服务端无法接收?  这些都是未知数, 真想得到一个完整的客户端发送服务器端接收的实例就好了?  请有实例的献出实例, 没实例的讲讲你们的思路.
      

  3.   

    // 这个是客户端用来发送xml内容的
    private void post(string url, string content)
    {
          Encoding encoding = Encoding.GetEncoding("GB2312");
          byte[] data = encoding.GetBytes("xml="+content);      // 准备请求...
          HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
          myRequest.Method = "POST";
          myRequest.ContentType="application/x-www-form-urlencoded";
          myRequest.ContentLength = data.Length;
          Stream newStream=myRequest.GetRequestStream();
          // 发送数据
          newStream.Write(data,0,data.Length);
          newStream.Close();
    }// 服务端接收
    Request.Form("xml").ToString()
      

  4.   

    楼上的这个程序只是一种类型的, 我是用来post文件到服务器端url的, 
    httpWebRequest.ContentType = "multipart/form-data; boundary = ---------------------------7d429871607fe";  类似于文件上传,   我贴出我的代码, 大家帮助分析一下:
    --------------------------------------------------------------
    public void SendFile(string fileName, Uri uri)
            {
                CookieContainer cookies = new CookieContainer();            string boundary = "---------------------------7d429871607fe";  
                HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(uri);
                httpWebRequest2.Credentials = CredentialCache.DefaultCredentials;
                httpWebRequest2.CookieContainer = cookies;
                httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
                httpWebRequest2.Method = "POST";            // Build up the post message header
                StringBuilder sb = new StringBuilder();
                sb.Append("--");
                sb.Append(boundary);
                sb.Append("\r\n");
                sb.Append("Content-Disposition: form-data; name=\"file\"; filename=\"");
                sb.Append(Path.GetFileName(fileName));
                sb.Append("\"");
                sb.Append("\r\n");
                sb.Append("Content-Type: application/octet-stream");
                sb.Append("\r\n");
                sb.Append("\r\n");            string postHeader = sb.ToString();
                byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);            // Build the trailing boundary string as a byte array
                // ensuring the boundary appears on a line by itself
                byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);            httpWebRequest2.ContentLength = fileStream.Length + (long)postHeaderBytes.Length + (long)boundaryBytes.Length;   //一定需要长度吗?
                // Write out the trailing boundary            Stream requestStream = httpWebRequest2.GetRequestStream();            requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
                byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
                int bytesRead = 0;
                while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                    requestStream.Write(buffer, 0, bytesRead);
                requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
                fileStream.Close();
                fileStream = null;            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest2.GetResponse();
    -----------------------------------------------------------------------------------
    还有一个很奇怪的问题: 如果httpWebRequest2.ContentLength属性不设置,  在HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest2.GetResponse();地方将等待无限久, 不知道httpWebRequest2.ContentLength属性需不需要设置?  httpWebRequest2.ContentLength属性的作用是什么?
      

  5.   

    不要光Ding不说啊!  在线等急!
      

  6.   

    我来回答!
    遇到了和我一模一样的问题,我解决了!
    如果httpWebRequest2.ContentLength不设定,默认为0,也就是发送完http头之后,程序会连续发送两个空行。
    我上次是在httpWebRequest2.heads.add("ContentLength",xxx),不行,因为这样发送两个空行,导致服务器人为什么也没有发送,因为http协议规定头和内容之间要有一个空行,内容结束后也有一个空行 !!
    这就是原因!
    所以,不能httpWebRequest2.heads.add("ContentLength",xxx),必须设定httpWebRequest2.ContentLength!可能是雷库设计时的一个bug吧!
    嘿嘿!大家多多交流一下!