代码string val = string.Empty;
byte[] buf = Encoding.GetEncoding("utf-8").GetBytes("1111111111");
try
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://www.baidu.com/"));
    request.ProtocolVersion = HttpVersion.Version10;
    request.KeepAlive = false;
    request.SendChunked = false;
    System.Net.ServicePointManager.Expect100Continue = false;
    request.Expect = null;
    request.Timeout = 60 * 1000;
    request.ContentType = "text/xml";
    request.Method = "POST";
    request.ContentLength = buf.Length;
    Stream stream = request.GetRequestStream();
    stream.Write(buf, 0, buf.Length);
    stream.Flush();
    stream.Close();    WebResponse response = request.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312")); ///, Encoding.GetEncoding(encoding));
    val = reader.ReadToEnd();
    reader.Close();
    response.Close();}
catch (Exception ex)
{
    val = "";
    //MessageBox.Show(ex.Message + "\r\n" + ex.ToString());
    MessageBox.Show(ex.ToString());
}
抓到发送的数据包:
协议:TCP 源IP:192.168.1.5 源端口:5079 目的IP:202.108.22.142 目的端口:80 TTL长度:128 数据大小:84
数据内容:POST / HTTP/1.1  Content-Type: text/xml  Host: www.baidu.com  Content-Length: 10    
 
协议:TCP 源IP:192.168.1.5 源端口:5079 目的IP:202.108.22.142 目的端口:80 TTL长度:128 数据大小:10
数据内容:1111111111
 请问:上述代码发送时拆分成两个包了,如果才能让包头和数据封装成一包整包发送出去???

解决方案 »

  1.   

    第二个包通常是要POST的数据,分成一个包和两个包是一样的,没有任何区别,这个是由底层协议控制的,在应用层无法控制,也没有控制的必要,对方接收的话会先封装成一个包再传给应用层的,所以在应用层看来还是发送一个包,接收一个包
      

  2.   

    好久没有涉及bs了,其实可以通过javascript来发送包数据的,你去网上搜索下,应该有很多的
      

  3.   

    //获取请求的内容
                    HttpWebRequest hwr = ( HttpWebRequest ) HttpWebRequest.Create( new Uri( EntryUrl ) );
                    hwr.Method = "POST";
                    hwr.KeepAlive = false;
                    hwr.ContentType = "application/x-www-form-urlencoded";
                    hwr.ContentLength = b.Length;
                    hwr.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; GTB6; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
                    hwr.CookieContainer = cookieCon;
                    //// 发送数据
                    using( Stream s = hwr.GetRequestStream() )
                    {
                        s.Write( b, 0, b.Length );
                    }
    看看有没有帮助你的