日期:2014-05-19  浏览次数:21013 次

使用http协议post方式从客户端发送文件到服务器端, 服务器端如何实现接收文件?

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

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