HttpWebRequest Post 数据分包问题,如果才能整包发送数据?

whisht 2010-08-04 08:43:19
代码

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


请问:上述代码发送时拆分成两个包了,如果才能让包头和数据封装成一包整包发送出去???
...全文
439 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
蝶恋花雨 2010-08-04
  • 打赏
  • 举报
回复
//获取请求的内容
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 );
}

看看有没有帮助你的
whisht 2010-08-04
  • 打赏
  • 举报
回复
是在WINCE系统上运行的应用

[Quote=引用 2 楼 ayouxinping 的回复:]
好久没有涉及bs了,其实可以通过javascript来发送包数据的,你去网上搜索下,应该有很多的
[/Quote]
苍穹无痕 2010-08-04
  • 打赏
  • 举报
回复
好久没有涉及bs了,其实可以通过javascript来发送包数据的,你去网上搜索下,应该有很多的
fancy967 2010-08-04
  • 打赏
  • 举报
回复
第二个包通常是要POST的数据,分成一个包和两个包是一样的,没有任何区别,这个是由底层协议控制的,在应用层无法控制,也没有控制的必要,对方接收的话会先封装成一个包再传给应用层的,所以在应用层看来还是发送一个包,接收一个包

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧